javascript 数据表排序不适用于 dd-mm-yyyy 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31270602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Datatable sorting is not working for dd-mm-yyyy format
提问by Granit Zhubi
I want to sort date on datatable. I want it to do in this format D-M-Y
, but it doesn't work.
我想对数据表上的日期进行排序。我希望它以这种格式执行D-M-Y
,但它不起作用。
When I change the format to Y-M-D
it works. But I need in this format D-M-Y
.
当我将格式更改为Y-M-D
它时。但我需要这种格式D-M-Y
。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#est').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": '<?php echo base_url(); ?>index.php/welcome/tendersdatatable',
"aaSorting": [
[3, "desc"]
],
"bJQueryUI": true,
"sPaginationType": "bootstrap",
"iDisplayStart ": 20,
"oLanguage": {},
"fnInitComplete": function () {
//oTable.fnAdjustColumnSizing();
},
'fnServerData': function (sSource, aoData, fnCallback) {
$.ajax({
'dataType': 'json',
'type': 'POST',
'url': sSource,
'data': aoData,
'success': fnCallback
});
}
});
$('.dataTables_filter input').addClass('form-control').attr('placeholder', 'Search...').css('margin-right', "4%");
$('.dataTables_length select').addClass('form-control');
});
</script>
采纳答案by Granit Zhubi
This one worked for me if anyone need's it . https://datatables.net/forums/discussion/2467/need-help-for-sorting-date-with-dd-mm-yyyy-format
如果有人需要的话,这个对我有用。 https://datatables.net/forums/discussion/2467/need-help-for-sorting-date-with-dd-mm-yyyy-format
回答by Shakeeb Ahmad
回答by Gyrocode.com
There are sorting plug-insavailable, however none of them except datetime-momentsupports the format you need. But datetime-moment
plug-in has dependency with moment.js.
有可用的排序插件,但是除了datetime-moment之外,没有一个插件支持您需要的格式。但是datetime-moment
插件依赖于moment.js。
However it could be done by defining a custom sorting method date-dmy
before you initialize your data table.
但是,可以通过date-dmy
在初始化数据表之前定义自定义排序方法来完成。
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-dmy-pre": function ( a ) {
if (a == null || a == "") {
return 0;
}
var date = a.split('-');
return (date[2] + date[1] + date[0]) * 1;
},
"date-dmy-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"date-dmy-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );
To use this custom type, you need to use aoColumnDefs
to set desired column type as shown below. I'm using index 0
to set type of the first column. Other initialization options are omitted for simplicity.
要使用此自定义类型,您需要使用aoColumnDefs
设置所需的列类型,如下所示。我正在使用索引0
来设置第一列的类型。为简单起见,省略了其他初始化选项。
$('#example').dataTable( {
"aoColumnDefs": [
{ "sType": "date-dmy", "aTargets": [ 0 ] }
]
} );
回答by matias salgado
You don't need a plugin. You can sort it adding a hidden element.
你不需要插件。您可以对其进行排序,添加一个隐藏元素。
Convert the date to the format YYYYMMDD and prepend to the actual value (DD/MM/YYYY) in the , wrap it in an element, set style display:none; to the elements. Now the date sort will work as a normal sort. The same can be applied to date-time sort.
将日期转换为 YYYYMMDD 格式并附加到 中的实际值 (DD/MM/YYYY),将其包装在一个元素中,设置样式 display:none; 到元素。现在日期排序将作为正常排序工作。这同样适用于日期时间排序。
HTML
HTML
<table id="data-table">
<tr>
<td><span>YYYYMMDD</span>DD/MM/YYYY</td>
</tr>
</table>
If you are using rails as me the sintaxys will be like this:
如果您像我一样使用 rails,那么 sintaxys 将是这样的:
<td>
<span>
<%= youmodel.created_at.strftime("%Y%m%d") %>
</span>
<%= youmodel.created_at.strftime("%d/%m/%Y") %>
</td>
CSS
CSS
#data-table span {
display:none;
}
回答by AamerMalik
This worked for me
这对我有用
jQuery.fn.dataTableExt.aTypes.unshift(
function (sData) {
if (sData !== null && sData.match(/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20|21)\d\d$/)) {
return 'date-uk';
}
return null;
}
);
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"date-uk-pre": function (a) {
if (a == null || a == "") {
return 0;
}
var ukDatea = a.split('/');
return (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
},
"date-uk-asc": function (a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"date-uk-desc": function (a, b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
});