data-order attribute is ignored once columns.data is specified

data-order attribute is ignored once columns.data is specified

WIDNR-TinbergWIDNR-Tinberg Posts: 7Questions: 4Answers: 0

Using HTML-sourced data, I am putting a data-order attribute on some date columns so that they may be sorted chronologically. This works fine until I also start using the columns.data option for the Datatable. So, I have a table row like this:

<tr id="31845074"><td data-order="1973-05-01T00:00:00">05/01/1973</td></tr>

and Javascript like this:

$('#datesTable').DataTable({
    order: [[0, 'desc']],
    columns: [
        { "data": "StartDate" }
    ]
});

Once I specify the columns.data option like above, the data-order is no longer used to sort the dates. I would like to preserve using both options. Can this be done?

Thanks,

Brian

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Hi Brian,

    Yes - what you are seeing is expected. If you define columns.data then all the data will, by default come from that defined location. You can change that using the orthogonal data options - documentation for that here and in columns.data.

    You can do this for your case:

    columns: [
      {
        data: {
          _: 'StartDate',
          type: '@data-order',
          sort: '@data-order' 
        }
      }
    ]
    

    The reason this is required is that DataTables will detect if you are using columns.data and if not do its own auto detection on the cell (attempting to build up something like the above). But if it is there, then it won't overrule you!

    Allan

  • WIDNR-TinbergWIDNR-Tinberg Posts: 7Questions: 4Answers: 0

    Thanks for the solution and explanation, Allan. It works!

This discussion has been closed.