简体   繁体   English

如何在具有文件jquery.dataTables.js的jquery插件的同一列中对日期和字符串进行排序?

[英]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.

相关问题 将JSON字符串转换为JSON对象,因此无需在基本jquery.dataTables.js中进行更改 - Convert JSON string to JSON object so I don't need to change in the base jquery.dataTables.js jquery.dataTables.js:无法使用 Svelte 读取未定义的属性 - jquery.dataTables.js: Cannot read properties of undefined with Svelte jQuery DataTables插件:排序德国日期 - jQuery DataTables plugin: Sort German date 获取错误:未捕获的语法错误:jquery-ui.js和jquery.dataTables.js中出现意外的令牌&lt; - Getting Error : Uncaught SyntaxError: Unexpected token < in jquery-ui.js and jquery.dataTables.js jQuery dataTables插件-如何从列排序中忽略单词 - jQuery dataTables plugin - how to ignore word from column sort 使用jquery.dataTables.js 1.10.11,该表最初不会显示对表的添加 - Using jquery.dataTables.js 1.10.11, the table doesn't initially show additions to tables 插件DataTables jquery:如何显示格式化的Date? - plugin DataTables jquery : how display formated Date? 如何对 jQuery DataTables 中的数字列进行排序 - How to sort on a numeric column in jQuery DataTables jQuery Datatables - 如何按列进行编程排序 - jQuery Datatables - how to programatically sort by a column 如何在 jQuery DataTables 中对文件大小进行排序 - How to sort file sizes in jQuery DataTables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM