Search code examples
primengprimeng-turbotable

PrimeNG p-table Lazy loading and Multi Sort - Goes in Loop - Maximum call stack size exceeded


Stackblitz: https://stackblitz.com/edit/primeng-p-table-multisort

Config:
"@angular/cli": "~7.0.2", "primeng": "7.0.5",

I have a PrimeNG p-table implemented with lazy loading. Need multi column sort added to it.

Sample code from above Stackblitz link.

<p-table [columns]="cols" [value]="cars1" [lazy]="true" [lazyLoadOnInit]="false" (onLazyLoad)="loadList($event)" sortMode="multiple" [multiSortMeta]="multiSortMeta">

This works properly if its single sort mode.

Getting error as ERROR RangeError: Maximum call stack size exceeded.

It should be a simple implementation but not able to understand what is missing here or this is not supported by PrimeNG.

Any help is appreciated.


Solution

  • Updated Stackblitz: https://stackblitz.com/edit/primeng-p-table-multisort

    It worked with overriding the PrimeNG Table - sortMultiple method via prototype chain.

    Old code:

    Table.prototype.sortMultiple = function () {
        var _this = this;
        if (this.multiSortMeta) {
            if (this.lazy) {
                this.onLazyLoad.emit(this.createLazyLoadMetadata());
            }
            else if (this.value) {
                if (this.customSort) {
                    this.sortFunction.emit({
                        data: this.value,
                        mode: this.sortMode,
                        multiSortMeta: this.multiSortMeta
                    });
                }
                else {
                    this.value.sort(function (data1, data2) {
                        return _this.multisortField(data1, data2, _this.multiSortMeta, 0);
                    });
                }
                if (this.hasFilter()) {
                    this._filter();
                }
            }
            this.onSort.emit({
                multisortmeta: this.multiSortMeta
            });
            this.tableService.onSort(this.multiSortMeta);
        }
    };  
    

    New code:

    Table.prototype.sortMultiple = function () {
        const _this = this;
        if (this.multiSortMeta) {
            if (this.lazy) {
                // additional conditions added
                if (this.lazyLoadOnInit || (!this.lazyLoadOnInit && this.initialized)) {
                    this.onLazyLoad.emit(this.createLazyLoadMetadata());
                }
            } else if (this.value) {
                if (this.customSort) {
                    this.sortFunction.emit({
                        data: this.value,
                        mode: this.sortMode,
                        multiSortMeta: this.multiSortMeta
                    });
                } else {
                    this.value.sort(function (data1, data2) {
                        return _this.multisortField(data1, data2, _this.multiSortMeta, 0);
                    });
                }
                if (this.hasFilter()) {
                    this._filter();
                }
            }
            this.onSort.emit({
                multisortmeta: this.multiSortMeta
            });
            this.tableService.onSort(this.multiSortMeta);
        }
    };