简体   繁体   中英

Column default sorting arrows reordering

I have a JQuery Datatable which I am default sorting based on the first column in descending order.

The table itself works just fine but I am trying to change the behaviour of the column reordering arrow buttons once the table has been drawn, which I have circled in red in the screenshot below:

我要更改的列重新排序行为

When I click these small arrow buttons, the table is reordered in ascending order first for the column I clicked in. So for example if I click once on the arrow for the Date Created (UK) column, the table is reordered in ascending order for these dates (the oldest date, which was added first, appears at the top of the table), which is not what I want:

我现在有的行为

What I am looking for is for the table to be ordered in descending order on the first click of that column, instead of having to click it twice (once to get in ascending and once again for descending). Basically I'm trying to get this result on the first click, but I am getting it on the second click only:

第一次点击时我想要的行为

Is there a simple setting I can add to my table code to change the behaviour of the column's arrow reordering? I had a look through their API and some past StackOverflow questions but couldn't find anything relevant to my problem.

Here is my code if you want to have a look:

self.table = table_dom.DataTable({
    serverSide: true,
    dom: "<'row'<'col-md-3'l><'col-md-5 text-right'B><'col-md-4'f>>" +
        "<'row'<'col-md-12'tr>>" +
        "<'row'<'col-md-5'i><'col-md-7'p>>",
    ajax: '...',
    columns: [
        {data: 'legacy_description', name: 'legacy_description', title: 'P', width: '20px'},
        ...
        {data: 'created', name: 'created', title: 'Date Created (UK)', width: '150px'},
    ...
    ],
    order: [[0, 'desc']],
    responsive: true,
    pageLength: 10,
    ...
});

Thanks in advance.

Use columns.orderSequence to customize order sequence for each column.

For example, to sort third column in descending order first, use the code below:

$('#example').DataTable( {
  "columnDefs": [
    { "orderSequence": [ "desc", "asc" ], "targets": [ 2 ] }
  ]
} );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM