-
Notifications
You must be signed in to change notification settings - Fork 13
/
table.js
207 lines (194 loc) · 8.71 KB
/
table.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
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
/* global jQuery3, luxon, tableDataProxy, bootstrap5 */
jQuery3(document).ready(function () {
/**
* Binds all tables that have the class 'data-table' to a new JQuery DataTables instance.
*/
function bindTables($) {
/**
* Creates the data table instance for the specified table element.
*/
function createDataTable(table) {
const defaultConfiguration = {
stateSave: table.attr('data-disable-state-save') !== 'true',
language: {
emptyTable: 'Loading - please wait ...'
},
responsive: {
details: false
},
deferRender: true,
pagingType: 'numbers', // page number button only
order: [[1, 'asc']], // default order, if not persisted yet
columnDefs: [
{
targets: 'nosort', // All columns with the '.nosort' class in the <th>
orderable: false
},
{
targets: 'text-end', // All columns with the '.text-end' class in the <th>
className: 'text-end'
},
{
targets: 'date', // All columns with the '.date' class in the <th>
render: function (data, type, _row, _meta) {
if (type === 'display') {
if (data === 0) {
return '-';
}
const dateTime = luxon.DateTime.fromMillis(data * 1000);
return '<span data-bs-toggle="tooltip" data-bs-placement="top" title="'
+ dateTime.toLocaleString(luxon.DateTime.DATETIME_SHORT) + '">'
+ dateTime.toRelative({locale: 'en'}) + '</span>';
}
else {
return data;
}
}
},
{
targets: 'percentage', // All columns with the '.percentage' class in the <th>
className: 'text-end',
render: function (data, type, _row, _meta) {
if (isNaN(data)) {
return data;
}
return Number(data).toLocaleString(undefined,
{style: 'percent', minimumFractionDigits: 2});
}
},
{
targets: 'hidden', // All columns with the '.hidden' class in the <th>
visible: false,
searchable: true,
orderable: false // There is no point in allowing sort by this column if it's not visible.
}
],
columns: JSON.parse(table.attr('data-columns-definition'))
};
const tableConfiguration = JSON.parse(table.attr('data-table-configuration'));
// overwrite/merge the default configuration with values from the provided table configuration
const dataTable = table.DataTable(Object.assign(defaultConfiguration, tableConfiguration));
// add the buttons to the top of the table
if (tableConfiguration.buttons) {
dataTable
.buttons()
.container()
.addClass('float-none mb-3')
.prependTo($(dataTable.table().container()).closest('.table-responsive'));
}
return dataTable;
}
/**
* Loads the content for the specified table element via an Ajax call.
*/
function loadTableData(table, dataTable) {
if (!table[0].hasAttribute('isLoaded')) {
table.attr('isLoaded', 'false');
tableDataProxy.getTableRows(table.attr('id'), function (t) {
(function () {
const model = JSON.parse(t.responseObject());
dataTable.rows.add(model).draw();
dataTable.columns.adjust().draw();
table.attr('isLoaded', 'true');
table.emptyTable = 'No records found';
table.find('.details-icon-close').each(function () {
$(this).hide();
});
})();
});
}
}
const allTables = $('table.data-table');
allTables.each(function () {
const table = $(this);
const id = table.attr('id');
const dataTable = createDataTable(table);
// Add event listener for opening and closing details
table.on('click', 'div.details-control', function () {
const tr = $(this).parents('tr');
const row = dataTable.row(tr);
const openRowButton = tr.find('.details-icon-open');
const closeRowButton = tr.find('.details-icon-close');
if (row.child.isShown()) {
row.child.hide();
tr.removeClass('shown');
openRowButton.show();
closeRowButton.hide();
}
else {
row.child($(this).data('description')).show();
tr.addClass('shown');
openRowButton.hide();
closeRowButton.show();
}
});
table.on('draw.dt', function () {
table.find('[data-bs-toggle="tooltip"]').each(function () {
const tooltip = new bootstrap5.Tooltip($(this)[0]);
tooltip.enable();
});
});
if (table.is(":visible")) {
loadTableData(table, dataTable);
}
else {
table.on('becameVisible', function () {
loadTableData(table, dataTable);
});
}
if (!table.hasClass("custom-persistence-off")) {
// Add event listener that stores the order a user selects
table.on('order.dt', function () {
const order = table.DataTable().order();
localStorage.setItem(id + '#orderBy', order[0][0]);
localStorage.setItem(id + '#orderDirection', order[0][1]);
});
/**
* Restores the order of every table by reading the local storage of the browser.
* If no order has been stored yet, the table is skipped.
* Also saves the default length of the number of table columns.
*/
const orderBy = localStorage.getItem(id + '#orderBy');
const orderDirection = localStorage.getItem(id + '#orderDirection');
if (orderBy && orderDirection) {
const order = [orderBy, orderDirection];
try {
dataTable.order(order).draw();
}
catch (ignore) { // TODO: find a way to determine the number of columns here
dataTable.order([[1, 'asc']]).draw();
}
}
// Store paging size
table.on('length.dt', function (e, settings, len) {
localStorage.setItem(id + '#table-length', len);
});
const storedLength = localStorage.getItem(id + '#table-length');
if ($.isNumeric(storedLength)) {
dataTable.page.len(storedLength).draw();
}
// Store search text
if (table.attr('data-remember-search-text') === 'true') {
table.on('search.dt', function () {
localStorage.setItem(id + '#table-search-text', dataTable.search());
});
const storedSearchText = localStorage.getItem(id + '#table-search-text');
if (storedSearchText) {
dataTable.search(storedSearchText).draw();
}
}
}
});
}
(function ($) {
$.extend(true, $.fn.dataTable.defaults, {
mark: {
className: 'highlight'
},
responsive: {
details: false
}
});
bindTables($);
})(jQuery3);
});