数据表获取特定列的所有行的值

问题描述:

如何获取特定列的所有行的所有值?

How to get all values of all rows of a specific column?

基本上我想要实现的是,从'Key'列中获取所有值,并将其作为全局变量推送到 allAdminKeys 数组中,因为我需要在其他地方使用这些值.

Basically what I want to achieve is, get all the values from 'Key' column and push to allAdminKeys array as global variable because I need those values in somewhere else.

 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

您可以使用 .columns()函数访问数据

You can use .columns() function to access the data

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()