2017-06-12 120 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/