Forum Discussion

FICO's avatar
FICO
Community Manager
6 years ago

vdl-table default column sort

vdl-table is sorted ascending by the first column by default and the user can easily change this by clicking the column they want to sort by, but how can I change the default sort so all users initial have the table sorted by the n-th column? In other words, how can I set the underlying Datatables.js "order" parameter?

3 Replies

  • FICO's avatar
    FICO
    Community Manager

    Alex,

    You can use the aaSorting option to change the default sort.

    function ascSort(tableOptions) {
        tableOptions.overrides.aaSorting = [[0, 'asc'],[1, 'asc']];
        return tableOptions;
    }
     
    <vdl-table modifier="=ascSort">
        <vdl-table-column entity="TestArray"/>
    </vdl-table>

    You only need to specify the columns you want to sort in the aaSorting definition.

    If this is a table created with vdl-repeat then count the columns in what you see in the UI and use that.

  • FICO's avatar
    FICO
    Community Manager

    Thanks @Libin Varghese​, I've tried implementing that but don't see the sort change. Looks like the syntax is from legacy Datatables.js (https://legacy.datatables.net/ref). I think the latest FICO Insight uses a newer Datatables.js version with the 'order' syntax rather than 'aaSorting'. Making that one change to your ascSort function worked for me.

  • FICO's avatar
    FICO
    Community Manager

    You are right. The 'aaSorting' was for the older VDL version 3.1. For the latest version you do need to use 'order'. Here is the modified code that should work.

    function ascSort(tableOptions) {
        tableOptions.overrides.order = [[0, 'asc'],[1, 'asc']];
        return tableOptions;
    }
     
    <vdl-table modifier="=ascSort">
        <vdl-table-column entity="TestArray"/>
    </vdl-table>

    By default, Insight saves the state of the Datatable. If a user changes the sort and later comes back to the view, they would see the same sort as they set the last time. To turn this off, add the below override as well.

    tableOptions.overrides.stateSave = false;