-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathdata-table-class.js
More file actions
156 lines (133 loc) · 5.07 KB
/
data-table-class.js
File metadata and controls
156 lines (133 loc) · 5.07 KB
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
import $ from '../../shared/dom7.js';
import { extend, deleteProps } from '../../shared/utils.js';
import Framework7Class from '../../shared/class.js';
class DataTable extends Framework7Class {
constructor(app, params = {}) {
// El
const $el = $(params.el);
// Because temporarily does not support virtual lists, so if the element does not exist, we can return directly.
if ($el[0].f7DataTable) return $el[0].f7DataTable;
super(params, [app]);
const table = this;
const defaults = {};
// Extend defaults with modules params
table.useModulesParams(defaults);
table.params = extend(defaults, params);
if ($el.length === 0) return undefined;
table.$el = $el;
table.el = $el[0];
table.$el[0].f7DataTable = table;
extend(table, {
collapsible: $el.hasClass('data-table-collapsible'),
// Headers
$headerEl: $el.find('.data-table-header'),
$headerSelectedEl: $el.find('.data-table-header-selected'),
});
// Events
function handleChange(e) {
if (e.detail && e.detail.sentByF7DataTable) {
// Scripted event, don't do anything
return;
}
const $inputEl = $(this);
const checked = $inputEl[0].checked;
const columnIndex = $inputEl.parents('td,th').index();
if ($inputEl.parents('thead').length > 0) {
if (columnIndex === 0) {
$el.find('tbody tr')[checked ? 'addClass' : 'removeClass']('data-table-row-selected');
}
$el
.find(`tbody tr td:nth-child(${columnIndex + 1}) input`)
.prop('checked', checked)
.trigger('change', { sentByF7DataTable: true });
$inputEl.prop('indeterminate', false);
} else {
if (columnIndex === 0) {
$inputEl.parents('tr')[checked ? 'addClass' : 'removeClass']('data-table-row-selected');
}
const checkedRows = $el.find(
`tbody .checkbox-cell:nth-child(${columnIndex + 1}) input[type="checkbox"]:checked`,
).length;
const totalRows = $el.find('tbody tr').length;
const $headCheckboxEl = $el.find(
`thead .checkbox-cell:nth-child(${columnIndex + 1}) input[type="checkbox"]`,
);
if (!checked) {
$headCheckboxEl.prop('checked', false);
} else if (checkedRows === totalRows) {
$headCheckboxEl.prop('checked', true).trigger('change', { sentByF7DataTable: true });
}
$headCheckboxEl.prop('indeterminate', checkedRows > 0 && checkedRows < totalRows);
}
table.checkSelectedHeader();
}
function handleSortableClick() {
const $cellEl = $(this);
const isActive = $cellEl.hasClass('sortable-cell-active');
const currentSort = $cellEl.hasClass('sortable-desc') ? 'desc' : 'asc';
let newSort;
if (isActive) {
newSort = currentSort === 'desc' ? 'asc' : 'desc';
$cellEl.removeClass('sortable-desc sortable-asc').addClass(`sortable-${newSort}`);
} else {
$el.find('thead .sortable-cell-active').removeClass('sortable-cell-active');
$cellEl.addClass('sortable-cell-active');
newSort = currentSort;
}
$cellEl.trigger('datatable:sort', newSort);
table.emit('local::sort dataTableSort', table, newSort);
}
table.attachEvents = function attachEvents() {
table.$el.on('change', '.checkbox-cell input[type="checkbox"]', handleChange);
table.$el.find('thead .sortable-cell').on('click', handleSortableClick);
};
table.detachEvents = function detachEvents() {
table.$el.off('change', '.checkbox-cell input[type="checkbox"]', handleChange);
table.$el.find('thead .sortable-cell').off('click', handleSortableClick);
};
// Install Modules
table.useModules();
// Init
table.init();
return table;
}
setCollapsibleLabels() {
const table = this;
if (!table.collapsible) return;
table.$el.find('tbody td:not(.checkbox-cell)').each((el) => {
const $el = $(el);
const elIndex = $el.index();
const collapsibleTitle = $el.attr('data-collapsible-title');
if (!collapsibleTitle && collapsibleTitle !== '') {
$el.attr('data-collapsible-title', table.$el.find('thead th').eq(elIndex).text());
}
});
}
checkSelectedHeader() {
const table = this;
if (table.$headerEl.length > 0 && table.$headerSelectedEl.length > 0) {
const checkedItems = table.$el.find('tbody .checkbox-cell input:checked').length;
table.$el[checkedItems > 0 ? 'addClass' : 'removeClass']('data-table-has-checked');
table.$headerSelectedEl.find('.data-table-selected-count').text(checkedItems);
}
}
init() {
const table = this;
table.attachEvents();
table.setCollapsibleLabels();
table.checkSelectedHeader();
}
destroy() {
let table = this;
table.$el.trigger('datatable:beforedestroy');
table.emit('local::beforeDestroy dataTableBeforeDestroy', table);
table.detachEvents();
if (table.$el[0]) {
table.$el[0].f7DataTable = null;
delete table.$el[0].f7DataTable;
}
deleteProps(table);
table = null;
}
}
export default DataTable;