I am using the prime-ng dataTable component to display a list of users. I would like this list to be sorted by the first column descending by default and have the dataTable display the first column as sorted.
<p-dataTable [value]="webUserSummaryList" [rows]="10" reorderableColumns="true">
<p-column field="userName" header="Username" [filter]="true" [sortable]="true"></p-column>
<p-column field="emailAddress" header="Email" [filter]="true" [sortable]="true"></p-column>
<p-column field="firstName" header="First Name" [filter]="true" [sortable]="true"></p-column>
<p-column field="lastName" header="Last Name" [filter]="true" filterMatchMode="contains" [sortable]="true"></p-column>
</p-dataTable>
Edit: I have figured out one way to set the default sort column is to use sortField="userName". However, I still can't get the column to default to descending order.
Bookmark this question. Show activity on this post. cols = [ { field: 'name', header: 'Name', sort: true }, { field: 'age', header: 'Age', sort: true }, { field: 'gender', header: 'Gender', sort: false }, { field: 'status', header: 'Status', sort: false } ];
Default ordering (sorting) With DataTables you can alter the ordering characteristics of the table at initialisation time. Using the order initialisation parameter, you can set the table to display the data in exactly the order that you want. The order parameter is an array of arrays where the first value of the inner array is the column ...
The docs also show the default value is [ [0, 'asc']]. If you want the first column to sort descending by default then use order: [ [0, 'desc']],. @kthorngren That second snippet worked, THANK YOU!
Using the order initialisation parameter, you can set the table to display the data in exactly the order that you want. The order parameter is an array of arrays where the first value of the inner array is the column to order on, and the second is 'asc' (ascending ordering) or 'desc' (descending ordering) as required.
We are using DataTables to display the contents of a database query. I have the sort order of the query set to descending (based on the field "ID"), however, the form always displays the results in ascending order when it first loads.
I figured it out. These two attributes should be added:
sortField="userName" [sortOrder]="-1"
The sortField matches the column name and the sortOrder can be either 1 for ascending and -1 for descending.
Here's the working solution:
<p-dataTable [value]="webUserSummaryList" [rows]="10" reorderableColumns="true" sortField="userName" sortOrder="-1">
<p-column field="userName" header="Username" [filter]="true" [sortable]="true"></p-column>
<p-column field="emailAddress" header="Email" [filter]="true" [sortable]="true"></p-column>
<p-column field="firstName" header="First Name" [filter]="true" [sortable]="true"></p-column>
<p-column field="lastName" header="Last Name" [filter]="true" filterMatchMode="contains" [sortable]="true"></p-column>
If you sort on multiple columns, you can parameter the initial sorting with (working on deprecated DataTable and on the current PrimeNG Table Component):
[multiSortMeta]="[{field: 'state', order: -1}, {field: 'displayName', order: 1}]"
Example with Table component (PrimeNG 7+):
<p-table [value]="products2" sortMode="multiple" [multiSortMeta]="[{field: 'code', order: -1}, {field: 'name', order: 1}, {field: 'category', order: -1}]">
<ng-template pTemplate="header">
<tr>
<th pSortableColumn="code">Code <p-sortIcon field="code"></p-sortIcon></th>
<th pSortableColumn="name">Name <p-sortIcon field="name"></p-sortIcon></th>
<th pSortableColumn="category">Category <p-sortIcon field="category"></p-sortIcon></th>
<th pSortableColumn="quantity">Quantity <p-sortIcon field="quantity"></p-sortIcon></th>
<th pSortableColumn="price">Price <p-sortIcon field="price"></p-sortIcon></th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-product>
<tr>
<td>{{product.code}}</td>
<td>{{product.name}}</td>
<td>{{product.category}}</td>
<td>{{product.quantity}}</td>
<td>{{product.price | currency: 'USD'}}</td>
</tr>
</ng-template>
</p-table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With