gpt4 book ai didi

jquery - 如何使用正交数据在DataTables中排序?

转载 作者:行者123 更新时间:2023-12-01 00:02:34 31 4
gpt4 key购买 nike

我正在使用DataTables v1.10。当列中显示的值不是数字时,我想让列可按数值排序。

我知道我需要使用 orthogonal data为此,如 described here

但是,我的排序功能不起作用。这是我的代码:

var myData = [{
'country': 'France',
'all_games': 7,
'won_games': 4
}, {
'country': 'England',
'all_games': 13,
'won_games': 13
}, {
'country': 'Germany',
'all_games': 2,
'won_games': 0
}];
var columns = [{
"data": "country",
"title": "Country"
}, {
"data": "outcomes_str",
"title": 'Games won',
"render": {
"_": "display",
"sort": "sort"
}
}];
$.each(myData, function (i, d) {
d.outcomes_str = {};
d.outcomes_str.sort = (d.all_games > 0) ? (d.won_games / d.all_games) * 100 : 0;
d.outcomes_str.display = d.won_games + '/' + d.all_games + ' (' + Math.round(d.outcomes_str.sort * 10) / 10 + '%)';
console.log(d.outcomes_str);
});
drawTable(myData, 'localTable');

function drawTable(data, tableId) {
var html = '<table class="table table-bordered table-hover ';
html += '" cellspacing="0" width="100%" id="myTable"></table>';
$('#table').append(html);
$("#myTable").DataTable({
data: data,
columns: columns,
paging: false
});
}
});

JSFiddle在这里:http://jsfiddle.net/07nk5wob/33/

我做错了什么?

最佳答案

参见my corrected answer .

基本上,对于数字数据,您需要使用 type: "num" 显式声明列数据类型,否则可能会默认按字母顺序排序。

{
"data": "outcomes_str",
"title": 'Games won',
"type": "num",
"render": {
"_": "display",
"sort": "sort"
}
}

参见updated jsFiddle用于代码和演示。

关于jquery - 如何使用正交数据在DataTables中排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33755422/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com