使用Jquery Datatables对data-sort属性中的值进行自定义排序

时间:2021-05-02 14:25:27

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

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

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

我想定义一个要排序的值,并且如果定义了值,则Datatables会忽略原始列值。

For example:

例如:

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

E 1.111,23

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

我希望Jquery Datatables在111123上以数字方式对该列进行排序。

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

20-12-1980

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

我希望Jquery Datatables在19801220以数字方式对该列进行排序。

<td>a string</td>

一个字符串

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

我希望Jquery Datatables通过其原始值对字符串进行排序。

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.

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

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.

相关:jQuery DataTables:如何按自定义参数值而不是单元格的内容进行排序?但遗憾的是,没有回答如何简单地通过自定义参数进行排序,而是指向自定义排序脚本。

2 个解决方案

#1


18  

You can use data-order attr, for example

例如,您可以使用数据顺序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="999999999999999999999999999"> <!--always last-->
          Total
      </td>
      <td>
         <?php echo count($users); ?>
      </td>
  </tr>

more information HTML5 data-* attributes

更多信息HTML5 data- *属性

#2


13  

I found an easy solution that works for me and does not require too much code or changes to datatables.js.

我找到了一个适合我的简单解决方案,并且不需要太多代码或更改datatables.js。

It is very similar to the requirements of the question, but not exactly the same.

它与问题的要求非常相似,但并不完全相同。

Instead of data-sort you can use a HTML comment tag.

您可以使用HTML注释标记代替数据排序。

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

E 1.111,23

becomes

<td><!-- 000000111123 -->E 1.111,23</td>

<! - 000000111123 - > E 1.111,23

By zero-padding an int they will be sorted as a string. The zero-padding makes the sort behave as you'd expect: sorting the integers from high to low.

通过零填充int,它们将被排序为字符串。零填充使排序的行为与您期望的一样:将整数从高到低排序。

Solution works for dates, ints and strings. For dates and ints you can use a scripting language to output them in the way you want (eg: zero-padded, formatted as yyyy-mm-dd).

解决方案适用于日期,整数和字符串。对于日期和整数,您可以使用脚本语言以您想要的方式输出它们(例如:零填充,格式化为yyyy-mm-dd)。

#1


18  

You can use data-order attr, for example

例如,您可以使用数据顺序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="999999999999999999999999999"> <!--always last-->
          Total
      </td>
      <td>
         <?php echo count($users); ?>
      </td>
  </tr>

more information HTML5 data-* attributes

更多信息HTML5 data- *属性

#2


13  

I found an easy solution that works for me and does not require too much code or changes to datatables.js.

我找到了一个适合我的简单解决方案,并且不需要太多代码或更改datatables.js。

It is very similar to the requirements of the question, but not exactly the same.

它与问题的要求非常相似,但并不完全相同。

Instead of data-sort you can use a HTML comment tag.

您可以使用HTML注释标记代替数据排序。

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

E 1.111,23

becomes

<td><!-- 000000111123 -->E 1.111,23</td>

<! - 000000111123 - > E 1.111,23

By zero-padding an int they will be sorted as a string. The zero-padding makes the sort behave as you'd expect: sorting the integers from high to low.

通过零填充int,它们将被排序为字符串。零填充使排序的行为与您期望的一样:将整数从高到低排序。

Solution works for dates, ints and strings. For dates and ints you can use a scripting language to output them in the way you want (eg: zero-padded, formatted as yyyy-mm-dd).

解决方案适用于日期,整数和字符串。对于日期和整数,您可以使用脚本语言以您想要的方式输出它们(例如:零填充,格式化为yyyy-mm-dd)。