forked from frappe/datatable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
294 lines (264 loc) · 12.3 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Frappe DataTable</title>
<style>
body {
font-family: 'Tahoma';
font-weight: normal;
font-size: 12px;
}
</style>
<link rel="stylesheet" href="./dist/frappe-datatable.css" />
</head>
<body>
<h1>Frappe DataTable</h1>
<button onclick="datatable.render()">Render Table</button>
<button onclick="datatable.refresh()">Refresh Data</button>
<button onclick="switchToTreeView()" data-action="treeview">TreeView</button>
<button onclick="appendRows()">Append Rows</button>
<label>
<input type="checkbox" id="input-large-data" />
<span>Large Data</span>
</label>
<label>
<input type="checkbox" id="dark-theme" />
<span>Dark Theme</span>
</label>
<section style="width: 60%; margin: 0 auto;">
</section>
<section id="datatable2" style="width: 60%; ">
</section>
<script src="./node_modules/sortablejs/Sortable.js"></script>
<script src="./dist/frappe-datatable.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
let data = [];
let columns = [];
let largeData = false;
document.querySelector('#input-large-data').addEventListener('change', (e) => {
const enabled = e.target.value === 'on';
largeData = enabled;
buildData();
makeDatatable();
});
document.querySelector('#dark-theme').addEventListener('change', (e) => {
const enabled = document.querySelector('#dark-theme:checked');
if (enabled) {
const link = document.createElement('link');
link.href = 'src/dark.css';
link.rel = 'stylesheet'
link.id = 'dark-theme-stylesheet'
document.head.appendChild(link);
link.onload = () => datatable.setDimensions()
} else {
document.getElementById('dark-theme-stylesheet').remove();
setTimeout(() => datatable.setDimensions())
}
})
function buildTreeData() {
columns = [
{ name: 'Files', width: 300 },
{ name: 'Size', width: 150, align: 'right' },
{ name: 'Last Updated', width: 200, align: 'right' },
]
data = [
{
'Files': 'All Folders',
'Size': '2M',
'Last Updated': '',
'indent': -1
},
{
'Files': 'Documents',
'Size': '2M',
'Last Updated': '',
'indent': 0
},
{
'Files': 'project.pdf',
'Size': '1M',
'Last Updated': 'Yesterday',
'indent': 1
},
{
'Files': 'my-face.png',
'Size': '500k',
'Last Updated': '2018-04-09',
'indent': 1
},
{
'Files': 'Projects',
'Size': '77M',
'Last Updated': '',
'indent': 0
},
{
'Files': 'frappe-gantt',
'Size': '23M',
'Last Updated': '',
'indent': 1
},
{
'Files': 'dist',
'Size': '50k',
'Last Updated': '2018-06-01',
'indent': 2
},
{
'Files': 'package.json',
'Size': '5k',
'Last Updated': '2018-06-01',
'indent': 2
},
{
'Files': 'frappe-datatable',
'Size': '54M',
'Last Updated': '',
'indent': 1
},
{
'Files': 'src',
'Size': '53k',
'Last Updated': 'A few seconds ago',
'indent': 2
},
{
'Files': 'dist',
'Size': '21k',
'Last Updated': 'A few seconds ago',
'indent': 2
},
]
data.map(d => d.indent++)
}
function buildData() {
columns = [
{ name: "Name" },
{ name: "Position" },
{ name: "Office" },
{ name: "Extn." },
{
name: "Start Date",
format: (value) => (value || '').split('/').reverse().join('/'),
compareValue: (cell, keyword) => {
const keywordValue = keyword.split('/').reverse().join('/')
return [+new Date(cell.content), +new Date(keywordValue)];
}
},
{ name: "Salary", format: value => formatMoney(value) }
];
data = [["Tiger Nixon", { content: "System Architect<br>New line", editable: false }, "Edinburgh", 5421, "2011/04/25", 320800], ["Garrett Winters", "Accountant", "Tokyo", 8422, "2011/07/25", 170750], ["Ashton Cox", "Junior Technical Author", "San Francisco", 1562, "2009/01/12", 86000], ["Cedric Kelly", "Senior Javascript Developer", "Edinburgh", 6224, "2012/03/29", 433060], ["Airi Satou", "Accountant", "Tokyo", 5407, "2008/11/28", 162700], ["Brielle Williamson", "Integration Specialist", "New York", 4804, "2012/12/02", 372000], ["Herrod Chandler", "Sales Assistant", "San Francisco", 9608, "2012/08/06", 137500], ["Rhona Davidson", "Integration Specialist", "Tokyo", 6200, "2010/10/14", 327900], ["Colleen Hurst", "Javascript Developer", "San Francisco", 2360, "2009/09/15", 205500], ["Sonya Frost", "Software Engineer", "Edinburgh", 1667, "2008/12/13", 103600], ["Jena Gaines", "Office Manager", "London", 3814, "2008/12/19", 90560], ["Quinn Flynn", "Support Lead", "Edinburgh", 9497, "2013/03/03", 342000], ["Charde Marshall", "Regional Director", "San Francisco", 6741, "2008/10/16", 470600], ["Haley Kennedy", "Senior Marketing Designer", "London", 3597, "2012/12/18", 313500], ["Tatyana Fitzpatrick", "Regional Director", "London", 1965, "2010/03/17", 385750], ["Michael Silva", "Marketing Designer", "London", 1581, "2012/11/27", 198500], ["Paul Byrd", "Chief Financial Officer (CFO)", "New York", 3059, "2010/06/09", 725000], ["Gloria Little", "Systems Administrator", "New York", 1721, "2009/04/10", 237500], ["Bradley Greer", "Software Engineer", "London", 2558, "2012/10/13", 132000], ["Dai Rios", "Personnel Lead", "Edinburgh", 2290, "2012/09/26", 217500], ["Jenette Caldwell", "Development Lead", "New York", 1937, "2011/09/03", 345000], ["Yuri Berry", "Chief Marketing Officer (CMO)", "New York", 6154, "2009/06/25", 675000], ["Caesar Vance", "Pre-Sales Support", "New York", 8330, "2011/12/12", 106450], ["Doris Wilder", "Sales Assistant", "Sidney", 3023, "2010/09/20", 85600], ["Angelica Ramos", "Chief Executive Officer (CEO)", "London", 5797, "2009/10/09", 1200000], ["Gavin Joyce", "Developer", "Edinburgh", 8822, "2010/12/22", 92575], ["Jennifer Chang", "Regional Director", "Singapore", 9239, "2010/11/14", 357650], ["Brenden Wagner", "Software Engineer", "San Francisco", 1314, "2011/06/07", 206850], ["Fiona Green", "Chief Operating Officer (COO)", "San Francisco", 2947, "2010/03/11", 850000], ["Shou Itou", "Regional Marketing", "Tokyo", 8899, "2011/08/14", 163000], ["Michelle House", "Integration Specialist", "Sidney", 2769, "2011/06/02", 95400], ["Suki Burks", "Developer", "London", 6832, "2009/10/22", 114500], ["Prescott Bartlett", "Technical Author", "London", 3606, "2011/05/07", 145000], ["Gavin Cortez", "Team Leader", "San Francisco", 2860, "2008/10/26", 235500], ["Martena Mccray", "Post-Sales support", "Edinburgh", 8240, "2011/03/09", 324050], ["Unity Butler", "Marketing Designer", "San Francisco", 5384, "2009/12/09", 85675], ["Howard Hatfield", "Office Manager", "San Francisco", 7031, "2008/12/16", 164500], ["Hope Fuentes", "Secretary", "San Francisco", 6318, "2010/02/12", 109850], ["Vivian Harrell", "Financial Controller", "San Francisco", 9422, "2009/02/14", 452500], ["Timothy Mooney", "Office Manager", "London", 7580, "2008/12/11", 136200], ["Jackson Bradshaw", "Director", "New York", 1042, "2008/09/26", 645750], ["Olivia Liang", "Support Engineer", "Singapore", 2120, "2011/02/03", 234500], ["Bruno Nash", "Software Engineer", "London", 6222, "2011/05/03", 163500], ["Sakura Yamamoto", "Support Engineer", "Tokyo", 9383, "2009/08/19", 139575], ["Thor Walton", "Developer", "New York", 8327, "2013/08/11", 98540], ["Finn Camacho", "Support Engineer", "San Francisco", 2927, "2009/07/07", 87500], ["Serge Baldwin", "Data Coordinator", "Singapore", 8352, "2012/04/09", 138575], ["Zenaida Frank", "Software Engineer", "New York", 7439, "2010/01/04", 125250], ["Zorita Serrano", "Software Engineer", "San Francisco", 4389, "2012/06/01", 115000], ["Jennifer Acosta", "Junior Javascript Developer", "Edinburgh", 3431, "2013/02/01", 75650], ["Cara Stevens", "Sales Assistant", "New York", 3990, "2011/12/06", 145600], ["Hermione Butler", "Regional Director", "London", 1016, "2011/03/21", 356250], ["Lael Greer", "Systems Administrator", "London", 6733, "2009/02/27", 103500], ["Jonas Alexander", "Developer", "San Francisco", 8196, "2010/07/14", 86500], ["Shad Decker", "Regional Director", "Edinburgh", 6373, "2008/11/13", 183000], ["Michael Bruce", "Javascript Developer", "Singapore", 5384, "2011/06/27", 183000], ["Donna Snider", "Customer Support", "New York", 4226, "2011/01/25", 112000], ["Donna Snider", "Customer Support", "LOS Angeles", 42.67, "2011/01/25", 112000]];
if (largeData) {
for (let i = 0; i < 10; i++) {
data = data.concat(data);
}
}
// data = data.slice(1, 3)
}
function makeDatatable(treeView = false) {
console.log('No of Rows:', data.length)
const start = performance.now();
var datatable = new DataTable('section', {
checkboxColumn: true,
serialNoColumn: true,
layout: 'fluid',
columns,
data,
inlineFilters: true,
dynamicRowHeight: true,
treeView: treeView,
showTotalRow: true,
// direction: 'rtl',
// language: 'myLang',
// translations: {
// myLang: {
// "Sort Ascending": "Sort low to high",
// "{count} cells copied": {
// "1": "1 cell was copied",
// "2": "2 cells were copied",
// "default": "Many cells were copied"
// }
// }
// },
// filterRows(keyword, cells, colIndex) {
// return cells
// .filter(cell => cell.content.includes(keyword))
// .map(cell => cell.rowIndex);
// },
getEditor(colIndex, rowIndex, value, parent) {
// editing obj only for date field
if (colIndex != 6) return;
const $input = document.createElement('input');
$input.type = 'date';
parent.appendChild($input);
const parse = value => value.replace(/\//g, '-');
const format = value => value.replace(/\-/g, '/');
return {
initValue(value) {
$input.focus();
$input.value = parse(value);
},
setValue(value) {
$input.value = parse(value);
},
getValue() {
return format($input.value);
}
}
},
hooks: {
columnTotal(columnValues, cell) {
if (cell.colIndex === 5) {
// calculated average for 5th column
const sum = columnValues.reduce((acc, value) => {
if (typeof value === 'number') {
return acc + value
}
return acc
}, 0);
return sum / columnValues.length
}
if (cell.colIndex === 2) {
return 'Total'
}
}
}
});
console.log(performance.now() - start);
window.datatable = datatable;
}
window.switchToTreeView = function () {
datatable.destroy();
buildTreeData();
makeDatatable(true);
}
buildData();
makeDatatable();
})
function formatMoney(amount, decimalCount = 2, decimal = ".", thousands = ",") {
try {
decimalCount = Math.abs(decimalCount);
decimalCount = isNaN(decimalCount) ? 2 : decimalCount;
const negativeSign = amount < 0 ? "-" : "";
let i = parseInt(amount = Math.abs(Number(amount) || 0).toFixed(decimalCount)).toString();
let j = (i.length > 3) ? i.length % 3 : 0;
return negativeSign + (j ? i.substr(0, j) + thousands : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands) + (decimalCount ? decimal + Math.abs(amount - i).toFixed(decimalCount).slice(2) : "");
} catch (e) {
console.log(e)
}
};
function appendRows() {
datatable.appendRows(
[
["Garrett", "Accountant", "Tokyo", 8422, "2011/07/25", 170],
["Winters", "Accountant", "Tokyo", 8422, "2011/07/25", 123]
]
)
}
</script>
</body>
</html>