gpt4 book ai didi

javascript - 如何通过 HTML5 'data-order' 属性对 jQuery DataTables 中的日期列进行排序?

转载 作者:行者123 更新时间:2023-12-02 23:11:30 24 4
gpt4 key购买 nike

我使用 jQuery 插件并通过 JSON 数组传递数据。在我的表格中,我有一个 Date 列,该列具有不同的日期格式,例如“Mon , 02 Sep 2019”,因此排序无法正确处理该日期列。

我有read关于 HTML5 data-sort 属性,这是他们在文档中提到的数据表代码,但我不知道如何将这个 data-sort 属性与 JSON 数组一起使用.

到目前为止我的代码是:

$(document).ready(function() {
$('#example').DataTable({
"order":[],
data : [['TEST','Develper','ABC','25','Mon , 05 Aug 2019','100000'],['TEST','Develper','ABC','25','Tue , 06 Aug 2019','100000'],['TEST','Develper','ABC','25','Mon , 02 Sep 2019','100000'],['TEST','Develper','ABC','25','Mon , 14 Oct 2019','100000'],['TEST','Develper','ABC','25','Mon , 04 Nov 2019','100000'],['TEST','Develper','ABC','25','Mon , 01 Nov 2020','100000']]
});
} );
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.18/rg-1.1.0/datatables.min.css" />
<script type="application/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.18/rg-1.1.0/datatables.min.js"></script>

<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
</table>

最佳答案

我可能会弄错,但如果您使用 HTML 源提供数据表,您可能会受益于 HTML5 属性 data-sort/data-order

考虑到您的源是 JSON 中的数组,我可能建议使用自定义数据类型排序:

  • 首先,您在特定列的列定义中指定自定义日期类型(例如“mydate”):
$('table').DataTable({
...
columnDefs: [{
targets: 4,
type: 'mydate'
}]
});
  • 接下来,您为该类型指定自定义排序算法:
Object.assign($.fn.DataTable.ext.oSort, {
'mydate-asc': (a,b) => new Date(a) - new Date(b),
'mydate-desc': (a,b) => new Date(b) - new Date(a)
});

因此,您的完整示例可能如下所示:

//your sample source data
const srcData = [{name:'TEST',position:'Develper',office:'ABC',age:25,startDate:'Mon , 05 Aug 2019',salary:100000},{name:'TEST',position:'Develper',office:'ABC',age:25,startDate:'Tue , 06 Aug 2019',salary:100000},{name:'TEST',position:'Develper',office:'ABC',age:25,startDate:'Mon , 02 Sep 2019',salary:100000},{name:'TEST',position:'Develper',office:'ABC',age:25,startDate:'Mon , 14 Oct 2019',salary:100000},{name:'TEST',position:'Develper',office:'ABC',age:25,startDate:'Mon , 04 Nov 2019',salary:100000},{name:'TEST',position:'Develper',office:'ABC',age:25,startDate:'Mon , 01 Nov 2020',salary:100000}];

//datatables init
$('table').DataTable({
dom: 't',
data: srcData,
columns: ['name','position','office','age','startDate','salary'].map(header => ({data:header,title:header})),
columnDefs: [{
targets: 4,
type: 'mydate'
}]
});

//custom sorting
Object.assign($.fn.DataTable.ext.oSort, {
'mydate-asc': (a,b) => new Date(a) - new Date(b),
'mydate-desc': (a,b) => new Date(b) - new Date(a)
});
<!doctype html><html><head><link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.18/rg-1.1.0/datatables.min.css" /><script type="application/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script><script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.18/rg-1.1.0/datatables.min.js"></script></head><body><table></table></body></html>

关于javascript - 如何通过 HTML5 'data-order' 属性对 jQuery DataTables 中的日期列进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57344592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com