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

fix: don't crash when using visible on columns #1725

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions demo/src/app/advanced/using-ng-pipe.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,23 @@ describe('UsingNgPipeComponent', () => {
.toEqual(expectedArray);
});


it('should not crash when using "visible: false" for columns', async () => {
await fixture.whenStable();
fixture.detectChanges();

const query = fixture.debugElement.query(By.directive(DataTableDirective));
const dir = query.injector.get(DataTableDirective);
expect(dir).toBeTruthy();

// hide first column
(await dir.dtInstance).columns(0).visible(false);
await fixture.whenRenderingDone();

fixture.detectChanges();

// verify app still works
expect((await dir.dtInstance).column(0).visible()).toBeFalse();
});

});
19 changes: 19 additions & 0 deletions demo/src/app/advanced/using-ng-template-ref.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ describe('UsingNgTemplateRefComponent', () => {

});

it('should not crash when using "visible: false" for columns', async () => {
await fixture.whenStable();
fixture.detectChanges();

const query = fixture.debugElement.query(By.directive(DataTableDirective));
const dir = query.injector.get(DataTableDirective);
expect(dir).toBeTruthy();

// hide first column
(await dir.dtInstance).columns(0).visible(false);
await fixture.whenRenderingDone();

fixture.detectChanges();

// verify app still works
expect((await dir.dtInstance).column(0).visible()).toBeFalse();
});


it('should not have duplicate contents in ngTemplateRef column when navigating pages', async () => {
await fixture.whenStable();
fixture.detectChanges();
Expand Down
4 changes: 2 additions & 2 deletions src/angular-datatables.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
const pipe = el.ngPipeInstance;
const pipeArgs = el.ngPipeArgs || [];
// find index of column using `data` attr
const i = columns.findIndex(e => e.data === el.data);
const i = columns.filter(c => c.visible !== false).findIndex(e => e.data === el.data);
// get <td> element which holds data using index
const rowFromCol = row.childNodes.item(i);
// Transform data with Pipe and PipeArgs
Expand All @@ -125,7 +125,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
colsWithTemplate.forEach(el => {
const { ref, context } = el.ngTemplateRef;
// get <td> element which holds data using index
const i = columns.findIndex(e => e.data === el.data);
const i = columns.filter(c => c.visible !== false).findIndex(e => e.data === el.data);
const cellFromIndex = row.childNodes.item(i);
// reset cell before applying transform
$(cellFromIndex).html('');
Expand Down