Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit cf114a4

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
Add functionality of Add/Update datagrid item with demo
1 parent e994f5d commit cf114a4

File tree

9 files changed

+198
-7
lines changed

9 files changed

+198
-7
lines changed

src/app/app-routing.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { NgModule } from '@angular/core';
2-
import { Routes, RouterModule } from '@angular/router';
1+
import { GridAddItemComponent } from './examples/grid-additem.component';
32
import { GridMenuComponent } from './examples/grid-menu.component';
43
import { GridBasicComponent } from './examples/grid-basic.component';
54
import { GridClientSideComponent } from './examples/grid-clientside.component';
@@ -10,8 +9,11 @@ import { GridHeaderMenuComponent } from './examples/grid-headermenu.component';
109
import { GridOdataComponent } from './examples/grid-odata.component';
1110
import { GridGraphqlComponent } from './examples/grid-graphql.component';
1211
import { GridRowSelectionComponent } from './examples/grid-rowselection.component';
12+
import { NgModule } from '@angular/core';
13+
import { Routes, RouterModule } from '@angular/router';
1314

1415
const routes: Routes = [
16+
{ path: 'additem', component: GridAddItemComponent },
1517
{ path: 'basic', component: GridBasicComponent },
1618
{ path: 'editor', component: GridEditorComponent },
1719
{ path: 'formatter', component: GridFormatterComponent },

src/app/app.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
<li routerLinkActive="active">
4747
<a [routerLink]="['/selection']">10- Row Selection</a>
4848
</li>
49+
<li routerLinkActive="active">
50+
<a [routerLink]="['/additem']">11- Add/Update Grid Item</a>
51+
</li>
4952
</ul>
5053
</div>
5154
</section>

src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { HttpClientModule } from '@angular/common/http';
66
import { NgModule } from '@angular/core';
77

88
import { AppComponent } from './app.component';
9+
import { GridAddItemComponent } from './examples/grid-additem.component';
910
import { GridBasicComponent } from './examples/grid-basic.component';
1011
import { GridClientSideComponent } from './examples/grid-clientside.component';
1112
import { GridEditorComponent } from './examples/grid-editor.component';
@@ -25,6 +26,7 @@ import { AngularSlickgridModule } from './modules/angular-slickgrid/modules/angu
2526
@NgModule({
2627
declarations: [
2728
AppComponent,
29+
GridAddItemComponent,
2830
GridBasicComponent,
2931
GridClientSideComponent,
3032
GridEditorComponent,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!--The content below is only a placeholder and can be replaced.-->
2+
<div class="container">
3+
<h2>{{title}}</h2>
4+
<div class="subtitle" [innerHTML]="subTitle"></div>
5+
6+
<div class="col-sm-12">
7+
<span>
8+
<button class="btn btn-sm btn-default" (click)="addNewItem()">Add New Mocked Item</button>
9+
<button class="btn btn-sm btn-default" (click)="updateSecondItem()">Update 2nd Row Item with Random Duration</button>
10+
<button class="btn btn-sm btn-default" (click)="highlighFifthRow()">Highlight 5th Row</button>
11+
</span>
12+
<hr/>
13+
</div>
14+
15+
<div class="col-sm-12">
16+
<angular-slickgrid gridId="grid2" [columnDefinitions]="columnDefinitions" [gridOptions]="gridOptions" [dataset]="dataset">
17+
</angular-slickgrid>
18+
</div>
19+
</div>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { Column, Editors, FieldType, Formatter, Formatters, GridExtraService, GridExtraUtils, GridOption, OnEventArgs, ResizerService } from './../modules/angular-slickgrid';
3+
4+
@Component({
5+
templateUrl: './grid-additem.component.html'
6+
})
7+
export class GridAddItemComponent implements OnInit {
8+
title = 'Example 11: Add / Update / Highlight a Datagrid Item';
9+
subTitle = `
10+
Add / Update / Hightlight an Item from the Datagrid (<a href="https://github.com/ghiscoding/Angular-Slickgrid/wiki/Add,-Update-or-Highlight-a-Datagrid-Item" target="_blank">Wiki link</a>).
11+
<ul>
12+
<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>
13+
<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>
14+
<li>Add/Update an item requires a valid Slickgrid Selection Model, you have 2 choices to deal with this:</li>
15+
<ul><li>You can enable "enableCheckboxSelector" or "enableRowSelection" to True</li></ul>
16+
<li>Click on any of the buttons below to test this out</li>
17+
<li>You can change the highlighted color &amp; animation by changing the SASS variables:</li>
18+
<ul>
19+
<li>"$row-highlight-background-color" or "$row-highlight-fade-animation"</li>
20+
<li>Take a look at the available <a href="https://github.com/ghiscoding/Angular-Slickgrid/blob/master/src/app/modules/angular-slickgrid/styles/_variables.scss" target="_blank">SASS Variables</a></li>
21+
</ul>
22+
</ul>
23+
`;
24+
25+
columnDefinitions: Column[];
26+
gridOptions: GridOption;
27+
dataset: any[];
28+
updatedObject: any;
29+
30+
constructor(private gridExtraService: GridExtraService, private resizer: ResizerService) {}
31+
32+
ngOnInit(): void {
33+
this.columnDefinitions = [
34+
{ id: 'title', name: 'Title', field: 'title', sortable: true, type: FieldType.string, editor: Editors.longText },
35+
{ id: 'duration', name: 'Duration (days)', field: 'duration', sortable: true, type: FieldType.number, editor: Editors.text,
36+
onCellChange: (args: OnEventArgs) => {
37+
alert('onCellChange directly attached to the column definition');
38+
console.log(args);
39+
}
40+
},
41+
{ id: 'complete', name: '% Complete', field: 'percentComplete', formatter: Formatters.percentCompleteBar, type: FieldType.number, editor: Editors.integer },
42+
{ id: 'start', name: 'Start', field: 'start', formatter: Formatters.dateIso, sortable: true, type: FieldType.date/*, editor: Editors.date*/ },
43+
{ id: 'finish', name: 'Finish', field: 'finish', formatter: Formatters.dateIso, sortable: true, type: FieldType.date },
44+
{ id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven', formatter: Formatters.checkmark, type: FieldType.number, editor: Editors.checkbox }
45+
];
46+
47+
this.gridOptions = {
48+
asyncEditorLoading: false,
49+
autoResize: {
50+
containerId: 'demo-container',
51+
sidePadding: 15
52+
},
53+
editable: true,
54+
enableColumnPicker: true,
55+
enableCellNavigation: true,
56+
enableRowSelection: true
57+
};
58+
59+
// mock a dataset
60+
let mockedDataset = [];
61+
for (let i = 0; i < 1000; i++) {
62+
const randomYear = 2000 + Math.floor(Math.random() * 10);
63+
const randomMonth = Math.floor(Math.random() * 11);
64+
const randomDay = Math.floor((Math.random() * 29));
65+
const randomPercent = Math.round(Math.random() * 100);
66+
67+
mockedDataset[i] = {
68+
id: i,
69+
title: 'Task ' + i,
70+
duration: Math.round(Math.random() * 100) + '',
71+
percentComplete: randomPercent,
72+
percentCompleteNumber: randomPercent,
73+
start: new Date(randomYear, randomMonth, randomDay),
74+
finish: new Date(randomYear, (randomMonth + 1), randomDay),
75+
effortDriven: (i % 5 === 0)
76+
};
77+
}
78+
this.dataset = mockedDataset;
79+
}
80+
81+
addNewItem() {
82+
const randomId = Math.floor(Math.random() * (10000 - 1000) + 1000);
83+
const randomYear = 2000 + Math.floor(Math.random() * 10);
84+
const randomMonth = Math.floor(Math.random() * 11);
85+
const randomDay = Math.floor((Math.random() * 29));
86+
const randomPercent = Math.round(Math.random() * 100);
87+
88+
const newItem = {
89+
id: randomId,
90+
title: 'Task ' + randomId,
91+
duration: Math.round(Math.random() * 100) + '',
92+
percentComplete: randomPercent,
93+
percentCompleteNumber: randomPercent,
94+
start: new Date(randomYear, randomMonth, randomDay),
95+
finish: new Date(randomYear, (randomMonth + 2), randomDay),
96+
effortDriven: true
97+
};
98+
this.gridExtraService.addItemToDatagrid(newItem);
99+
}
100+
101+
highlighFifthRow() {
102+
this.gridExtraService.highlightRow(4, 1500);
103+
}
104+
105+
updateSecondItem() {
106+
const firstItem = this.gridExtraService.getDataItemByRowNumber(1);
107+
firstItem.duration = Math.round(Math.random() * 100);
108+
this.gridExtraService.updateDataGridItem(firstItem);
109+
}
110+
}

src/app/examples/grid-editor.component.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ <h2>{{title}}</h2>
55

66
<div class="col-sm-4">
77
<label>autoEdit setting</label>
8-
<br/>
98
<span id="radioAutoEdit">
109
<label class="radio-inline control-label" for="radioTrue">
1110
<input type="radio" name="inlineRadioOptions" id="radioTrue" checked [value]="isAutoEdit" (change)="setAutoEdit(true)"> ON (single-click)

src/app/examples/grid-editor.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class GridEditorComponent implements OnInit {
99
subTitle = `
1010
Grid with Inline Editors and onCellClick actions (<a href="https://github.com/ghiscoding/Angular-Slickgrid/wiki/Editors" target="_blank">Wiki link</a>).
1111
<ul>
12-
<li>When using "enableCellNavigation: true", clicking on a cell will automatically make it active &amp; selected.
12+
<li>When using "enableCellNavigation: true", clicking on a cell will automatically make it active &amp; selected.</li>
1313
<ul><li>If you don't want this behavior, then you should disable "enableCellNavigation"</li></ul>
1414
<li>Inline Editors requires "enableCellNavigation: true" (not sure why though)</li>
1515
</ul>

src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
117117
this.attachResizeHook(this.grid, this._gridOptions);
118118

119119
// attach grid extra service
120-
const gridExtraService = this.gridExtraService.init(this.grid, this._dataView);
120+
const gridExtraService = this.gridExtraService.init(this.grid, this.columnDefinitions, this._gridOptions, this._dataView);
121121
}
122122

123123
attachDifferentHooks(grid: any, options: GridOption, dataView: any) {

src/app/modules/angular-slickgrid/services/gridExtra.service.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
import { GridOption } from './../models';
1+
import { Column, GridOption } from './../models';
22
import $ from 'jquery';
33

44
export class GridExtraService {
55
private _grid: any;
66
private _dataView: any;
7+
private _columnDefinition: Column[];
8+
private _gridOptions: GridOption;
79

8-
init(grid, dataView) {
10+
init(grid, columnDefinition, gridOptions, dataView) {
911
this._grid = grid;
12+
this._columnDefinition = columnDefinition;
13+
this._gridOptions = gridOptions;
1014
this._dataView = dataView;
1115
}
1216

17+
getDataItemByRowNumber(rowNumber: number) {
18+
if (!this._grid || typeof this._grid.getDataItem !== 'function') {
19+
throw new Error('We could not find SlickGrid Grid object');
20+
}
21+
return this._grid.getDataItem(rowNumber);
22+
}
23+
1324
/** Chain the item Metadata with our implementation of Metadata at given row index */
1425
getItemRowMetadata(previousItemMetadata: any) {
1526
return (rowNumber: number) => {
@@ -71,4 +82,49 @@ export class GridExtraService {
7182
setSelectedRows(rowIndexes: number[]) {
7283
this._grid.setSelectedRows(rowIndexes);
7384
}
85+
86+
/** Add an item (data item) to the datagrid
87+
* @param object dataItem: item object holding all properties of that row
88+
*/
89+
addItemToDatagrid(item) {
90+
if (!this._grid || !this._gridOptions || !this._dataView) {
91+
throw new Error('We could not find SlickGrid Grid, DataView objects');
92+
}
93+
if (!this._gridOptions || (!this._gridOptions.enableCheckboxSelector && !this._gridOptions.enableRowSelection)) {
94+
throw new Error('addItemToDatagrid() requires to have a valid Slickgrid Selection Model. You can overcome this issue by enabling enableCheckboxSelector or enableRowSelection to True');
95+
}
96+
97+
const row = 0;
98+
this._dataView.insertItem(row, item);
99+
this.highlightRow(0, 1500);
100+
101+
// refresh dataview & grid
102+
this._dataView.refresh();
103+
104+
// get new dataset length
105+
const datasetLength = this._dataView.getLength();
106+
}
107+
108+
/** Update an existing item with new properties inside the datagrid
109+
* @param object item: item object holding all properties of that row
110+
*/
111+
updateDataGridItem(item: any) {
112+
const row = this._dataView.getRowById(item.id);
113+
const itemId = (!item || !item.hasOwnProperty('id')) ? -1 : item.id;
114+
if (itemId === -1) {
115+
throw new Error(`Could not find the item in the item in the grid or it's associated "id"`);
116+
}
117+
118+
// Update the item itself inside the dataView
119+
this._dataView.updateItem(itemId, item);
120+
121+
// highlight the row we just updated
122+
this.highlightRow(row, 1500);
123+
124+
// refresh dataview & grid
125+
this._dataView.refresh();
126+
127+
// get new dataset length
128+
const datasetLength = this._dataView.getLength();
129+
}
74130
}

0 commit comments

Comments
 (0)