简体   繁体   中英

jQuery DataTables: display rows in the original data source order, while keeping sorting feature available

I wish to display a list of items in the order in which they are returned from my data source initially but still give the user the ability to sort on columns if they so wish.

In order to do this, i set the order attribute to false like so:

$('#table_id').DataTable({
    order: false;
 });

What this does however is hide the up/down caret symbols effectively disabling sorting. It seems that they only appear if you set order to an array of arrays (like [[1, "asc"]] for example).

I have looked into the bSort attribute but that doesnt seem to work..

Any ideas on how i can display a list in the order in which it comes first?

Note: the datasource is a web service which returns a block of html that has the desired order of elements.

You need to set order option, which defines initial sorting order (in form of array ), to empty array, so that it will keep your data entries in their original order, while letting the users to sort the table afterwards:

$('#table_id').DataTable({
    order: [];
});

For older version use this

$(document).ready( function() {
    $('#example').dataTable({
        /* Disable initial sort */
        "aaSorting": []
    });
})

newer versions

$(document).ready( function() {
    $('#example').dataTable({
        /* No ordering applied by DataTables during initialisation */
        "order": []
    });
})

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