数据表排序-如何忽略列中的文本? [英] Datatables sorting - how to ignore text in column?
问题描述
我将使用此脚本对数据表进行排序,并忽略我不想排序的文本.
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.
多数民众赞成在我的脚本:
thats my script:
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
}
]
});
});
推荐答案
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屋!