Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

javascript - jQuery DataTables - Ordering dates by hidden column

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...