-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactive-datatables.js
72 lines (64 loc) · 2.31 KB
/
reactive-datatables.js
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
ReactiveDatatable = function(options) {
var self = this;
this.options = options = _.defaults(options, {
// Any of these can be overriden by passing an options
// object into your ReactiveDatatable template (see readme)
rowId: '_id',
stateSave: true,
stateDuration: -1, // Store data for session only
pageLength: 10,
lengthMenu: [3, 5, 10, 50, 100],
columnDefs: [{ // Global default blank value to avoid popup on missing data
targets: '_all',
defaultContent: '–––'
}],
stateLoadParams: function(settings, data) {
// Make it easy to change to the stored page on .update()
self.page = data.start / data.length;
}
});
};
ReactiveDatatable.prototype.update = function(data, settings) {
if (!data) return;
var self = this;
var existingData = self.datatable.rows().data().toArray();
var dataDifference = self.difference(existingData, data);
if (data.length > existingData.length) {
/**
* ADD ROW
*/
// console.log("//////////// ADD //////////////");
dataDifference.forEach(function(dataItem) {
var index = self.datatable.row('#' + dataItem._id);
if (index.length > 0) {
self.datatable.row(index[0]).data(dataItem).invalidate();
} else {
self.datatable.row.add(dataItem);
}
});
} else if (data.length < existingData.length) {
/**
* REMOVE ROW
*/
// console.log("//////////// REMOVE //////////////");
dataDifference.forEach(function(dataItem) {
self.datatable.row('#' + dataItem._id).remove();
});
} else if (existingData.length > 0 && (existingData.length === data.length)) {
/**
* UPDATE
*/
// console.log("//////////// UPDATE //////////////");
data.forEach(function(dataItem) {
self.datatable.row('#' + dataItem._id).data(dataItem).invalidate();
});
} else if (!existingData.length) {
/**
* INITIALIZE
*/
// console.log("//////////// INIT //////////////");
// Data initializing or we have the same data
self.datatable.rows.add(data);
}
self.datatable.draw(false);
};