简体   繁体   English

如何使用单独的列对 jQuery DataTables 表进行排序?

[英]How to sort a jQuery DataTables table using a separate column?

Is it possible to sort a DataTables table using a separate column?是否可以使用单独的列对 DataTables 表进行排序? In the example below, the table is sorted using the first column by default, which is also hidden.在下面的示例中,默认情况下使用第一列对表格进行排序,该列也是隐藏的。 Even though the third column is a date column, it is not in a numerical format.即使第三列是日期列,它也不是数字格式。 When trying to sort the table using the third column, it is sorting alphabetically rather than by date.当尝试使用第三列对表格进行排序时,它是按字母顺序而不是按日期排序。

How can the table be sorted by date correctly using the third column?如何使用第三列正确按日期对表格进行排序? Is it possible to sort the table using the hidden first column when toggling the third column?切换第三列时是否可以使用隐藏的第一列对表格进行排序?

 $('#table').DataTable({ responsive: true, "order": [[0, "desc"]], "columnDefs": [ { "targets": [0], "visible": false, "searchable": false } ] });
 <link href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.js"></script> <table id="table"> <thead> <tr> <th>Numerical date</th> <th>Description</th> <th>String format date</th> </tr> <thead> <tbody> <tr> <td>20200801</td> <td>Record 1</td> <td>August 1, 2020</td> </tr> <tr> <td>20200701</td> <td>Record 2</td> <td>July 1, 2020</td> </tr> <tr> <td>20200501</td> <td>Record 3</td> <td>May 1, 2020</td> </tr> <tr> <td>20200401</td> <td>Record 4</td> <td>April 1, 2020</td> </tr> </tbody> </table>

You can add the following.您可以添加以下内容。 You should be able to sort by date correctly:您应该能够正确按日期排序:

"aoColumns": [{},{},{"bSortable": true, "sType": "date"}] 

See it in action in the demo below:在下面的演示中查看它的实际效果:

 $('#table').DataTable({ responsive: true, "order": [[2, "desc"]], "columnDefs": [ { "targets": [0], "visible": false, "searchable": false } ], "aoColumns": [{},{},{"bSortable": true, "sType": "date"}] });
 <link href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.js"></script> <table id="table"> <thead> <tr> <th>Numerical date</th> <th>Description</th> <th>String format date</th> </tr> <thead> <tbody> <tr> <td>20200801</td> <td>Record 1</td> <td>August 1, 2020</td> </tr> <tr> <td>20200701</td> <td>Record 2</td> <td>July 1, 2020</td> </tr> <tr> <td>20200501</td> <td>Record 3</td> <td>May 1, 2020</td> </tr> <tr> <td>20200401</td> <td>Record 4</td> <td>April 1, 2020</td> </tr> </tbody> </table>

You can give tds a data-sort attribute.你可以给 tds 一个数据排序属性。 Then you wouldn't need the first column at all.那么你根本不需要第一列。

 $('#table').DataTable({ responsive: true, "order": [ [1, "desc"] ] });
 <link href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.js"></script> <table id="table"> <thead> <tr> <th>Description</th> <th>String format date</th> </tr> <thead> <tbody> <tr> <td>Record 1</td> <td data-sort="20200801">August 1, 2020</td> </tr> <tr> <td>Record 2</td> <td data-sort="20200701">July 1, 2020</td> </tr> <tr> <td>Record 3</td> <td data-sort="20200501">May 1, 2020</td> </tr> <tr> <td>Record 4</td> <td data-sort="20200401">April 1, 2020</td> </tr> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM