MKP
MKP

Reputation: 117

Jquery datatables. Array of rows of visible columns

I'm trying to extract an array of rows from my datatable. My problem is that I have some fields of the json that populates the table that I don't show in the table. When I use

$('#myTable').DataTable().rows().data().toArray() 

I get those fields that I don't need.

¿How can I get that array of the shown fields or columns?

Thanks in advance.

Upvotes: 0

Views: 1757

Answers (1)

Dacklf
Dacklf

Reputation: 700

You need to use a selector-modifier.

$('#myTable').DataTable().rows({search:'applied'}).data().toArray();

-------------------------------------

EDIT

A possible way to accomplish what you are asking for is to check first what columns are visible. Then, process each result row and get only the fields you want.

var columns = $('#myTable').DataTable().columns().visible();
var rows = $('#myTable').DataTable().rows().data().toArray();
var result = []; // this array will contain only the visible fields of each row
for (var i = 0; i < rows.length; ++i) {
    var row = [];
    for (var j = 0; j < columns.length; ++j)
        if (columns[j]) // is visible
            row.push(rows[i][j]);
    result.push(row);
}

Upvotes: 1

Related Questions