-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
170 lines (156 loc) · 4.91 KB
/
index.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
$(() => {
const formatDate = new Intl.DateTimeFormat('en-US').format;
let changes = [];
const dataGrid = $('#gridContainer')
.dxDataGrid({
dataSource: employees,
keyExpr: 'ID',
dataRowTemplate(container, item) {
if (item.isEditing) {
createEditRowTemplate(container, item);
} else {
createDataRowTemplate(container, item);
}
},
rowAlternationEnabled: true,
columnAutoWidth: true,
showBorders: true,
columns: [
{
dataField: 'Prefix',
caption: 'Title',
width: 70,
},
'FirstName',
'LastName',
'Position',
{
dataField: 'BirthDate',
dataType: 'date',
},
{
dataField: 'HireDate',
dataType: 'date',
},
{
width: 160,
},
],
onToolbarPreparing(e) {
e.toolbarOptions.items.unshift({
location: 'after',
widget: 'dxButton',
options: {
text: 'Add new item',
onClick() {
e.component.cancelEditData();
e.component.addRow();
changes = e.component.option('editing.changes');
},
},
});
},
onInitNewRow(e) {
e.data.Prefix = '';
e.data.FirstName = '';
e.data.LastName = '';
e.data.Position = '';
e.data.BirthDate = new Date('1986/07/08');
e.data.HireDate = new Date('1986/07/08');
e.data.Notes = '';
},
})
.dxDataGrid('instance');
const createEditRowTemplate = (container, item) => {
const data = item.data;
const markup = createEditMarkup(data);
container.append(markup);
editors.forEach((editor) => {
createEditor(editor.editorID, data[editor.dataField], editor.dataField, data.ID);
});
$('#notesEditor').dxTextArea({
value: data.Notes,
onValueChanged: onValueChanged('Notes', data.ID),
});
createButtons(container, item, 'Save', 'Cancel');
};
const createDataRowTemplate = (container, item) => {
const markup = createDataMarkup(item.data);
container.append(markup);
createButtons(container, item, 'Edit', 'Delete');
};
const createEditor = (editorID, value, dataField, dataID) => {
const isDateField = dataField === 'BirthDate' || dataField === 'HireDate';
const options = {
value: isDateField ? new Date(value) : value,
onValueChanged: onValueChanged(dataField, dataID),
};
return isDateField ? $(editorID).dxDateBox(options) : $(editorID).dxTextBox(options);
};
const onValueChanged = (dataField, key) => ({ value }) => {
if (!changes.length) {
changes.push({ data: { [dataField]: value }, key, type: 'update' });
} else {
changes[0].data = { ...changes[0].data, [dataField]: value };
}
};
const createButton = (text, item) => {
const $buttonContainer = $('<div>');
return $buttonContainer.dxButton({
text,
onClick() {
const { component } = item;
switch (text) {
case 'Save':
component.option('editing.changes', changes);
component.saveEditData();
component.refresh();
break;
case 'Cancel':
component.cancelEditData();
component.refresh();
break;
case 'Edit':
component.cancelEditData();
component.editRow(item.rowIndex);
changes = component.option('editing.changes');
break;
case 'Delete':
component.deleteRow(item.rowIndex);
break;
default:
break;
}
},
});
};
const createButtons = (container, item, button1, button2) => {
const $button1 = createButton(button1, item);
const $button2 = createButton(button2, item);
$(container).find(`#editColumn-${item.data.ID}`).append($button1, $button2);
};
const createEditMarkup = (data) => "<tr class='main-row'>"
+ "<td><div id='prefixEditor'></div></td>"
+ "<td><div id='firstNameEditor'></div></td>"
+ "<td><div id='lastNameEditor'></div></td>"
+ "<td><div id='positionEditor'></td>"
+ "<td><div id='birthDateEditor'></div></td>"
+ "<td><div id='hireDateEditor'></div></td>"
+ `<td rowspan='2' id='editColumn-${data.ID}'></td>`
+ '</tr>'
+ "<tr class='notes-row'>"
+ "<td colspan='6'><div id='notesEditor'></div></td>"
+ '</tr>';
const createDataMarkup = (data) => "<tr class='main-row'>"
+ `<td>${data.Prefix}</td>`
+ `<td>${data.FirstName}</td>`
+ `<td>${data.LastName}</td>`
+ `<td>${data.Position}</td>`
+ `<td>${formatDate(new Date(data.BirthDate))}</td>`
+ `<td>${formatDate(new Date(data.HireDate))}</td>`
+ `<td rowspan='2' id='editColumn-${data.ID}'></td>`
+ '</tr>'
+ "<tr class='notes-row'>"
+ `<td colspan='6'><div>${data.Notes}</div></td>`
+ '</tr>';
});