-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample17.js
More file actions
61 lines (59 loc) · 1.85 KB
/
Copy pathexample17.js
File metadata and controls
61 lines (59 loc) · 1.85 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
const table = new DataTable('#example', {
data: [
{ id: 'a', person: { name: 'Alice' }, budget: 100, enabled: true },
{ id: 'b', person: { name: 'Bob' }, budget: 200, enabled: false },
],
rowId: 'id',
columns: [
{
data: (row) => row.person.name,
title: 'Name',
render: DataTable.render.text(),
required: true,
inlineEditSetValue(row, value) {
row.person.name = value;
return row;
},
},
{
data: 'budget',
title: 'Budget',
render: DataTable.render.number(',', '.', 2, '$'),
inlineEditType: 'number',
inlineEditOptions: { min: 0, max: 10000, step: 0.01, required: true },
},
{
data: 'enabled',
title: 'Enabled',
inlineEditType: 'checkbox',
render: (value, type) =>
type === 'display' ? (value ? 'Yes' : 'No') : value,
},
],
altEditor: { inlineEdit: { enabled: true, submitOnBlur: true } },
});
const editor = table.altEditor();
document.querySelector('#start').addEventListener('click', () => {
table.search('').order([]).page('first').draw(false);
editor.startInlineEdit({ row: table.row('#a').index(), column: 0 });
});
// Keep the active input focused until the explicit save or cancel action runs.
for (const id of ['save', 'cancel']) {
document
.querySelector('#' + id)
.addEventListener('pointerdown', (event) => event.preventDefault());
}
document
.querySelector('#save')
.addEventListener('click', () => editor.commitInlineEdit());
document
.querySelector('#cancel')
.addEventListener('click', () => editor.cancelInlineEdit());
$(table.table().node()).on(
'alteditor-inline-open.dt alteditor-inline-close.dt',
() => {
document.querySelector('#result').textContent = editor.isInlineEditing()
? 'Editing. Leaving the input saves it; use Cancel cell to discard it.'
: 'No active cell.';
}
);