Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primeng Datatable rowTrackBy

I have searched the web for a decent example of primeng datatable with rowTrackBy. Documentation is incomplete and does not say much. Is there anyone out there that can help?

like image 426
pantonis Avatar asked Dec 23 '22 13:12

pantonis


2 Answers

Just wanted to show where and how to use rowTrackBy in table template as well in this answer:

<p-table #tt [value]="data" [lazy]="true" (onLazyLoad)="loadDataLazily($event)" [paginator]="true"
                            [rows]="dataSize" [totalRecords]="totalRecords" 
                            [rowsPerPageOptions]="[10,20,30]" [rowTrackBy]="trackByFunction">

trackByFunction = (index, item) => {
    return item.id // O index
}
like image 81
DirtyMind Avatar answered Dec 28 '22 10:12

DirtyMind


It's very similar to the trackBy function in ngFor. You just need to provide a function with the index and the item as arguments and you return what you are tracking by. For example

trackByFunction = (index, item) => {
    return item.id // O index
}
like image 27
Carlos Angarita Avatar answered Dec 28 '22 09:12

Carlos Angarita