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

Commit 82322f8

Browse files
authored
Merge pull request #1725 from l-lin/fix-crash-on-visible-flag
fix: don't crash when using `visible` on columns
2 parents 7e04669 + 68488bc commit 82322f8

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

demo/src/app/advanced/using-ng-pipe.component.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,23 @@ describe('UsingNgPipeComponent', () => {
113113
.toEqual(expectedArray);
114114
});
115115

116+
117+
it('should not crash when using "visible: false" for columns', async () => {
118+
await fixture.whenStable();
119+
fixture.detectChanges();
120+
121+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
122+
const dir = query.injector.get(DataTableDirective);
123+
expect(dir).toBeTruthy();
124+
125+
// hide first column
126+
(await dir.dtInstance).columns(0).visible(false);
127+
await fixture.whenRenderingDone();
128+
129+
fixture.detectChanges();
130+
131+
// verify app still works
132+
expect((await dir.dtInstance).column(0).visible()).toBeFalse();
133+
});
134+
116135
});

demo/src/app/advanced/using-ng-template-ref.component.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ describe('UsingNgTemplateRefComponent', () => {
7575

7676
});
7777

78+
it('should not crash when using "visible: false" for columns', async () => {
79+
await fixture.whenStable();
80+
fixture.detectChanges();
81+
82+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
83+
const dir = query.injector.get(DataTableDirective);
84+
expect(dir).toBeTruthy();
85+
86+
// hide first column
87+
(await dir.dtInstance).columns(0).visible(false);
88+
await fixture.whenRenderingDone();
89+
90+
fixture.detectChanges();
91+
92+
// verify app still works
93+
expect((await dir.dtInstance).column(0).visible()).toBeFalse();
94+
});
95+
96+
7897
it('should not have duplicate contents in ngTemplateRef column when navigating pages', async () => {
7998
await fixture.whenStable();
8099
fixture.detectChanges();

src/angular-datatables.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
108108
const pipe = el.ngPipeInstance;
109109
const pipeArgs = el.ngPipeArgs || [];
110110
// find index of column using `data` attr
111-
const i = columns.findIndex(e => e.data === el.data);
111+
const i = columns.filter(c => c.visible !== false).findIndex(e => e.data === el.data);
112112
// get <td> element which holds data using index
113113
const rowFromCol = row.childNodes.item(i);
114114
// Transform data with Pipe and PipeArgs
@@ -125,7 +125,7 @@ export class DataTableDirective implements OnDestroy, OnInit {
125125
colsWithTemplate.forEach(el => {
126126
const { ref, context } = el.ngTemplateRef;
127127
// get <td> element which holds data using index
128-
const i = columns.findIndex(e => e.data === el.data);
128+
const i = columns.filter(c => c.visible !== false).findIndex(e => e.data === el.data);
129129
const cellFromIndex = row.childNodes.item(i);
130130
// reset cell before applying transform
131131
$(cellFromIndex).html('');

0 commit comments

Comments
 (0)