Skip to content

Commit 1f49101

Browse files
authored
refactor: ensure that rows are always defined internally (#192)
BREAKING CHANGE: The datatable no longer accepts `undefined` as a value of the `count` input. The TypeScript type never allowed `undefined` but it previously worked anyway. Now passing `undefined` will lead to an error. Instead of `undefined` just pass `0` which has the same effect.
1 parent cba69cb commit 1f49101

File tree

5 files changed

+11
-16
lines changed

5 files changed

+11
-16
lines changed

projects/ngx-datatable/src/lib/components/body/body.component.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import { DatatableBodyRowDirective } from './body-row.directive';
7373
(select)="select.emit($event)"
7474
(activate)="activate.emit($event)"
7575
>
76-
@if (rows?.length) {
76+
@if (rows.length) {
7777
<datatable-scroller
7878
[scrollbarV]="scrollbarV"
7979
[scrollbarH]="scrollbarH"
@@ -314,9 +314,6 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
314314
if (val !== this._offset) {
315315
this._offset = val;
316316
if (!this.scrollbarV || (this.scrollbarV && !this.virtualization)) {
317-
if (!isNaN(this._offset) && this.ghostLoadingIndicator) {
318-
this.rows = [];
319-
}
320317
this.recalcLayout();
321318
}
322319
}
@@ -728,7 +725,7 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
728725
* @returns the CSS3 style to be applied
729726
*/
730727
bottomSummaryRowsStyles = computed(() => {
731-
if (!this.scrollbarV || !this.rows || !this.rows.length || !this.rowsToRender()) {
728+
if (!this.scrollbarV || !this.rows.length || !this.rowsToRender()) {
732729
return null;
733730
}
734731

@@ -787,7 +784,7 @@ export class DataTableBodyComponent<TRow extends Row = any> implements OnInit, O
787784
this.rowHeightsCache().clearCache();
788785

789786
// Initialize the tree only if there are rows inside the tree.
790-
if (this.rows && this.rows.length) {
787+
if (this.rows.length) {
791788
const rowExpansions = new Set<TRow>();
792789
if (this.rowDetail) {
793790
for (const row of this.rows) {

projects/ngx-datatable/src/lib/components/body/summary/summary-row.component.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ describe('DataTableSummaryRowComponent', () => {
5656

5757
describe('Visibility', () => {
5858
it('should not be visible when there are no columns', () => {
59+
component.columns = [];
5960
component.rows = rows;
6061
triggerChange();
6162
expect(element.query(By.css('datatable-body-row'))).toBeNull();
6263
});
6364

6465
it('should not be visible when there are no rows', () => {
6566
component.columns = columns;
67+
component.rows = [];
6668
triggerChange();
6769
expect(element.query(By.css('datatable-body-row'))).toBeNull();
6870
});

projects/ngx-datatable/src/lib/components/body/summary/summary-row.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class DataTableSummaryRowComponent implements OnChanges {
5151
summaryRow: any = {};
5252

5353
ngOnChanges() {
54-
if (!this.columns || !this.rows) {
54+
if (!this.columns.length || !this.rows.length) {
5555
return;
5656
}
5757
this.updateInternalColumns();

projects/ngx-datatable/src/lib/components/datatable.component.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export class DatatableComponent<TRow extends Row = any>
119119
* Rows that are displayed in the table.
120120
*/
121121
@Input() set rows(val: TRow[] | null | undefined) {
122-
this._rows = val;
122+
this._rows = val ?? [];
123123
if (val) {
124124
// This will ensure that datatable detects changes on doing like this rows = [...rows];
125125
if (val.length) {
@@ -311,7 +311,7 @@ export class DatatableComponent<TRow extends Row = any>
311311
this._ghostLoadingIndicator = val;
312312
if (val && this.scrollbarV && !this.externalPaging) {
313313
// in case where we don't have predefined total page length
314-
this.rows = [...(this.rows ?? []), undefined]; // undefined row will render ghost cell row at the end of the page
314+
this.rows = [...this.rows, undefined]; // undefined row will render ghost cell row at the end of the page
315315
}
316316
}
317317
get ghostLoadingIndicator(): boolean {
@@ -692,9 +692,9 @@ export class DatatableComponent<TRow extends Row = any>
692692
_limit: number | undefined;
693693
_count = 0;
694694
_offset = 0;
695-
_rows: TRow[] | null | undefined;
695+
_rows: TRow[];
696696
_groupRowsBy: keyof TRow;
697-
_internalRows: TRow[];
697+
_internalRows: TRow[] = [];
698698
_internalColumns: TableColumnInternal<TRow>[];
699699
_columns: TableColumn[];
700700
_subscriptions: Subscription[] = [];
@@ -1056,10 +1056,6 @@ export class DatatableComponent<TRow extends Row = any>
10561056
*/
10571057
calcRowCount(): number {
10581058
if (!this.externalPaging) {
1059-
if (!this.rows) {
1060-
return 0;
1061-
}
1062-
10631059
if (this.groupedRows) {
10641060
return this.groupedRows.length;
10651061
} else if (this.treeFromRelation != null && this.treeToRelation != null) {

src/app/paging/paging-virtual.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import { Employee } from '../data.model';
5050
imports: [DatatableComponent]
5151
})
5252
export class VirtualPagingComponent {
53-
totalElements: number;
53+
totalElements = 0;
5454
pageNumber: number;
5555
rows: Employee[];
5656
cache: Record<string, boolean> = {};

0 commit comments

Comments
 (0)