使用Jquery Datatables对数据排序属性中的值进行自定义排序 [英] Custom sorting on values in a data-sort attribute with Jquery Datatables

查看:648
本文介绍了使用Jquery Datatables对数据排序属性中的值进行自定义排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用Jquery Datatables做一些自定义排序。我不想为每个自定义排序编写自定义排序函数。



我想定义一个值进行排序,并且Datatables忽略原始列值,如果值为



例如:



< td data-sort =111123 > E 1.111,23< / td>



我希望Jquery Datatables可以在 111123



< td data-sort =19801220> 20-12-1980< / td>



我希望Jquery Datatables在 19801220 上以数字方式排列此列。 >

< td>一个字符串< / td>



我想让Jquery Datatables按原始值一个字符串对这一列进行排序。



http://www.datatables.net/plug-ins/sorting 具有接近我想要的隐藏的标题数字排序,但是要求我为每个datatable指定wh这个自定义排序应用的列。我有太多的不同大小的数据表在合理的时间内完成。我只是想让Datatables总是排序这个隐藏的值/ data- *属性,如果它存在。不需要对特定列进行自定义排序定义。



相关: jQuery DataTables:如何按照自定义参数值排序,而不是单元格的内容?但是不幸的是没有回答如何

解决方案

可以使用数据顺序attr,例如

 < table class =table table-bordered table-hover> 
< thead>
< tr>
< th> Date< / th>
< th> Count< / th>
< / tr>
< / thead>
< tbody>
<?php
$ count = 0;
foreach($ users as $ user){?>
< tr>
< td data-order =<?php echo $ count?>>
<?php echo $ user ['createdDate']; ?>
< / td>
< td>
<?php echo $ user ['count']; ?>
< / td>
< / tr>
<?php
$ count ++;
}?>
< tr>
< td data-order =9999999999999999999999999> <! - always last - >
总计
< / td>
< td>
<?php echo count($ users); ?>
< / td>
< / tr>




更多信息 HTML5数据 - *属性


I have to do some custom sorts with Jquery Datatables. I do not want to write custom sort functions for every custom sort.

I want to define a value to sort on and have Datatables ignore the original column values, if value is defined.

For example:

<td data-sort="111123">E 1.111,23</td>

I want Jquery Datatables to sort this column numerically on 111123.

<td data-sort="19801220">20-12-1980</td>

I want Jquery Datatables to sort this column numerically on 19801220.

<td>a string</td>

I want Jquery Datatables to sort this column by its original value a string.

http://www.datatables.net/plug-ins/sorting has "Hidden title numeric sorting" which is close to what I want, but requires me to specify for every datatable on which column this custom sorting applies. I have too many datatables of differing sizes to do this in a reasonable time. I just want to make Datatables always sort this hidden value / data-* attribute if it is present. No need for custom sort definitions on specific columns.

Related: jQuery DataTables: how to sort by custom parameter value rather than the content of the cell? but unfortunately no answer as to how to sort simply by custom parameter, instead pointers to custom sorting scripts.

解决方案

You can use data-order attr, for example

<table class="table table-bordered table-hover">
<thead>
    <tr>
        <th>Date</th>
        <th>Count</th>
    </tr>
</thead>
<tbody>
<?php
   $count = 0;
   foreach($users as $user) {?>
      <tr>
         <td data-order="<?php echo $count ?>">
            <?php echo $user['createdDate']; ?>
         </td>
         <td>
            <?php echo $user['count']; ?>
         </td>
         </tr>
   <?php
      $count++;
   }?>
   <tr>
      <td data-order="999999999999999999999999999"> <!--always last-->
          Total
      </td>
      <td>
         <?php echo count($users); ?>
      </td>
  </tr>

more information HTML5 data-* attributes

这篇关于使用Jquery Datatables对数据排序属性中的值进行自定义排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆