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

Commit 013524b

Browse files
Ghislain BeaulacGhislain Beaulac
Ghislain Beaulac
authored and
Ghislain Beaulac
committed
refactor(postRender): cleanup code & fix RowDetailExtension
1 parent 5b32de2 commit 013524b

File tree

7 files changed

+53
-45
lines changed

7 files changed

+53
-45
lines changed

src/app/app-routing.module.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1+
import { HomeComponent } from './examples/home.component';
12
import { GridAddItemComponent } from './examples/grid-additem.component';
2-
import { GridMenuComponent } from './examples/grid-menu.component';
3+
import { GridAngularComponent } from './examples/grid-angular.component';
34
import { GridBasicComponent } from './examples/grid-basic.component';
45
import { GridClientSideComponent } from './examples/grid-clientside.component';
56
import { GridColspanComponent } from './examples/grid-colspan.component';
67
import { GridDraggableGroupingComponent } from './examples/grid-draggrouping.component';
78
import { GridEditorComponent } from './examples/grid-editor.component';
8-
import { GridEditorAngularComponent } from './examples/grid-editor-angular.component';
99
import { GridFormatterComponent } from './examples/grid-formatter.component';
1010
import { GridFrozenComponent } from './examples/grid-frozen.component';
11+
import { GridGraphqlComponent } from './examples/grid-graphql.component';
1112
import { GridGroupingComponent } from './examples/grid-grouping.component';
1213
import { GridHeaderButtonComponent } from './examples/grid-headerbutton.component';
1314
import { GridHeaderMenuComponent } from './examples/grid-headermenu.component';
1415
import { GridLocalizationComponent } from './examples/grid-localization.component';
16+
import { GridMenuComponent } from './examples/grid-menu.component';
1517
import { GridOdataComponent } from './examples/grid-odata.component';
16-
import { GridGraphqlComponent } from './examples/grid-graphql.component';
1718
import { GridRemoteComponent } from './examples/grid-remote.component';
1819
import { GridRowDetailComponent } from './examples/grid-rowdetail.component';
1920
import { GridRowMoveComponent } from './examples/grid-rowmove.component';
2021
import { GridRowSelectionComponent } from './examples/grid-rowselection.component';
2122
import { GridStateComponent } from './examples/grid-state.component';
22-
import { HomeComponent } from './examples/home.component';
2323
import { SwtCommonGridTestComponent } from './examples/swt-common-grid-test.component';
2424

2525
import { NgModule } from '@angular/core';
@@ -28,11 +28,11 @@ import { TranslateModule } from '@ngx-translate/core';
2828

2929
const routes: Routes = [
3030
{ path: 'home', component: HomeComponent },
31+
{ path: 'angular-components', component: GridAngularComponent },
3132
{ path: 'additem', component: GridAddItemComponent },
3233
{ path: 'basic', component: GridBasicComponent },
3334
{ path: 'colspan', component: GridColspanComponent },
3435
{ path: 'editor', component: GridEditorComponent },
35-
{ path: 'editor-angular', component: GridEditorAngularComponent },
3636
{ path: 'formatter', component: GridFormatterComponent },
3737
{ path: 'frozen', component: GridFrozenComponent },
3838
{ path: 'headerbutton', component: GridHeaderButtonComponent },

src/app/app.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<a [routerLink]="['/rowdetail']">21- Row Detail View</a>
9393
</li>
9494
<li routerLinkActive="active">
95-
<a [routerLink]="['/editor-angular']">22- Editors Angular Components</a>
95+
<a [routerLink]="['/angular-components']">22- Editors Angular Components</a>
9696
</li>
9797
</ul>
9898
</div>

src/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { GridClientSideComponent } from './examples/grid-clientside.component';
1717
import { GridColspanComponent } from './examples/grid-colspan.component';
1818
import { GridDraggableGroupingComponent } from './examples/grid-draggrouping.component';
1919
import { GridEditorComponent } from './examples/grid-editor.component';
20-
import { GridEditorAngularComponent } from './examples/grid-editor-angular.component';
20+
import { GridAngularComponent } from './examples/grid-angular.component';
2121
import { GridFormatterComponent } from './examples/grid-formatter.component';
2222
import { GridFrozenComponent } from './examples/grid-frozen.component';
2323
import { GridGraphqlComponent } from './examples/grid-graphql.component';
@@ -75,12 +75,12 @@ export function appInitializerFactory(translate: TranslateService, injector: Inj
7575
CustomTitleFormatterComponent,
7676
EditorNgSelectComponent,
7777
GridAddItemComponent,
78+
GridAngularComponent,
7879
GridBasicComponent,
7980
GridClientSideComponent,
8081
GridColspanComponent,
8182
GridDraggableGroupingComponent,
8283
GridEditorComponent,
83-
GridEditorAngularComponent,
8484
GridFormatterComponent,
8585
GridFrozenComponent,
8686
GridGraphqlComponent,

src/app/examples/custom-angularComponentEditor.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Editor,
66
EditorValidator,
77
EditorValidatorOutput,
8+
GridOption,
89
} from './../modules/angular-slickgrid';
910

1011
/*
@@ -21,13 +22,21 @@ export class CustomAngularComponentEditor implements Editor {
2122
/** default item object */
2223
defaultItem: any;
2324

25+
/** SlickGrid grid object */
26+
grid: any;
27+
2428
constructor(private args: any) {
29+
this.grid = args && args.grid;
2530
this.init();
2631
}
2732

28-
/** Angular Util Service */
33+
/** Angular Util Service (could be inside the Grid Options Params or the Editor Params ) */
2934
get angularUtilService(): AngularUtilService {
30-
return this.columnDef && this.columnDef && this.columnDef.internalColumnEditor && this.columnDef.internalColumnEditor.params.angularUtilService;
35+
let angularUtilService = this.gridOptions && this.gridOptions.params && this.gridOptions.params.angularUtilService;
36+
if (!angularUtilService || !(angularUtilService instanceof AngularUtilService)) {
37+
angularUtilService = this.columnEditor && this.columnEditor.params && this.columnEditor.params.angularUtilService;
38+
}
39+
return angularUtilService;
3140
}
3241

3342
/** Get the Collection */
@@ -45,8 +54,13 @@ export class CustomAngularComponentEditor implements Editor {
4554
return this.columnDef && this.columnDef.internalColumnEditor || {};
4655
}
4756

57+
/** Getter for the Grid Options pulled through the Grid Object */
58+
get gridOptions(): GridOption {
59+
return (this.grid && this.grid.getOptions) ? this.grid.getOptions() : {};
60+
}
61+
4862
get hasAutoCommitEdit() {
49-
return this.args.grid.getOptions().autoCommitEdit;
63+
return this.gridOptions.autoCommitEdit;
5064
}
5165

5266
/** Get the Validator function, can be passed in Editor property or Column Definition */
@@ -55,13 +69,14 @@ export class CustomAngularComponentEditor implements Editor {
5569
}
5670

5771
init() {
58-
if (!this.columnEditor || !this.columnEditor.params.component || !(this.columnEditor.params.angularUtilService instanceof AngularUtilService)) {
72+
if (!this.columnEditor || !this.columnEditor.params.component || !(this.angularUtilService instanceof AngularUtilService)) {
5973
throw new Error(`[Angular-Slickgrid] For the Editors.angularComponent to work properly, you need to provide your component to the "component" property and make sure to add it to your "entryComponents" array.
60-
Example: this.columnDefs = [{ id: 'title', field: 'title', editor: { model: Editors.angularComponent, collection: [...] }, params: { component: MyComponent, angularUtilService: this.angularUtilService }`);
74+
You also need to provide the "AngularUtilService" via the Editor Params OR the Grid Options Params
75+
Example: this.columnDefs = [{ id: 'title', field: 'title', editor: { model: CustomAngularComponentEditor, collection: [...] }, params: { component: MyComponent, angularUtilService: this.angularUtilService }];
76+
OR this.columnDefs = [{ id: 'title', field: 'title', editor: { model: CustomAngularComponentEditor, collection: [...] }]; this.gridOptions = { params: { angularUtilService: this.angularUtilService }}`);
6177
}
6278
if (this.columnEditor && this.columnEditor.params.component) {
63-
const angularUtilService = this.columnEditor.params.angularUtilService as AngularUtilService;
64-
const componentOutput = angularUtilService.createAngularComponentAppendToDom(this.columnEditor.params.component, this.args.container);
79+
const componentOutput = this.angularUtilService.createAngularComponentAppendToDom(this.columnEditor.params.component, this.args.container);
6580
this.componentRef = componentOutput && componentOutput.componentRef;
6681
Object.assign(this.componentRef.instance, { collection: this.collection });
6782

src/app/examples/grid-editor-angular.component.ts renamed to src/app/examples/grid-angular.component.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ declare var $: any;
2323
const NB_ITEMS = 100;
2424

2525
@Component({
26-
templateUrl: './grid-editor-angular.component.html'
26+
templateUrl: './grid-angular.component.html'
2727
})
2828
@Injectable()
29-
export class GridEditorAngularComponent implements OnInit {
30-
title = 'Example 22: Editors with Angular Components';
29+
export class GridAngularComponent implements OnInit {
30+
title = 'Example 22: Multiple Angular Components';
3131
subTitle = `
32-
Grid with Inline Editors and onCellClick actions (<a href="https://github.com/ghiscoding/Angular-Slickgrid/wiki/Editors" target="_blank">Uncyclo docs</a>).
32+
Grid with usage of Angular Components as Editor &amp; AsyncPostRender (similar to Formatter).
3333
<ul>
34-
<li>Support of Angular Component as Custom Editor (click on "Assignee" column)</li>
35-
<li>The column "Assignee" shown below uses <a href="https://github.com/ng-select/ng-select" target="_blank">ng-select</a> as a custom editor with Angular Component
36-
<li>Increased rowHeight to 45 so that the "ng-select" fits in the cell. Ideally it would be better to override the ng-select component styling to change it's max height</li>
34+
<li>Support of Angular Component as Custom Editor (click on any "Assignee" name cell)</li>
35+
<ul>
36+
<li>That column uses <a href="https://github.com/ng-select/ng-select" target="_blank">ng-select</a> as a custom editor as an Angular Component
37+
<li>Increased Grid Options "rowHeight" to 45 so that the "ng-select" fits in the cell. Ideally it would be better to override the ng-select css styling to change it's max height</li>
38+
</ul>
39+
<li>The 2nd "Assignee" column (showing in bold text) uses "asyncPostRender" with an Angular Component</li>
3740
</ul>
3841
`;
3942

@@ -98,7 +101,6 @@ export class GridEditorAngularComponent implements OnInit {
98101
model: CustomAngularComponentEditor,
99102
collection: this.assignees,
100103
params: {
101-
angularUtilService: this.angularUtilService,
102104
component: EditorNgSelectComponent,
103105
}
104106
},
@@ -127,20 +129,6 @@ export class GridEditorAngularComponent implements OnInit {
127129
angularUtilService: this.angularUtilService,
128130
},
129131
exportWithFormatter: true,
130-
}, {
131-
id: 'duration',
132-
name: 'Duration (days)',
133-
field: 'duration',
134-
minWidth: 100,
135-
filterable: true,
136-
sortable: true,
137-
type: FieldType.number,
138-
filter: { model: Filters.slider, params: { hideSliderNumber: false } },
139-
editor: {
140-
model: Editors.slider,
141-
minValue: 0,
142-
maxValue: 100,
143-
}
144132
}, {
145133
id: 'complete',
146134
name: '% Complete',
@@ -229,7 +217,10 @@ export class GridEditorAngularComponent implements OnInit {
229217
this._commandQueue.push(editCommand);
230218
editCommand.execute();
231219
},
232-
i18n: this.translate
220+
i18n: this.translate,
221+
params: {
222+
angularUtilService: this.angularUtilService // provide the service to all at once (Editor, Filter, AsyncPostRender)
223+
}
233224
};
234225

235226
this.dataset = this.mockData(NB_ITEMS);
@@ -313,8 +304,8 @@ export class GridEditorAngularComponent implements OnInit {
313304
const componentOutput = this.angularUtilService.createAngularComponent(colDef.params.component);
314305
Object.assign(componentOutput.componentRef.instance, { item: dataContext });
315306

316-
// use a delay to make sure Angular ran at least a cycle and it finished rendering the Component
317-
setTimeout(() => $(cellNode).empty().html($(componentOutput.domElement).html()));
307+
// use a delay to make sure Angular ran at least a full cycle and it finished rendering the Component
308+
setTimeout(() => $(cellNode).empty().html(componentOutput.domElement));
318309
}
319310
}
320311
}

src/app/modules/angular-slickgrid/extensions/rowDetailViewExtension.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,14 @@ export class RowDetailViewExtension implements Extension {
324324
private renderViewModel(item: any) {
325325
const containerElements = document.getElementsByClassName(`${ROW_DETAIL_CONTAINER_PREFIX}${item.id}`);
326326
if (containerElements && containerElements.length) {
327-
const compRef = this.angularUtilService.createAngularComponentAppendToDom(this._viewComponent, containerElements[0]);
328-
Object.assign(compRef.instance, { model: item });
327+
const componentOutput = this.angularUtilService.createAngularComponentAppendToDom(this._viewComponent, containerElements[0]);
328+
if (componentOutput && componentOutput.componentRef && componentOutput.componentRef.instance) {
329+
Object.assign(componentOutput.componentRef.instance, { model: item });
329330

330-
const viewObj = this._views.find((obj) => obj.id === item.id);
331-
if (viewObj) {
332-
viewObj.componentRef = compRef;
331+
const viewObj = this._views.find((obj) => obj.id === item.id);
332+
if (viewObj) {
333+
viewObj.componentRef = componentOutput.componentRef;
334+
}
333335
}
334336
}
335337
}

0 commit comments

Comments
 (0)