Datatables not using data-order

Datatables not using data-order

mknudsenmknudsen Posts: 5Questions: 2Answers: 0

We have an internal system built with DataTables and Bootstrap - and everything works perfectly except sort of date columns (DD-MM-YYYY format). We have chosen to use the option to add 'data-order' (date in numeric format) to each row, but we can't get it to work :-/

Is it posible to check, if DataTables is using the 'data-order' or the column data for sorting?

It's working on the live test system with some of our data:
http://live.datatables.net/woxeneju/1/edit

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    Your example appears to work for me unless I'm missing something - it is shown in the following order when I click on the name column:

    • 12-28-2015 19:17:48
    • 12-31-2015 19:11:48
    • 01-29-2016 20:16:57

    Allan

  • mknudsenmknudsen Posts: 5Questions: 2Answers: 0

    Hi Allan, and thanks for your fast reply.

    "It's working on the live test system with some of our data...", but I'm looking for an option to check/debug if our internally system is using the 'data-order' or not.

    Do you know how I can achieve this?

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    Sorry - I missed that bit. To check use data() and check what the data structure that DataTables has for the rows.

    Allan

  • mknudsenmknudsen Posts: 5Questions: 2Answers: 0
    edited January 2016

    Hi Allan, with your help I achieved to come one step closer to understand why my sorting didn't work, but it also give me some new problems. If I remove the following the date sorting works:

    columns: [
          { data: "name" },
          { data: "position" },
          { data: "office" },
          { data: "age" },
          { data: "start_date" },
          { data: "salary" }
        ]
    
    • but we are using the following line, to return a object with the search result:
    var data = dtTable.rows( {filter: 'applied'} ).data().toArray();
    ...
    for (var i = 0, l = data.length; i < l; i++) {
        sum += parseInt(data[i]["age"]);
    };
    ...
    
    • and without the columns definition, my function to receive values by 'name' won't work.

    Can you suggest another solution or have I found a error in dataTables?

    Updated live test:
    http://live.datatables.net/woxeneju/6/edit?html,js,console,output

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin
    Answer ✓

    You could use data[i][3] and not define the column data into objects.

    Otherwise you'd need to use a statement such as the following for the age column:

    {
      _: "age.display",
      sort: "age.@data-order",
      type: "age.@data-order"
    }
    

    Its a bit messy, but that is basically what DataTables does internally. I will look to see if the automatic detection can be improved when using objects.

    Allan

  • mknudsenmknudsen Posts: 5Questions: 2Answers: 0

    Thanks Allan, I will be using the data[i][3] notation, but it will require some extra comments in my code :-P

    Keep up the good work.

This discussion has been closed.