Skip to content

Commit

Permalink
refactor: avoid duplicate sort calls
Browse files Browse the repository at this point in the history
  • Loading branch information
chintankavathia committed Oct 9, 2024
1 parent fa1f611 commit 4a9478b
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions projects/ngx-datatable/src/lib/components/datatable.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,11 @@ export class DatatableComponent<TRow = any>
*/
@Input() set rows(val: TRow[] | null | undefined) {
this._rows = val;

// This will ensure that datatable detects changes on doing like this rows = [...rows];
this.rowDiffer.diff([]);
if (val) {
this._internalRows = [...val];
}

// auto sort on new updates
if (!this.externalSorting) {
this.sortInternalRows();
}

// auto group by parent on new update
this._internalRows = groupRowsByParents(
this._internalRows,
optionalGetterForProp(this.treeFromRelation),
optionalGetterForProp(this.treeToRelation)
);

// recalculate sizes/etc
this.recalculate();

if (this._rows && this._groupRowsBy) {
// If a column has been specified in _groupRowsBy created a new array with the data grouped by that row
this.groupedRows = this.groupArrayBy(this._rows, this._groupRowsBy);
}

this.cd.markForCheck();
}

/**
Expand Down Expand Up @@ -752,10 +731,6 @@ export class DatatableComponent<TRow = any>
* view has been fully initialized.
*/
ngAfterViewInit(): void {
if (!this.externalSorting) {
this.sortInternalRows();
}

// this has to be done to prevent the change detection
// tree from freaking out because we are readjusting
if (typeof requestAnimationFrame === 'undefined') {
Expand Down Expand Up @@ -815,7 +790,7 @@ export class DatatableComponent<TRow = any>
this._internalColumns = translateTemplates(arr);
setColumnDefaults(this._internalColumns);
this.recalculateColumns();
if (!this.externalSorting) {
if (!this.externalSorting && this.rows?.length) {
this.sortInternalRows();
}
this.cd.markForCheck();
Expand Down Expand Up @@ -856,7 +831,8 @@ export class DatatableComponent<TRow = any>
ngDoCheck(): void {
const rowDiffers = this.rowDiffer.diff(this.rows);
if (rowDiffers || this.disableRowCheck) {
if (!this.externalSorting) {
// we don't sort again when ghost loader adds a dummy row
if (!this.ghostLoadingIndicator && !this.externalSorting && this._internalColumns) {
this.sortInternalRows();
} else {
this._internalRows = [...this.rows];
Expand All @@ -869,6 +845,10 @@ export class DatatableComponent<TRow = any>
optionalGetterForProp(this.treeToRelation)
);

if (this._rows && this._groupRowsBy) {
// If a column has been specified in _groupRowsBy create a new array with the data grouped by that row
this.groupedRows = this.groupArrayBy(this._rows, this._groupRowsBy);
}
if (rowDiffers) {
queueMicrotask(() => {
this.recalculate();
Expand Down

0 comments on commit 4a9478b

Please sign in to comment.