Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the default sort order on the primeng datatable?

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.

like image 957
Heather92065 Avatar asked May 25 '16 20:05

Heather92065


People also ask

How do I turn off sort in PrimeNG table?

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 } ];

How do I sort data in DataTables?

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 ...

What is the default order to sort descending columns by default?

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!

How do I set the Order of data in a table?

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.

Does DataTables display results in ascending or descending order?

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.


2 Answers

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>

like image 143
Heather92065 Avatar answered Oct 10 '22 13:10

Heather92065


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>
like image 41
Bob Avatar answered Oct 10 '22 12:10

Bob