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

Commit 444c790

Browse files
authored
Merge pull request #212 from ghiscoding/bugfix/dynamic-editor-column
fix(editor): dynamically adding editor column throws error, fixes #195
2 parents 8b7e4f1 + e1ab9be commit 444c790

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ <h2>{{title}}</h2>
2929
<button class="btn btn-default btn-sm" (click)="angularGrid.sortService.clearSorting()">Clear Sorting</button>
3030
<button class="btn btn-default btn-sm btn-info" (click)="addItem()" title="Clear Filters &amp; Sorting to see it better">Add item</button>
3131
<button class="btn btn-default btn-sm btn-danger" (click)="deleteItem()">Delete item</button>
32+
<button class="btn btn-default btn-sm" (click)="dynamicallyAddTitleHeader()">
33+
<i class="fa fa-plus"></i>
34+
Dynamically Duplicate Title Column
35+
</button>
3236
</div>
3337
</div>
3438

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export class GridEditorComponent implements OnInit {
9494
alertWarning: any;
9595
updatedObject: any;
9696
selectedLanguage = 'en';
97+
duplicateTitleHeaderCount = 1;
9798

9899
constructor(private http: HttpClient, private translate: TranslateService) { }
99100

@@ -575,6 +576,22 @@ export class GridEditorComponent implements OnInit {
575576
return true;
576577
}
577578

579+
dynamicallyAddTitleHeader() {
580+
const newCol = {
581+
id: `title${this.duplicateTitleHeaderCount++}`,
582+
name: 'Title',
583+
field: 'title',
584+
editor: {
585+
model: Editors.text,
586+
required: true,
587+
validator: myCustomTitleValidator, // use a custom validator
588+
},
589+
sortable: true, minWidth: 100, filterable: true, params: { useFormatterOuputToFilter: true }
590+
};
591+
this.columnDefinitions.push(newCol);
592+
this.columnDefinitions = this.columnDefinitions.slice();
593+
}
594+
578595
setAutoEdit(isAutoEdit) {
579596
this.isAutoEdit = isAutoEdit;
580597
this.gridObj.setOptions({ autoEdit: isAutoEdit }); // change the grid option dynamically

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,11 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
243243
}
244244
}
245245

246-
// for convenience, we provide the property "editor" as an Angular-Slickgrid editor complex object
246+
// for convenience to the user, we provide the property "editor" as an Angular-Slickgrid editor complex object
247247
// however "editor" is used internally by SlickGrid for it's own Editor Factory
248248
// so in our lib we will swap "editor" and copy it into a new property called "internalColumnEditor"
249249
// then take back "editor.model" and make it the new "editor" so that SlickGrid Editor Factory still works
250-
this._columnDefinitions = this._columnDefinitions.map((column: Column | any) => {
251-
// on every Editor that have a "collectionAsync", resolve the data and assign it to the "collection" property
252-
if (column.editor && column.editor.collectionAsync) {
253-
this.loadEditorCollectionAsync(column);
254-
}
255-
return { ...column, editor: column.editor && column.editor.model, internalColumnEditor: { ...column.editor } };
256-
});
250+
this._columnDefinitions = this.swapInternalEditorToSlickGridFactoryEditor(this._columnDefinitions);
257251

258252
// save reference for all columns before they optionally become hidden/visible
259253
this.sharedService.allColumns = this._columnDefinitions;
@@ -699,6 +693,9 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
699693
* If using i18n, we also need to trigger a re-translate of the column headers
700694
*/
701695
updateColumnDefinitionsList(newColumnDefinitions) {
696+
// map/swap the internal library Editor to the SlickGrid Editor factory
697+
newColumnDefinitions = this.swapInternalEditorToSlickGridFactoryEditor(newColumnDefinitions);
698+
702699
if (this.gridOptions.enableTranslate) {
703700
this.extensionService.translateColumnHeaders(false, newColumnDefinitions);
704701
} else {
@@ -748,6 +745,22 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
748745
}
749746
}
750747

748+
/**
749+
* For convenience to the user, we provide the property "editor" as an Angular-Slickgrid editor complex object
750+
* however "editor" is used internally by SlickGrid for it's own Editor Factory
751+
* so in our lib we will swap "editor" and copy it into a new property called "internalColumnEditor"
752+
* then take back "editor.model" and make it the new "editor" so that SlickGrid Editor Factory still works
753+
*/
754+
private swapInternalEditorToSlickGridFactoryEditor(columnDefinitions: Column[]) {
755+
return columnDefinitions.map((column: Column | any) => {
756+
// on every Editor that have a "collectionAsync", resolve the data and assign it to the "collection" property
757+
if (column.editor && column.editor.collectionAsync) {
758+
this.loadEditorCollectionAsync(column);
759+
}
760+
return { ...column, editor: column.editor && column.editor.model, internalColumnEditor: { ...column.editor } };
761+
});
762+
}
763+
751764
/**
752765
* Update the Editor "collection" property from an async call resolved
753766
* Since this is called after the async call resolves, the pointer will not be the same as the "column" argument passed.

0 commit comments

Comments
 (0)