DataTables - 计算每种类型的选项

时间:2017-06-12 14:35:51

标签: jquery datatable datatables

我有一个简单的数据表设置。有1000行。一列只能有4个不同的值。

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

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

由于

1 个答案:

答案 0 :(得分: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请求,请参阅item属性名而不是数组索引:

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/