2017-06-12 117 views
0

我有一个简单的数据表设置。有1000行。一列只能有4个不同的值。DataTables - 统计每种类型的选项

我需要找出每种类型有多少。我已经看过https://datatables.net/reference/api/count(),但它似乎没有做我所需要的。

有什么建议吗?有人提到https://github.com/DataTables/Plugins/blob/master/features/alphabetSearch/dataTables.alphabetSearch.js#L61但我不知道从哪里开始

感谢

+0

你是如何拉动你的信息?为什么你不能这样做,而不是当你处理前端? – NoReceipt4Panda

+0

它来自我无法控制的预定义报告 – pee2pee

+0

您正在处理哪些语言?你打算用jQuery/js来做这件事吗? – NoReceipt4Panda

回答

2

这是一个相当容易的任务,如果它仅仅是如何计算的某些值的数量为特定列。你甚至可以实现它作为一个插件:

jQuery.fn.dataTable.Api.register('countValue()', function(index, value) { 
    var count = 0; 
    var data = this.data(); 
    for (var d in data) { 
    if (data[d][index] == value) count++ 
    } 
    return count; 
}); 

现在你可以做

console.log('Developer', table.countValue(2, 'Developer')); 
console.log('Regional Marketing', table.countValue(2, 'Regional Marketing')); 
console.log('Software Engineer', table.countValue(2, 'Software Engineer')); 

演示 - >http://jsfiddle.net/hmwLw2yw/

如果您使用的是JSON源,也可能是AJAX请求,请参阅项属性名称而不是数组索引:

console.log('Developer', table.countValue('position', 'Developer')); 
console.log('Regional Marketing', table.countValue('position', 'Regional Marketing')); 
console.log('Software Engineer', table.countValue('position', 'Software Engineer')); 

演示 - >http://jsfiddle.net/ytw647ak/