【问题标题】:Sort column by value other than cell content按除单元格内容以外的值对列进行排序
【发布时间】:2016-12-25 09:17:18
【问题描述】:

我正在使用 jQuery DataTables,我有一列如下所示:

<td><span class="badge"> 123 </span>  <span> customer name </span></td>

即,我先输入一些数字 (ID),然后输入我想要排序的实际名称。

如何告诉 jQuery DataTables 按客户名称正确排序?

【问题讨论】:

  • 你为什么不格式化为:“客户名称 123”,我认为没有一种简单的方法可以按照你想要的方式进行
  • 我以前做过,但看起来很糟糕(数字需要对齐)。当我切换 td 中元素的顺序时,排序确实有效。无论如何,我更喜欢名字前的数字..
  • 好吧,如果你需要...我想你可以从:datatables.net/plug-ins/sortingdatatables.net/development/sorting开始

标签: javascript jquery sorting datatables


【解决方案1】:

td 元素上使用data-order 属性,如this example 所示。

<td data-order="customer name">
   <span class="badge"> 123 </span>  
   <span> customer name </span>
</td>

【讨论】:

  • 这就是我要找的。现在我尝试对 JSON ajax 数据做同样的事情(对于不同的表)。对于我的 DOM 表,它确实有效。
【解决方案2】:

你可以用 jQuery 做到这一点。

<table id="example" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>12</td>
            <td data-search>Paul</td>
        </tr>
        <tr>
            <td>13</td>
            <td data-search>Nickson</td>
        </tr>
    </tbody>
 </table>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf8" src="jquery.dataTables.min.js"></script>
<script>
     $(document).ready(function() {
         $('#example').DataTable();
     } );
</script>

您必须在包含 jquery 后添加 jquery.dataTables.min.js。你可以找到更多关于这个here

【讨论】:

    猜你喜欢
    • 2021-12-11
    • 2014-12-09
    • 2011-03-13
    • 1970-01-01
    • 2021-01-04
    • 2016-11-21
    • 1970-01-01
    • 2022-06-26
    • 1970-01-01
    相关资源
    最近更新 更多