/
ArticlesDataTable.vue
191 lines (156 loc) · 6.09 KB
/
ArticlesDataTable.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<template>
<div class="table-responsive">
<table :id="id" class="table table-striped table-bordered table-hover nowrap" style="width:100%;">
<thead>
<tr>
<th>id</th>
<th>Title</th>
<th>Created</th>
<th>Visible</th>
<th>Author</th>
<th></th>
</tr>
</thead>
<tbody>
<tr v-for="article in articles" :key="article.id">
<td>{{article.id}}</td>
<td>{{article.title}}</td>
<td :data-order="article.created_at">{{getCreatedAt(article.created_at)}}</td>
<td :class="article.visible ? 'text-success' : 'text-danger'">{{article.visible ? 'visible' : 'hidden'}}</td>
<td>{{article.user.name}}</td>
<td class="actions">
<router-link :to="{name: 'Article edit', params: { id: article.id}}" class="action"><font-awesome-icon icon="pencil-alt" /></router-link>
<a href="#" @click.prevent="toggleVisibility(article.id)" class="action"><font-awesome-icon icon="eye" /></a>
<a href="#" @click.prevent="deleteArticle(article.id, $event)" class="action text-danger deleteBtn"><font-awesome-icon icon="trash" /></a>
</td>
</tr>
</tbody>
</table>
</div>
<Loader v-if="loading" />
</template>
<script>
import { mapActions } from 'vuex'
import apiRoutes from "@/router/apiRoutes"
//Bootstrap and jQuery libraries
//import 'jquery/dist/jquery.min.js';
import $ from 'jquery'
//Datatable Modules
//import "datatables.net-dt/js/dataTables.dataTables"
//import "datatables.net-dt/css/jquery.dataTables.min.css"
import "datatables.net-bs4/css/dataTables.bootstrap4.css"
//import "datatables.net-bs4/js/dataTables.bootstrap4.js"
import 'datatables.net-bs4'
// fa font awesome
import { library } from '@fortawesome/fontawesome-svg-core'
import { faPencilAlt, faEye, faTrash } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
library.add(faPencilAlt, faTrash, faEye)
import Loader from '@/components/Loader'
export default {
name: 'ArticlesDataTable',
data() {
return {
id: "articlesDataTable",
articles: [],
dataTableObject: null,
loading: 0,
}
},
computed: {
issetArticles() {
return this.articles.length > 0;
}
},
methods: {
...mapActions('alerts', ['setErrorAlert', 'setSuccessAlert']),
getArticles() {
this.loading++;
axios.get( apiRoutes.ARTICLES_URL )
.then((response) => {
this.articles = response.data.articles;
this.$nextTick(function() {
this.setDataTable();
});
})
.catch( error => {
console.log(error);
this.setErrorAlert('Nepodarilo sa načítať články.');
})
.then( () => this.loading-- );
},
toggleVisibility(id) {
this.loading++;
axios.get( apiRoutes.ARTICLE_VISIBILITY_URL + id )
.then( response => {
if( response.data.error ) return this.setErrorAlert(response.data.error);
this.getArticles();
this.setSuccessAlert('Viditeľnosť článku bola zmenená.');
})
.catch( error => {
console.log(error);
this.setErrorAlert('Nepodarilo sa zmeniť viviteľnosti článku.');
})
.then( () => this.loading-- )
},
deleteArticle(id, e) {
this.loading++;
axios.get( apiRoutes.ARTICLE_DELETE_URL + id )
.then( response => {
if( response.data.error ) return this.setErrorAlert(response.data.error);
this.dataTableObject.row( $(e.target).closest('tr') ).remove().draw();
this.setSuccessAlert('Článok bol vymazaný.');
})
.catch( response => {
console.log(response);
this.setErrorAlert('Nepodarilo sa vymazať článok.');
})
.then( () => this.loading-- );
},
setDataTable() {
var table = $('#'+this.id);
if( this.dataTableObject ) return this.dataTableObject.draw();
this.dataTableObject = table.DataTable({
"pageLength": 10,
"order": [[0, table.data('sort')]],
"aoColumnDefs": [
{
"bSortable": false,
"aTargets": [-1] // gets last column and turns off sorting
}
],
"language": {
"url": "//cdn.datatables.net/plug-ins/1.10.21/i18n/Slovak.json",
}
});
// This is a bug in datatables-bs4 v3.2.x
/*this.dataTableObject.on('draw', function(e) {
$(this).closest('.dataTables_wrapper')
.removeClass('form-inline')
.find('.col-xs-12').addClass('col-12');
});*/
},
getCreatedAt(timestamp) {
let date = new Date(timestamp * 1000);
let day = date.getDate() + 1;
let month = date.getMonth() + 1;
let year = date.getFullYear();
let hours = ('0' + date.getHours()).substr(-2);
let minutes = ('0' + date.getMinutes()).substr(-2);
return day + '.' + month + '.' + year + ' ' + hours + ':' + minutes;
},
},
// Hook
created() {
this.getArticles();
},
components: {
'font-awesome-icon': FontAwesomeIcon, Loader
},
}
</script>
<style scoped>
table.dataTable {
border-collapse: collapse !important;
}
</style>