数据表排序 - 如何忽略列中的文本? [英] Datatables sorting - how to ignore text in column?

查看:10
本文介绍了数据表排序 - 如何忽略列中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个脚本对我的数据表进行排序并忽略我不想排序的文本,我会解释.

I have used this script to sort my datatable and ignore text that I do not want to sort, I'll explain.

这是列示例:

10,836
↑(10.71%)
14,836
↑(13.71%)

我想忽略这个:↑(10.71%) 并根据这个排序:10,836.

I want to ignore this: ↑(10.71%) and to sort according to this: 10,836.

这就是我的脚本:

jQuery.extend(jQuery.fn.dataTableExt.oSort, {
  "justNum-pre": a => parseFloat(a.replace(/D/g, "")),
  "justNum-asc": (a, b) => a - b,
  "justNum-desc": (a, b) => b - a
});
$(document).ready(function () {
  var table = $('#dataTable').DataTable({
      order: [[ 1, "desc" ]],
      scrollY: 200,
      scrollX: false,
      responsive: true,
      paging: false,
      //colReorder: true,
      //pageLength: 100,
      columns: [
          {
              "render": function(data, type, row){
                  return data.split(" ").join("<br/>");
              }
          },
          null,
          null,
          null,
          null,
          null,
          null
      ],
      columnDefs: [
      { className: "all", "targets": [ 0, 1, 3, 6 ] },
      {
        type: 'justNum',
        targets: 1
      }
      ]
  });
});

推荐答案

您可以使用 DataTables orderData 功能.

You can do this using the DataTables orderData feature.

例如,假设您的格式化数据在第一列中:

For example, assume your formatted data is in the first column:

10,836
↑(10.71%)

添加仅包含数字部分(无文本)10836 的第二列并将其定义为隐藏列.

Add a second column containing only the numeric portion (no text) 10836 and define it as a hidden column.

然后,在您的数据表定义中创建一个 columnDefs 部分 - 如下所示:

Then, create a columnDefs section in your datatable definition - something like this:

$('#demo').DataTable( {
    "columnDefs": [
      { "orderData": [ 1 ], "targets": 0 },
      { "visible": false, "targets": 1 }
    ]
  } );

} );

这表示第一列(目标索引 0)将使用第二列(目标索引 1)中的数据进行排序.第二列将是一个隐藏列.

This says that the first column (target index 0) will use the data in the second column (target index 1) for sorting. And the second column will be a hidden column.

这篇关于数据表排序 - 如何忽略列中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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