[英]Datatable get the values of all rows of a specific column
如何獲取特定列的所有行的所有值?
基本上我想實現的是,從'Key'
列中獲取所有值,並將其作為全局變量推送到allAdminKeys
數組,因為我需要在其他地方使用這些值。
var t = $('#adminKeysTable').DataTable( {
"ajax": {
"url": getKeysById,
"dataSrc": function(json) {
var rows = [];
for (var i=0;i<json.keys.length;i++) {
//skip rows "if a condition is met"
//here just any rows except row #1
if (json.keys[i].privileges == '32')
rows.push(json.keys[i]);
}
return rows;
}
},
"columns": [
{ "data": null },
{ "data": "name" },
{ "data": "key" },
{ "data": null }
],
"columnDefs": [
{ "targets": 0, "searchable": false, "orderable": false},
{ "targets": 2, "name": "key"},
{ "targets": -1, "defaultContent": '<div class="tb-btn regenerate-btn" id="btnRegenerateAdminKey" data-toggle="modal" data-target="#regenerateAdminKeyConfirmation"></div>'}
],
"order": [[ 1, 'asc' ]],
"paging": false,
"ordering": false,
"info": false,
'processing': true
} );
var allAdminKeys = [];
var rowData = t.rows().data(); //t is my table
$.each($(rowData), function(key,value){
allAdminKeys.push(value["key"]); //filter by "Key" column
})
console.log(allAdminKeys); // returning an empty array
"initComplete": function(settings, json){
for (var i=0;i<json.keys.length;i++) {
allAdminKeys.push(json.keys[i].key);
}
}
通過添加此內容成功實現了我想要的功能。
您可以使用.columns()
函數來訪問數據
let col = 0 // can be column index or css class of column header
// get all cells of the column
const cells = $yourDataTable.columns(col).nodes()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.