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

Commit f84ba67

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
feat(grid): update inline editor demo with undo, related to #15
1 parent 0cd718f commit f84ba67

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ <h2>{{title}}</h2>
1414
</label>
1515
</span>
1616
<div class="row col-sm-12">
17+
<span>
18+
<button class="btn btn-default btn-sm" (click)="undo()">
19+
<i class="fa fa-undo"></i>
20+
Undo last edit
21+
</button>
22+
</span>
1723
<button class="btn btn-default btn-sm" (click)="switchLanguage()">Switch Language</button>
24+
<br/>
1825
<b>Locale:</b> <span style="font-style: italic">{{selectedLanguage + '.json'}}</span>
1926
</div>
2027
</div>
@@ -25,6 +32,9 @@ <h2>{{title}}</h2>
2532
<div class="alert alert-info" *ngIf="updatedObject">
2633
<strong>Update Object:</strong> {{updatedObject | json}}
2734
</div>
35+
<div class="alert alert-warning" *ngIf="alertWarning">
36+
<strong>Update Object:</strong> {{alertWarning}}
37+
</div>
2838
</div>
2939

3040
<div class="col-sm-12">

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { Component, Injectable, OnInit, OnDestroy } from '@angular/core';
22
import { Column, Editors, FieldType, Formatters, GridExtraService, GridExtraUtils, GridOption, OnEventArgs, ResizerService } from './../modules/angular-slickgrid';
33
import { TranslateService } from '@ngx-translate/core';
44

5+
// using external non-typed js libraries
6+
declare var Slick: any;
7+
58
@Component({
69
templateUrl: './grid-editor.component.html'
710
})
@@ -17,10 +20,12 @@ export class GridEditorComponent implements OnInit, OnDestroy {
1720
</ul>
1821
`;
1922

23+
private _commandQueue = [];
2024
columnDefinitions: Column[];
2125
gridOptions: GridOption;
2226
dataset: any[];
2327
isAutoEdit = true;
28+
alertWarning: any;
2429
updatedObject: any;
2530
gridObj: any;
2631
dataviewObj: any;
@@ -31,6 +36,7 @@ export class GridEditorComponent implements OnInit, OnDestroy {
3136
ngOnInit(): void {
3237
this.prepareGrid();
3338
}
39+
3440
ngOnDestroy(): void {
3541
// unsubscrible any Slick.Event you might have used
3642
// a reminder again, these are SlickGrid Event, not RxJS events
@@ -48,7 +54,7 @@ export class GridEditorComponent implements OnInit, OnDestroy {
4854
// use onCellClick OR grid.onClick.subscribe which you can see down below
4955
onCellClick: (args: OnEventArgs) => {
5056
console.log(args);
51-
alert(`Editing: ${args.dataContext.title}`);
57+
this.alertWarning = `Editing: ${args.dataContext.title}`;
5258
this.gridExtraService.highlightRow(args.row, 1500);
5359
this.gridExtraService.setSelectedRow(args.row);
5460
}
@@ -61,14 +67,14 @@ export class GridEditorComponent implements OnInit, OnDestroy {
6167
// use onCellClick OR grid.onClick.subscribe which you can see down below
6268
onCellClick: (args: OnEventArgs) => {
6369
console.log(args);
64-
alert(`Deleting: ${args.dataContext.title}`);
70+
this.alertWarning = `Deleting: ${args.dataContext.title}`;
6571
}
6672
},
6773
{ id: 'title', name: 'Title', field: 'title', sortable: true, type: FieldType.string, editor: Editors.longText },
6874
{ id: 'duration', name: 'Duration (days)', field: 'duration', sortable: true, type: FieldType.number, editor: Editors.text,
6975
onCellChange: (args: OnEventArgs) => {
70-
alert('onCellChange directly attached to the column definition');
71-
console.log(args);
76+
console.log(args);
77+
this.alertWarning = 'onCellChange triggered and attached to the column definition';
7278
}
7379
},
7480
{ id: 'complete', name: '% Complete', field: 'percentComplete', formatter: Formatters.percentCompleteBar, type: FieldType.number, editor: Editors.integer },
@@ -86,7 +92,11 @@ export class GridEditorComponent implements OnInit, OnDestroy {
8692
},
8793
editable: true,
8894
enableColumnPicker: true,
89-
enableCellNavigation: true
95+
enableCellNavigation: true,
96+
editCommandHandler: (item, column, editCommand) => {
97+
this._commandQueue.push(editCommand);
98+
editCommand.execute();
99+
}
90100
};
91101

92102
// mock a dataset
@@ -126,7 +136,7 @@ export class GridEditorComponent implements OnInit, OnDestroy {
126136
const column = GridExtraUtils.getColumnDefinitionAndData(args);
127137
console.log('onClick', args, column);
128138
if (column.columnDef.id === 'edit') {
129-
alert('open a modal window to edit: ' + column.dataContext.title);
139+
this.alertWarning = `open a modal window to edit: ${column.dataContext.title}`;
130140

131141
// highlight the row, to customize the color, you can change the SASS variable $row-highlight-background-color
132142
this.gridExtraService.highlightRow(args.row, 1500);
@@ -155,4 +165,12 @@ export class GridEditorComponent implements OnInit, OnDestroy {
155165
this.selectedLanguage = (this.selectedLanguage === 'en') ? 'fr' : 'en';
156166
this.translate.use(this.selectedLanguage);
157167
}
168+
169+
undo() {
170+
const command = this._commandQueue.pop();
171+
if (command && Slick.GlobalEditorLock.cancelCurrentEdit()) {
172+
command.undo();
173+
this.gridObj.gotoCell(command.row, command.cell, false);
174+
}
175+
}
158176
}

0 commit comments

Comments
 (0)