数据表排序时间列 [英] Datatables sorting time column
本文介绍了数据表排序时间列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用jQuery DataTables,并且在对这种格式为mm:ss
的时间列进行排序时遇到问题.例如,当我对该00:08
进行排序时,该排序会执行某些操作,但这并不好.我在专栏中:
I'm using jQuery DataTables and I have a problem when I'm sorting my time column with this format mm:ss
. For example when I sort this 00:08
the sort does something but this is not good. I have in my column:
00:08
00:15
00:01
01:20
00:16
02:11
因此排序无效.您知道如何对时间栏进行排序吗?
So the sort doesn't work. Do you know how can I sort my time column?
这是我的代码:
$('#table').DataTable({
dom: "t<'col-sm-5'i><'col-sm-7'p>",
autoWidth: false,
serverSide: true,
aaSorting: [[0, 'desc']],
rowId: 'id',
lengthChange: false,
ajax: {
url: 'index',
method: 'POST'
}
columns: [
{data: "id", width: '5%'},
{data: "name", width: '10%', orderData: [ 1, 0 ]},
{data: "user_name", width: '10%', orderData: [ 2, 0 ]},
{data: "email", width: '35%', orderData: [ 3, 0 ]},
{data: "duration", render: duration_time, width: '10%', type: "time",orderData: [ 4, 0 ]},
{data: "incomplete", render: incomplete, width: '30%', orderData: [ 5, 0 ]}
]
});
这是render参数的功能:
Here is the function for the render parameter :
function duration_time(data, type, dataToSet){
var start = dataToSet.date_start;
var end = dataToSet.date_end;
var time = moment.utc(moment(end, "YYYY-MM-DD HH:mm:ss").diff(moment(start, "YYYY-MM-DD HH:mm:ss"))).format("mm:ss");
return time;
}
function incomplete(data, type, dataToSet){
return dataToSet.incomplete == 0 ? 'Complete' : 'Incomplete';
}
推荐答案
您必须使用排序插件ins .
它使您不仅可以根据类型对列进行排序,而且还可以根据数据进行排序.
It allows you to sort the columns based on type and not only on data.
您的代码将如下所示:
<script type="text/javascript" src="jquery.dataTables.js"></script>
<script type="text/javascript" src="dataTables.numericComma.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable( {
"columnDefs": [
{ "type": "time", targets: 3 }
]
} );
} );
</script>
这篇关于数据表排序时间列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文