Skip to content

Commit 5d61692

Browse files
authored
Merge pull request #1505 from ghiscoding/feat/grid-options-not-required
feat: allow using AngularSlickgrid component w/o grid options
2 parents 9c91f8d + 396582b commit 5d61692

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,14 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
454454
expect(component.gridOptions.enableMouseWheelScrollHandler).toBeTrue();
455455
});
456456

457-
it('should throw an error when [gridOptions] and/or [columnDefinitions] is undefined', (done) => {
457+
it('should throw an error when [columnDefinitions] is undefined', (done) => {
458458
try {
459-
component.gridOptions = undefined as any;
459+
component.columnDefinitions = '' as any;
460+
component.gridOptions = gridOptions;
460461
component.ngAfterViewInit();
461462
component.dataset = [];
462463
} catch (e: any) {
463-
expect(e.toString()).toContain('Using `<angular-slickgrid>` requires [gridOptions] and [columnDefinitions]');
464+
expect(e.toString()).toContain('Using `<angular-slickgrid>` requires [columnDefinitions]');
464465
component.destroy();
465466
done();
466467
}

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
156156

157157
@Input() customDataView: any;
158158
@Input() gridId = '';
159-
@Input() gridOptions!: GridOption;
159+
@Input() gridOptions: GridOption = {};
160160

161161
@Input()
162162
get paginationOptions(): Pagination | undefined {
@@ -359,8 +359,8 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
359359
}
360360

361361
ngAfterViewInit() {
362-
if (!this.gridOptions || !this.columnDefinitions) {
363-
throw new Error('Using `<angular-slickgrid>` requires [gridOptions] and [columnDefinitions], it seems that you might have forgot to provide them since at least of them is undefined.');
362+
if (!this.columnDefinitions) {
363+
throw new Error('Using `<angular-slickgrid>` requires [columnDefinitions], it seems that you might have forgot to provide the missing bindable input.');
364364
}
365365

366366
this.initialization(this._eventHandler);
@@ -424,8 +424,10 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
424424
}
425425
this.backendServiceApi = undefined;
426426
}
427-
for (const prop of Object.keys(this.columnDefinitions)) {
428-
(this.columnDefinitions as any)[prop] = null;
427+
if (this.columnDefinitions) {
428+
for (const prop of Object.keys(this.columnDefinitions)) {
429+
(this.columnDefinitions as any)[prop] = null;
430+
}
429431
}
430432
for (const prop of Object.keys(this.sharedService)) {
431433
(this.sharedService as any)[prop] = null;
@@ -459,7 +461,7 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
459461
}
460462

461463
emptyGridContainerElm() {
462-
const gridContainerId = this.gridOptions?.gridContainerId ?? 'grid1';
464+
const gridContainerId = this.gridOptions?.gridContainerId || 'grid1';
463465
const gridContainerElm = document.querySelector(`#${gridContainerId}`);
464466
emptyElement(gridContainerElm);
465467
}
@@ -502,7 +504,7 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
502504
this._eventPubSubService.publish('onBeforeGridCreate', true);
503505

504506
// make sure the dataset is initialized (if not it will throw an error that it cannot getLength of null)
505-
this._dataset = this._dataset || [];
507+
this._dataset ||= [];
506508
this.gridOptions = this.mergeGridOptions(this.gridOptions);
507509
this._paginationOptions = this.gridOptions?.pagination;
508510
this.locales = this.gridOptions?.locales ?? Constants.locales;

0 commit comments

Comments
 (0)