Datatable get the values of all rows of a specific column

孤街浪徒 提交于 2021-01-28 07:56:21

问题


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

回答1:


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



回答2:


"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.



来源:https://stackoverflow.com/questions/53221439/datatable-get-the-values-of-all-rows-of-a-specific-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!