[英]How to sort date and string in same column in jquery plugin having file jquery.dataTables.js?
I have a table containing "Date(in format M/d like 'Aug-23')" and string(like 'process' and 'completed') in same column. 我有一个表在同一列中包含“日期(格式为M / d,如'Aug-23')”和字符串(如'process'和'completed')。 But the issue is that it is sorted alphabetically not sort according to Date.
但是问题在于,它不是按日期按字母顺序排序的。 And structure of my file is:
我文件的结构是:
<script type="text/javascript" language="javascript" src="../../table-sorting/js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="../../table-sorting/js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript" class="init">
$(document).ready(function() {
$('#sorting').dataTable( {
"dom": '<"top"iflp<"clear">>rt<"bottom"iflp<"clear">>'
} );
} );
</script>
CPA Shipping
单次转换出价
123 Aug-28
123年8月28日
327 July-30
327年7月30日
789 Process
789过程
if i sort shipping column then it give result like 如果我对运输列进行排序,那么它给出的结果是
Process
处理
Aug-28
8月28日
July-30
7月30日
But i want result like: 但我想要这样的结果:
Process
处理
July-30
7月30日
Aug-28
8月28日
means firstly string sorting and after that date sorting 意味着首先进行字符串排序,然后再进行日期排序
And these shipping values are coming from Mysql database 这些运输价值来自Mysql数据库
How I solve this issue?? 我该如何解决这个问题? Any Suggestion??
有什么建议吗?
You have to write your dates in YYYYmmdd
format so that alphabetical order matches chronological order. 您必须以
YYYYmmdd
格式编写日期,以便字母顺序与时间顺序匹配。
But you want your dates to be displayed in a better-looking format (legit). 但您希望日期以更好看的格式显示(合法)。
The solution is to have 2 columns: one with the good-looking date, one with the YYYYmmdd
date. 解决方案是使用两列:一列带有漂亮日期,一列带有
YYYYmmdd
日期。 This one will be hidden, but will be used by dataTable
sorting function. 这将是隐藏的,但将由
dataTable
排序功能使用。
<table id="sorting">
<thead>
<tr>
<th>CPA</th>
<th>Shipping</th>
<th>Shipping (sorting format)</th>
</tr>
</thead>
<tbody>
<tr>
<td>123</td>
<td>Aug 28</td>
<td>20150828</td>
</tr>
<tr>
<td>327</td>
<td>July 30</td>
<td>20150730</td>
</tr>
<tr>
<td>789</td>
<td>Process</td>
<td>0</td>
</tr>
</tbody>
</table>
Then, the Javascript : 然后,JavaScript:
$('#sorting').dataTable( {
"aoColumns": [
{"bSortable": true}, // First column: normal
{"iDataSort": 2}, // Second column's sorting depends on third column (dataTable starts counting from 0, that's why third column is number 2)
{"bVisible": false}, // Third column: hide it
]
});
You'll find more tips and examples in DataTables' documentation . 您可以在DataTables文档中找到更多提示和示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.