简体   繁体   中英

Datatable get the values of all rows of a specific column

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

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
"initComplete": function(settings, json){
                for (var i=0;i<json.keys.length;i++) {                                              
                    allAdminKeys.push(json.keys[i].key);
                }
            }   

Managed to achieve what I wanted by adding this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM