-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathgrid-additem.component.ts
297 lines (271 loc) · 9.47 KB
/
grid-additem.component.ts
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
295
296
297
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import {
AngularGridInstance,
Column,
Editors,
FieldType,
Formatters,
GridOption,
GridService,
OnEventArgs,
SlickDataView,
SlickGrid,
} from './../modules/angular-slickgrid';
@Component({
styles: ['.duration-bg { background-color: #e9d4f1 !important }'],
encapsulation: ViewEncapsulation.None,
templateUrl: './grid-additem.component.html',
})
export class GridAddItemComponent implements OnInit {
title = 'Example 11: Add / Update / Highlight a Datagrid Item';
subTitle = `
Add / Update / Hightlight an Item from the Datagrid (<a href="https://ghiscoding.gitbook.io/angular-slickgrid/grid-functionalities/add-update-highlight" target="_blank">Wiki docs</a>).
<ul>
<li><b>Note:</b> this demo is <b>only</b> on the datagrid (client) side, you still need to deal with the backend yourself</li>
<li>Adding an item, will always be showing as the 1st item in the grid because that is the best visual place to add it</li>
<li>Add/Update an item requires a valid Slickgrid Selection Model, you have 2 choices to deal with this:</li>
<ul><li>You can enable "enableCheckboxSelector" or "enableRowSelection" to True</li></ul>
<li>Click on any of the buttons below to test this out</li>
<li>You can change the highlighted color & animation by changing the <a href="https://github.com/ghiscoding/slickgrid-universal/blob/master/packages/common/src/styles/_variables.scss" target="_blank">SASS Variables</a>:</li>
<ul>
<li>"$row-highlight-background-color" or "$row-highlight-fade-animation"</li>
</ul>
<li>You can also add CSS class(es) on the fly (or on page load) on rows with certain criteria, (e.g. click on last button)</li>
<ul>
<li>Example, click on button "Highlight Rows with Duration over 50" to see row styling changing. <a href="https://ghiscoding.gitbook.io/angular-slickgrid/grid-functionalities/dynamic-item-metadata" target="_blank">Wiki doc</a></li>
</ul>
</ul>
`;
angularGrid!: AngularGridInstance;
grid!: SlickGrid;
gridService!: GridService;
dataView!: SlickDataView;
columnDefinitions: Column[] = [];
gridOptions!: GridOption;
dataset: any[];
updatedObject: any;
constructor() {
// mock a dataset
this.dataset = this.mockDataset(1000);
}
angularGridReady(angularGrid: AngularGridInstance) {
this.angularGrid = angularGrid;
this.dataView = angularGrid.dataView;
this.grid = angularGrid.slickGrid as SlickGrid;
this.gridService = angularGrid.gridService;
// if you want to change background color of Duration over 50 right after page load,
// you would put the code here, also make sure to re-render the grid for the styling to be applied right away
/*
this.dataView.getItemMetadata = this.updateItemMetadataForDurationOver50(this.dataView.getItemMetadata);
this.grid.invalidate();
this.grid.render();
*/
}
ngOnInit(): void {
this.columnDefinitions = [
{
id: 'delete',
field: 'id',
excludeFromHeaderMenu: true,
formatter: Formatters.icon,
params: { iconCssClass: 'mdi mdi-trash-can pointer' },
minWidth: 30,
maxWidth: 30,
// use onCellClick OR grid.onClick.subscribe which you can see down below
onCellClick: (e: Event, args: OnEventArgs) => {
console.log(args);
if (confirm('Are you sure?')) {
this.angularGrid.gridService.deleteItemById(args.dataContext.id);
}
},
},
{
id: 'title',
name: 'Title',
field: 'title',
sortable: true,
type: FieldType.string,
editor: {
model: Editors.longText,
},
},
{
id: 'duration',
name: 'Duration (days)',
field: 'duration',
sortable: true,
type: FieldType.number,
editor: {
model: Editors.text,
},
onCellChange: (e: Event, args: OnEventArgs) => {
alert('onCellChange directly attached to the column definition');
console.log(args);
},
},
{
id: 'complete',
name: '% Complete',
field: 'percentComplete',
formatter: Formatters.percentCompleteBar,
type: FieldType.number,
editor: {
model: Editors.integer,
},
},
{
id: 'start',
name: 'Start',
field: 'start',
formatter: Formatters.dateIso,
sortable: true,
type: FieldType.date,
/*
editor: {
model: Editors.date
}
*/
},
{
id: 'finish',
name: 'Finish',
field: 'finish',
formatter: Formatters.dateIso,
sortable: true,
type: FieldType.date,
},
{
id: 'effort-driven',
name: 'Effort Driven',
field: 'effortDriven',
formatter: Formatters.checkmarkMaterial,
type: FieldType.number,
editor: {
model: Editors.checkbox,
},
},
];
this.gridOptions = {
asyncEditorLoading: false,
autoResize: {
container: '#demo-container',
rightPadding: 10,
},
editable: true,
enableColumnPicker: true,
enableCellNavigation: true,
enableRowSelection: true,
};
}
mockDataset(itemCount: number) {
// mock a dataset
const mockedDataset = [];
for (let i = 0; i < itemCount; i++) {
const randomYear = 2000 + Math.floor(Math.random() * 10);
const randomMonth = Math.floor(Math.random() * 11);
const randomDay = Math.floor(Math.random() * 29);
const randomPercent = Math.round(Math.random() * 100);
mockedDataset[i] = {
id: i,
title: 'Task ' + i,
duration: Math.round(Math.random() * 100) + '',
percentComplete: randomPercent,
percentCompleteNumber: randomPercent,
start: new Date(randomYear, randomMonth, randomDay),
finish: new Date(randomYear, randomMonth + 1, randomDay),
effortDriven: i % 5 === 0,
};
}
return mockedDataset;
}
addNewItem(insertPosition?: 'top' | 'bottom') {
const newItem1 = this.createNewItem(1);
// single insert
this.angularGrid.gridService.addItem(newItem1, { position: insertPosition });
// OR multiple inserts
// const newItem2 = this.createNewItem(2);
// this.angularGrid.gridService.addItems([newItem1, newItem2], { position: insertPosition });
}
createNewItem(incrementIdByHowMany = 1) {
const dataset = this.angularGrid.dataView.getItems();
let highestId = 0;
dataset.forEach((item: any) => {
if (item.id > highestId) {
highestId = item.id;
}
});
const newId = highestId + incrementIdByHowMany;
const randomYear = 2000 + Math.floor(Math.random() * 10);
const randomMonth = Math.floor(Math.random() * 11);
const randomDay = Math.floor(Math.random() * 29);
const randomPercent = Math.round(Math.random() * 100);
return {
id: newId,
title: 'Task ' + newId,
duration: Math.round(Math.random() * 100) + '',
percentComplete: randomPercent,
percentCompleteNumber: randomPercent,
start: new Date(randomYear, randomMonth, randomDay),
finish: new Date(randomYear, randomMonth + 2, randomDay),
effortDriven: true,
};
}
highlighFifthRow() {
this.scrollGridTop();
this.angularGrid.gridService.highlightRow(4, 1500);
}
/** Change the Duration Rows Background Color */
changeDurationBackgroundColor() {
this.dataView.getItemMetadata = this.updateItemMetadataForDurationOver40(this.dataView.getItemMetadata);
// also re-render the grid for the styling to be applied right away
this.grid.invalidate();
this.grid.render();
// or use the Angular-SlickGrid GridService
// this.gridService.renderGrid();
}
/**
* Change the SlickGrid Item Metadata, we will add a CSS class on all rows with a Duration over 50
* For more info, you can see this SO https://stackoverflow.com/a/19985148/1212166
*/
updateItemMetadataForDurationOver40(previousItemMetadata: any) {
const newCssClass = 'duration-bg';
return (rowNumber: number) => {
const item = this.dataView.getItem(rowNumber);
let meta = {
cssClasses: '',
};
if (typeof previousItemMetadata === 'object') {
meta = previousItemMetadata(rowNumber);
}
if (meta && item && item.duration) {
const duration = +item.duration; // convert to number
if (duration > 40) {
meta.cssClasses = (meta.cssClasses || '') + ' ' + newCssClass;
}
}
return meta;
};
}
updateSecondItem() {
this.scrollGridTop();
const updatedItem = this.angularGrid.gridService.getDataItemByRowNumber(1);
updatedItem.duration = Math.round(Math.random() * 100);
this.angularGrid.gridService.updateItem(updatedItem);
// OR by id
// this.angularGrid.gridService.updateItemById(updatedItem.id, updatedItem);
// OR multiple changes
/*
const updatedItem1 = this.angularGrid.gridService.getDataItemByRowNumber(1);
const updatedItem2 = this.angularGrid.gridService.getDataItemByRowNumber(2);
updatedItem1.duration = Math.round(Math.random() * 100);
updatedItem2.duration = Math.round(Math.random() * 100);
this.angularGrid.gridService.updateItems([updatedItem1, updatedItem2], { highlightRow: true });
*/
}
scrollGridBottom() {
this.angularGrid.slickGrid.navigateBottom();
}
scrollGridTop() {
this.angularGrid.slickGrid.navigateTop();
}
}