简体   繁体   中英

datatables sort by ticks and show date

I'm using Jquery dataTable. doing ajax call to the server to get json array with data:

[{"ticks":635020621354830000,"created":"20/04/13 13:42","action":"1","reference":"1444","fee":"0.6"},{"ticks":635023360450070000,"created":"23/04/13 17:47","action":"0","reference":"1503","fee":"0.6"},{"ticks":635023360461470000,"created":"23/04/13 17:47","action":"0","reference":"1505","fee":"0.6"}]

then I build a table with javascript and call $('#mytable').dataTable({...options}); I want to show first column the "created" data, but to sort it by the "ticks". How can I do it ? the reason for it so there are many entries in the json array that the "created" field is the same value in "dd/MM/yy hh:mm" format (I don't want to show milliseconds) and the ticks are different.

Return the ticks as you do now, but hide them. Use the iDataSort parameter to tell the date column to sort using the hidden column.

See this jsfiddle: http://jsfiddle.net/bFpmJ/

In the demo, click on the Column 0 header. Column 0 values are all the same date, but the table will sort correctly. Column 1 labels which is earliest/latest in agreement with the hidden column.

Here is the relevant code:

jQuery('#myTable').dataTable(
    { 
    "aoColumns": [
        { "sType":"string", "bSearchable": false, "bVisible":    false },
        { "iDataSort": 0 },
            null,
            null,
            null
        ] 
    }
    );

Note the first column is made invisible and non-searchable. Also, because of the length of the number, the default DataTables sorting wasn't working (perhaps it can't handle a long of that length, I'm not sure) - you can see this by just removing the "sType":"string" and then trying to sort by Column 0. So I had to change the "sType" to "string". Assuming the ticks are always the same number of digits, this shouldn't be a problem.

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