2

I have been working for a couple of days with DataTables and I have this task: I need to disable the initial sorting and filter the first column which contains dates like Aug 15 depending on the fourth one (2015.08.15), which will be hidden.

For example, if I have:

Aug 15    |  2015.08.15
Aug 7     |  2015.08.07
Aug 3     |  2015.08.03
Aug 20    |  2015.08.20

In ascending sort I should get:

Aug 3     |  2015.08.03
Aug 7     |  2015.08.07
Aug 15    |  2015.08.15
Aug 20    |  2015.08.20

But I get the alphabetical sort:

Aug 15    |  2015.08.15
Aug 20    |  2015.08.20
Aug 3     |  2015.08.03
Aug 7     |  2015.08.07

My first code was something like:

$("#TableBt" + rid).DataTable({
  "aaSorting": [],
  "columns": [
    null,
    null,
    {
      "title": lC2
    },
    {
      "visible": false
    }]

This disabled my initial sorting, but it alphabetical sort my date column (the first and visible one).

After some research, I changed the code like this:

$("#TableBt" + rid).dataTable({
  "asSorting": [],
  "aoColumnDef": [
    {
      "iDataSort": 3,
      "aTargets": [4]
    },
    null,
    {
      "sTitle": lC2
    },
    {
      "bVisible": false,
      "aTargets": [3]
    }]
});

But now all the columns are visible, the initial sorting is again enable and the date sort works only alphabetical.

What am I doing wrong?

1 Answer 1

10

SOLUTION

You need to use columnDefs to target first column (targets: 0) and define the column which data would be used for sorting of the first column with orderData. Also you need to hide forth column (targets: 3) with visible: false.

$("#TableBt" + rid).DataTable({
   columnDefs: [
       { targets: 0, orderData: 3 },
       { targets: 3, visible: false }    
   ]
});

DEMO

See this jsFiddle for code and demonstration.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.