首页 > 解决方案 > 数据表按编号文档和文档年份排序

问题描述

我不知道如何实现一种简单的方法来对列进行排序,该列向我显示带有尾随年份的文档数量,例如:

COL 1    COL 2     COL 3    DOC. NR
------------------------------------
  x        x          x       2/2020
------------------------------------
  x        x          x       3/2020
------------------------------------
  x        x          x       4/2021
------------------------------------
  x        x          x       1/2022
------------------------------------

在我的示例中,我想doc. nr根据文档的数量(按年份分组)按 asc 或 desc 对 col 进行排序。

我尝试使用公式设置数据顺序:numberdoc + year但它不起作用,例如,3 + 2020 等于 1 + 2022....所以这种方式是不正确的。任何想法?

<td data-order="2020">1/2019</td>

脚本:

$(document).ready( function () {
  var table = $('#example').DataTable({
     "aaSorting": [[ 1, "desc" ]],
    "columnDefs": [
            {
                targets: [1],
                data: {
                    _: "1.display",
                    sort: "1.@data-order",
                    type: "1.@data-order"
                }
            }
        ]
  });
} );

我的小提琴: http: //live.datatables.net/jivekefo/1/edit

预期(ASC)结果:

1/2018
2/2018
3/2018
1/2019
2/2019
3/2019
4/2019
1/2020
...

标签: datatables

解决方案


好的解决了这个规则:

<td data-order="<?php echo (new Datetime($dateDoc))->format("Y") . str_pad(ltrim($numberDoc, "0"), 5, "0", STR_PAD_LEFT); /* example. 201900001 */ ?>">

http://live.datatables.net/jivekefo/2/edit


推荐阅读