-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample7.js
More file actions
171 lines (168 loc) · 4.52 KB
/
Copy pathexample7.js
File metadata and controls
171 lines (168 loc) · 4.52 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var dataUrl = './';
var countryOptions = ['Italy', 'France', 'Germany'];
var allTownsOptions = [
'Torino',
'Roma',
'Milano',
'Napoli',
'Paris',
'Lyon',
'Toulouse',
];
$(document).ready(function () {
var columnDefs = [
{
data: 'id',
title: 'Id',
type: 'readonly',
},
{
data: 'name',
title: 'Name',
},
{
data: 'country',
title: 'Country',
type: 'select',
options: countryOptions,
value: 'Italy',
select2: { width: '100%' },
editorOnChange: function (event, editor) {
const country = event.currentTarget.value;
const modal = $(event.target).closest('.altEditor-modal');
const town = modal.find('[name="town"]');
let message = modal.find('#town-label');
if (!message.length) {
message = $('<div>', { id: 'town-label', role: 'status' }).appendTo(
town.parent()
);
town.attr('aria-describedby', 'town-label');
}
const previousRequest = town.data('options-request');
if (previousRequest) previousRequest.abort();
message.text('');
const hasTowns = country === 'Italy' || country === 'France';
modal
.find('[name="town"]')
.closest('.altEditor-field')
.toggle(hasTowns);
town.prop('required', hasTowns);
if (!hasTowns) {
editor.reloadOptions(town, ['']);
town.val('').trigger('change');
town[0].setCustomValidity('');
return;
}
town[0].setCustomValidity('Wait for the towns to load.');
const request = $.ajax({
url:
dataUrl +
(country === 'Italy'
? 'mock_svc_italy.json'
: 'mock_svc_france.json'),
dataType: 'json',
success: function (options) {
if (!town[0].isConnected || event.currentTarget.value !== country)
return;
editor.reloadOptions(town, options);
if (town.val() === null) town.val(options[0]).trigger('change');
town[0].setCustomValidity('');
},
error: function (_request, status) {
if (status === 'abort' || !town[0].isConnected) return;
const text =
'Unable to load towns. Select the country again to retry.';
town[0].setCustomValidity(text);
message.text(text);
},
});
town.data('options-request', request);
},
},
{
data: 'town',
title: 'Town',
type: 'select',
options: allTownsOptions,
select2: { width: '100%' },
},
];
var myTable;
myTable = $('#example').DataTable({
rowId: 'id',
pagingType: 'full_numbers',
ajax: {
url: dataUrl + 'mock_svc_load.json',
// The response is a JSON array rather than an object with a data property.
dataSrc: '',
},
columns: columnDefs,
layout: { topStart: 'buttons' },
select: 'single',
responsive: true,
altEditor: true, // Enable altEditor
buttons: [
{
text: 'Add',
name: 'add', // do not change name
},
{
extend: 'selected', // Bind to Selected row
text: 'Edit',
name: 'edit', // do not change name
},
{
extend: 'selected', // Bind to Selected row
text: 'Delete',
name: 'delete', // do not change name
},
{
text: 'Refresh',
name: 'refresh', // do not change name
},
],
onAddRow: function (editor, values, success, error) {
// Static JSON simulates persistence; production code must use a write endpoint.
values.id =
Math.max(
0,
...editor
.api()
.rows()
.data()
.toArray()
.map(function (row) {
return Number(row.id) || 0;
})
) + 1;
$.ajax({
url: dataUrl + 'mock_svc_ok.json',
type: 'GET',
success: function () {
success();
},
error: error,
});
},
onDeleteRow: function (editor, values, success, error) {
$.ajax({
url: dataUrl + 'mock_svc_ok.json',
type: 'GET',
success: function () {
success();
},
error: error,
});
},
onEditRow: function (editor, values, success, error) {
$.ajax({
url: dataUrl + 'mock_svc_ok.json',
type: 'GET',
success: function () {
success();
},
error: error,
});
},
});
});