-
Notifications
You must be signed in to change notification settings - Fork 4
/
DataTable.vue
751 lines (699 loc) · 18.5 KB
/
DataTable.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
<!--
Vue Datatable
Description: Vue Component for datatable with search, sorting, editing and pagination
Author: Antonio Okoro
Version: 1.0.0
-->
<template>
<div class="data-table">
<div class="data-table-loading" v-if="loading || ajaxLoading">
<div class="data-table-loading-spinner"></div>
<div class="data-table-loading-text">Loading Data</div>
</div>
<div class="data-table-inner" v-else>
<div class="row data-table-control" v-if="header">
<div class="col-md-6" v-if="limitable">
<div class="form-group">
<label>
Show
<select class="custom-select custom-select-sm" v-model="itemsPerPage">
<option value="1">1</option>
<option value="2">2</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="75">75</option>
<option value="100">100</option>
</select>
rows
</label>
</div>
</div>
<div class="col-md-6" v-if="searchable">
<div class="form-group">
<input type="text" class="form-control form-control-sm" placeholder="Search Records" @keyup="search(query)" v-model="query">
</div>
</div>
<div class="col-auto ml-auto" v-if="showFilters">
Filters:
<div class="table-filters d-inline-block">
<div class="table-filter" v-for="option in filters" @click="filter(option)">
<span>{{ option.title }}</span>
</div>
</div>
</div>
</div>
<div class="table-responsive">
<table class="table" :class="{straight: !breakWords, 'table-hover': !!onClick}">
<thead>
<tr>
<!-- Display Checkboxes If Requested -->
<th v-if="selectable">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" @change="selectAll">
<span class="custom-control-label"></span>
</label>
</th>
<!-- Display Index If Requested -->
<th
v-if="index" @click="sortIndex()"
class="sortable"
:class="{sort: sortColumn == '#', 'asc': sortColumn == '#' && asc, 'desc': sortColumn == '#' && !asc}"
>#</th>
<!-- Display All Parsed Headers -->
<th
v-bind:key="index"
v-for="(th, index) in headers"
@click="sort(th.name)"
class="sortable"
:class="{sort: sortColumn == th.name, 'asc': sortColumn == th.name && asc, 'desc': sortColumn == th.name && !asc}"
v-if="th.show"
>{{ th.th }}</th>
<!-- Display Actions If Provided -->
<th v-if="actions.length">Actions</th>
</tr>
</thead>
<tbody v-if="paginatedItems.length">
<!-- Loop Through All Parsed and Paginated Items -->
<tr v-bind:key="i" v-for="(item, i) in paginatedItems" :class="{clickable: !!onClick}">
<!-- Display Checkboxes If Requested -->
<th v-if="selectable">
<div class="custom-control custom-checkbox" @click="select(item)">
<input type="checkbox" class="custom-control-input" :checked="item.selected">
<span class="custom-control-label"></span>
</div>
</th>
<!-- Display Index If Requested -->
<td v-if="index">{{ item.index + 1 }}</td>
<!-- Display All Parsed Values -->
<td v-bind:key="j" v-for="(td, j) in item.details" @click="click(item.row, td.value, td.name, i), columnClick(td.click, item.row, td.value, td.name, i)" v-if="td.show">
<!-- <component :is="i+'Component'" v-if="value.render"></component> -->
<span v-html="td.rendered != null ? td.rendered : '----'"></span>
</td>
<!-- Diplay Actions If Provided -->
<td v-if="item.buttons.length">
<!-- Loop Through All Provided Actions -->
<button
type="button"
class="btn"
:class="`btn-${button.color} btn-${button.size}`"
v-bind:key="j"
v-for="(button, j) in item.buttons"
@click="button.action(item.row, item.index)"
v-if="button.show"
:disabled="button.disabled"
>
{{ button.text }}
</button>
</td>
</tr>
</tbody>
<tbody v-else>
<!-- Display Empty Message If No Items Are Rendered -->
<tr>
<td align="center" :colspan="headers.length + (actions.length ? 1 : 0) + (index ? 1 : 0)">No results</td>
</tr>
</tbody>
</table>
</div>
<div class="row" v-if="footer">
<div class="col-md-6" v-if="pageDetails">
<div class="showing">
Showing
<!-- Current Page Starting Index -->
{{ paginatedItems.length ? (itemsPerPage * (currentPage - 1)) + 1 : 0 }}
to
<!-- Current Page End Index -->
{{ (itemsPerPage * (currentPage -1 )) + paginatedItems.length }}
of
<!-- All Items Provided -->
{{ renderedItems.length }} items
</div>
</div>
<div class="col-md-6" v-if="paginate">
<ul class="pagination" v-if="paginateLinks.length">
<li class="page-item" v-if="pages && currentPage != 1">
<span class="page-link" @click="prev">Prev</span>
</li>
<li class="page-item" v-bind:key="item.page" v-for="item in paginateLinks" :class="{active: currentPage == item.page}">
<span class="page-link" @click="paginate(item.page)">{{ item.page }}</span>
</li>
<li class="page-item" v-if="pages && currentPage < pages">
<span class="page-link" @click="next">Next</span>
</li>
</ul>
</div>
</div>
</div>
</div>
</template>
<script>
import toastr from "toastr";
import Axios from "axios";
import moment from "moment";
import Vue from 'vue';
export default {
data() {
return {
// Items TO Display For Each Paginated Page
itemsPerPage: 100,
// Current Page Number In Pagination
currentPage: 1,
// Current Page Items
paginatedItems: [],
// Sort Order
asc: "asc",
// Column For Sorting
sortColumn: null,
// Search Query
query: '',
// Table Headers
headers: [],
// Mapped Data
items: [],
// Mapped Action Buttons
buttons: [],
// Loading State For Ajax Requests
ajaxLoading: false,
// Items To Be Displayed
renderedItems: [],
// Selected Items
selected: [],
}
},
props: {
// =================================
// Ajax
// Datatables Ajax Uses Axios
// Make Sure Axios Is Added As An NPM Dependency
// =================================
// Ajax URL
url: {
type: String,
default: () => ''
},
// Whether Or Not To Use Ajax
ajax: {
type: Boolean,
default: () => false
},
// Ajax Headers
AjaxHeaders: {
type: Object,
default: () => {}
},
// Table Items
data: {
type: Array,
default: () => []
},
// Action Buttons For Each Item
actions: {
type: Array,
default: () => []
},
// Columns and Appropriate Data Assigment
columns: {
type: Array,
default: () => []
},
filters: {
type: Array,
default: () => []
},
// Whether or Not Items Should Be Indexed
index: {
type: Boolean,
default: () => true
},
// Set Loading Status
loading: {
type: Boolean,
default: () => false
},
// Click Events For Each Cell
onClick: {
type: Function,
default: () => {}
},
// Whether Or Not The Table Should Be Allowed To Break Elements
breakWords: {
type: Boolean,
default: () => false
},
// Whether Or Not The Header Should Be Visible
header: {
type: Boolean,
default: () => true
},
// Whether Or Not The Footer Should Be Visible
footer: {
type: Boolean,
default: () => true
},
// Whether Or Not Searching Should Be Available
searchable: {
type: Boolean,
default: () => true
},
// Whether Or Not Page Limitation Should Be Changeable
limitable: {
type: Boolean,
default: () => true
},
// Whether Or Not Details Should Be Visible
pageDetails: {
type: Boolean,
default: () => true
},
// Whether Or Not The Results Should Be Paginatable
paginatable: {
type: Boolean,
default: () => true
},
// Whether Or Not Items Should Be Selctable
selectable: {
type: Boolean,
default: () => false
}
},
methods: {
// Navigate To Provided Page
// Arguments
// Page: int
paginate(page) {
this.currentPage = page;
},
// Navigate To Next Page
next() {
this.currentPage = this.currentPage >= this.renderedItems.length ? 0 : this.currentPage + 1;
},
// Navigate To Previous Page
prev() {
this.currentPage = this.currentPage <= 0 ? this.renderedItems.length : this.currentPage - 1;
},
// Navigate To Last Page
end() {
this.currentPage = this.renderedItems.length;
},
// Navigate To First Page
start() {
this.currentPage = 1;
},
// Search Through Items With Provided Search Query
// Arguments
// Query: string
search(query) {
var items = this.mapItems(this.items);
let retval = items.filter(item => {
var found = false;
// Search In Mapped Data
item.details.forEach(detail => {
// Cancel If Original And Processed Value Are NULL
if (detail.value == null || detail.rendered == null) {
return;
}
// If Found In Original Value
if (detail.value.toString().match(new RegExp(query, "i"))) {
found = true;
}
// If Found In Processed Value
if (detail.rendered.toString().match(new RegExp(query, "i"))) {
found = true;
}
});
// Search In Provided Data
for (var column in item.row) {
if (!item.row[column]) {
continue;
}
if (item.row[column].toString().match(new RegExp(query, "i"))) {
found = true;
}
}
return found;
});
this.renderedItems = retval;
this.sortIndex(false);
},
// Sort Items By Specified Column and Order
// Arguments
// Column: String
// Order: String [asc, desc]
sort(column) {
this.renderedItems = this.renderedItems.sort((a,b) => {
var detailx = a.details.find(detail => detail.name == column);
var x = detailx.rendered;
if (!x) {
}
x = typeof x == 'string' ? x.toLowerCase() : x;
var detaily = b.details.find(detail => detail.name == column);
var y = detaily.rendered;
if (!x) {
}
y = typeof y == 'string' ? y.toLowerCase() : y;
return x > y ? 1 : -1;
});
if (column !== this.sortColumn) {
this.asc = true;
}else {
this.asc = !this.asc;
}
if (!this.asc) {
this.renderedItems = this.renderedItems.reverse();
}
this.sortColumn = column;
this.currentPage = 1;
},
sortIndex(asc){
this.renderedItems = this.renderedItems.sort((a, b) => {
var indexA = a.index;
var indexB = b.index;
return indexA > indexB ? 1 : -1;
});
this.asc = this.sortColumn == '#' ? !this.asc : true;
if (asc != undefined) {
if (!asc) {
// console.log(this.renderedItems);
this.renderedItems = this.renderedItems.reverse();
}
}else {
if (!this.asc) {
this.renderedItems = this.renderedItems.reverse();
}
}
this.sortColumn = '#';
this.currentPage = 1;
},
filter(filter){
var filterValue = filter.value,
filterColumn = filter.name;
var items = this.mapItems(this.items);
var filtered = items.filter((item, index) => {
var column = item.details.find((column, i) => column.name == filterColumn);
if (!column) {
return false;
}
// If Value Type Is A Custom Function
if (filterValue.constructor.toString().match(/Function/)) {
if (filterValue(item.row, column.value, index)) {
return true;
}
}else if (column.value == filterValue || column.rendered == filterValue) {
return true;
}
return false;
});
this.renderedItems = filtered;
this.currentPage = 1;
this.sortIndex(false);
},
getHeaders() {
this.headers = this.columns.map((item) => ({name: item.name, th: item.th, show: item.show !== false}));
},
mapItems(items) {
items = items.map((item, index) => {
// Row Item
var row = {
row: item,
details: [],
index,
buttons: [],
selected: !!this.selected.find(a => a.index == index)
};
// Get Provided Columns
this.columns.forEach((column, index2) => {
row.details.push({
// Item Column Name
name:column.name,
// Table Header Title
th: column.th,
// Provided Value
value: item[column.name],
// Decide Value Depending On Whether Render Method Is Provided
rendered: column.render ? column.render(item, item[column.name], index) : item[column.name],
// Origin Item Row
row: item,
// Whether Or Not To Display Item
show: column.show !== false,
// Click Event For Column
click: column.click
});
});
// Get Provided Actions
this.actions.forEach((button, index3) => {
row.buttons.push({
// Spread Provided Button Properties
...button,
// Decide Visibility Depending On Whether Show Method Is Provided
// Default: true
show: button.show ? button.show(item,index) : true,
disabled: button.disabled ? button.disabled(item, index) : false
});
});
return row;
});
return items;
} ,
click(row, cell, name, index) {
if (this.onClick) {
this.onClick(...arguments);
}
},
columnClick(action, row, cell, name, index) {
if (action) {
action(row, cell, name, index);
}
},
selectAll(event) {
if (event.target.checked) {
this.selected = [];
this.renderedItems.forEach(item => {
item.selected = true;
this.selected.push(item);
});
}else {
this.selected = [];
this.renderedItems.forEach(item => {
item.selected = false;
});
}
},
select(item) {
var index = this.selected.findIndex(a => a.index == item.index);
if (index > -1) {
item.selected = false;
this.selected.splice(index, 1);
}else {
item.selected = true;
this.selected.push(item);
}
},
// Alerts
success(success = "Success") {
toastr.success(success);
},
error(error = "Error") {
toastr.error(error);
}
},
computed: {
// Total Number Of Pages For Pagination
pages() {
if (this.renderedItems.length > this.itemsPerPage) {
return Math.ceil(this.renderedItems.length / this.itemsPerPage);
}else {
return 0;
}
},
// Array Of Links With Page Number For Pagination
paginateLinks() {
var links = [];
var approved = [];
let center = Math.round(this.pages / 2) - 1 ;
for (var i = 0; i < this.pages; i++) {
if (this.pages > 6) {
let difference = this.currentPage - i;
let centerDifference = center - i;
// around the current page
if (!(difference < 0) && !(difference > 2)) {
// around the center
}else if (i === center) {
// at the start or end
}else if (this.pages - i <= 2 || i <= 1){
// everywhere else
}else {
continue;
}
}
links.push({page: i + 1});
}
return links;
},
showFilters() {
return Object.keys(this.filters).length > 0;
}
},
watch: {
currentPage(newValue) {
this.paginatedItems = this.renderedItems.slice(this.itemsPerPage * (newValue - 1), (this.itemsPerPage * newValue));
},
itemsPerPage(newValue) {
this.currentPage = 1;
this.paginatedItems = this.renderedItems.slice(newValue * (this.currentPage - 1), (newValue * this.currentPage));
},
items(newValue) {
this.getHeaders();
this.renderedItems = this.mapItems(newValue);
// Get All Items In Current Page
this.paginatedItems = this.renderedItems.slice(this.itemsPerPage * (this.currentPage - 1), (this.itemsPerPage * this.currentPage));
this.asc = true;
this.sortIndex();
},
data(newValue) {
this.items = newValue;
},
renderedItems(newValue) {
this.paginatedItems = newValue.slice(this.itemsPerPage * (this.currentPage - 1), (this.itemsPerPage * this.currentPage));
}
},
// Lifetime Events
async mounted() {
// Parse Headers
this.getHeaders();
// Set Default Sorting To Index
// Asc will be converted to false so order will be in reverse
this.asc = true;
this.sortIndex();
// Use Provided Data If Ajax Is Not Specified
if (!this.ajax) {
// Map Items From Provided Data
this.items = this.data;
// Get All Items In Current Page
// this.paginatedItems = this.renderedItems.slice(this.itemsPerPage * (this.currentPage - 1), (this.itemsPerPage * this.currentPage));
}else {
// Get Data From Server Using Ajax
this.ajaxLoading = true;
await Axios
.get(this.url)
.then(response => {
if (!response.data.data) {
return this.error("Unable To Parse Data");
}
this.items = response.data.data;
this.success("Data Loaded");
})
.catch(error => {
this.error(error || "Unable To Load Data");
});
this.ajaxLoading = false;
}
}
}
</script>
<style lang="sass">
@keyframes spin
from
transform: rotate(0deg)
to
transform: rotate(359deg)
.data-table
// font-size: 14px
&-loading
align-items: center
display: flex
height: 200px
flex-flow: column
justify-content: center
position: relative
width: 100%
&-spinner
animation: spin 1s linear infinite
border-radius: 999px
border: 2px solid #007bff
border-top-color: transparent
content: ''
height: 75px
margin-bottom: 15px
width: 75px
&-text
font-weight: 300
text-trnasform: uppercase
&-control
.custom-select
width: initial
.table
&-responsive
margin-bottom: 30px
&::-webkit-scrollbar
-webkit-apperance: none
height: 15px
width: 15px
&-track
background: #eee
border-radius: 999px
&-thumb
background: #ccc
border-radius: 999px
border: 3px solid #eee
&:focus
background: #ccc
&.straight
white-space: nowrap
thead
th
font-size: 12px
font-weight: 500
&.sortable
cursor: pointer
padding-right: 30px
position: relative
&:before,
&:after
border: 5px solid transparent
content: ''
display: block
opacity: .3
position: absolute
right: 10px
&:before
border-bottom-color: currentColor
top: 10px
&:after
bottom: 10px
border-top-color: currentColor
&.sort
font-weight: 700
&.asc
&:before
opacity: 1
&.desc
&:after
opacity: 1
tbody
tr
&.clickable
cursor: pointer
td
// font-size: 12px
&-filters
margin-bottom: 15px
&-filter
background: #fff
border-radius: 3px
cursor: pointer
color: #777
display: inline-block
font-size: 12px
padding: 5px 15px
margin: 0 0 3px 3px
&:hover
background: #aaa
color: #fff
&.active
background: #337ab7
color: #fff
</style>