-
Notifications
You must be signed in to change notification settings - Fork 12
/
dataTables.treeTable.js
96 lines (87 loc) · 2.9 KB
/
dataTables.treeTable.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
(function ($) {
"use strict";
// Private APIs
var expandedNodes = {},
getInitOpts = function (oDTSettings) {
return oDTSettings.oInit.oTreeTable || {};
},
initTreeTable = function (dataTable, options) {
dataTable.treeTable(options).addClass('initialized-treeTable');
// remove expander
if (options !== null && options.showExpander === false) {
dataTable.$('td a.expander').remove();
}
};
// Public APIs as extension of jquery.dataTables
/**
* Returns whether the current dataTable is also a treeTable or not.
*/
$.fn.dataTableExt.oApi.isTreeTable = function (oSettings) {
return $('#' + oSettings.sTableId).hasClass('treeTable');
};
function isTreeTableInitialized(oSettings) {
return $('#' + oSettings.sTableId).hasClass('treeTable initialized-treeTable');
}
/**
* Explicitly trigger the initialisation of treeTable.
*/
$.fn.dataTableExt.oApi.fnInitTreeTable = function (oSettings, options) {
initTreeTable(oSettings.oInstance, options || {});
};
/**
* Save expanded nodes (TRs) before reloading data table.
* And after reloading, call restoreExpanded to restore the expand state.
*/
function saveExpanded (oSettings) {
var oTable = oSettings.oInstance, expandedRows;
if (!oTable.isTreeTable()) {
return;
}
// save expanded row ids
expandedRows = [];
oTable.$('tr.parent.expanded').each(function () {
expandedRows.push($(this).attr('id'));
});
expandedNodes[oSettings.sTableId] = expandedRows;
}
/**
* Expand nodes which are saved before reloading data table to restore the expand state.
*/
function restoreExpanded(oSettings) {
if (!isTreeTableInitialized(oSettings)) {
setTimeout(function () {
restoreExpanded(oSettings);
}, 200);
return;
}
var i, iLen, oTable = oSettings.oInstance, tableId = oSettings.sTableId,
expandedRows = expandedNodes[tableId] || [];
for (i = 0, iLen = expandedRows.length; i < iLen; i++) {
oTable.$('tr#' + expandedRows[i]).expand();
}
expandedNodes[tableId] = null;
}
$.fn.dataTableExt.oApi.fnReloadAjaxTreeTable = function (oSettings, sNewSource, fnCallback, bStandingRedraw) {
var oTable = oSettings.oInstance;
saveExpanded(oSettings);
oTable.removeClass('initialized-treeTable');
oTable.fnReloadAjax(sNewSource, fnCallback, bStandingRedraw);
restoreExpanded(oSettings);
};
// Register a plugin to integrate jquery.treeTable
$.fn.dataTableExt.aoFeatures.push({
"fnInit": function (oDTSettings) {
var oTable = oDTSettings.oInstance,
opts = getInitOpts(oDTSettings);
if (typeof opts.fnPreInit === 'function') {
oTable.oApi._fnCallbackReg(oDTSettings, "aoRowCreatedCallback", opts.fnPreInit, "preInitTreeTable");
}
oTable.oApi._fnCallbackReg(oDTSettings, "aoDrawCallback", function () {
oTable.fnInitTreeTable(opts);
}, "initTreeTable");
return null; /* No node to insert */
},
"cFeature": "T",
"sFeature": "Integrate jquery.treeTable"
});
}(jQuery));