How to get a DataTable Cell object from a filtered/sorted table
How to get a DataTable Cell object from a filtered/sorted table
Hello,
I have a DataTable v1.10.9 loaded dynamically with objects. I've implemented a custom export to CSV which works ok but when I'm pulling the data out I can't get the DataTable API to applied the sort/filter modifilers when I'm accessing cells. It appears if you access Cell objects the API bypasses the modifiers.
I'm accessing each Cell individually so I can run the render() method on it. This seemed to be the easiest and best approach for grabbing the data so I wouldn't need to know anything the object the developer is using inside the table. For instance, an object User with fields firstName, lastName are initialized using code similar to below.
"columns" : [ { data: 'id', "visible":false}
,{ "title":"Last name", type:"string", data: 'user.lastName' }
,{ "title":"First name", type:"string", data: 'user.firstName' }
...
Here's the select modifier I'm passing to DataTables.
var selectorModifier = {order:"current", search:"applied"};
I've tried applying the modifier to the rows() method before accessing a cell() but it still doesn't apply the modifiers.
for(var rowIndex=0; rowIndex < tableApi.rows(selectorModifier).data().length; rowIndex++) {
var data = [];
for(var colIndex = 0; colIndex < colCount; colIndex++) {
if ($.inArray(colIndex, self.options.excludeColsFromExport) == -1 && tableApi.column(colIndex).visible()) {
data.push('"' + self.convertToText(tableApi.rows(selectorModifier).data().cell(rowIndex, colIndex).render('sort')) + '"');
}
}
rows.push(data.join(","));
}
If I take the following approach, the filter/sort modifiers are applied but I get back a comma separated string for basic tables and [Object] for the dynamically loaded tables.
for(var rowIndex=0; rowIndex < tableApi.rows(selectorModifier).data().length; rowIndex++) {
var data = [];
for(var colIndex = 0; colIndex < colCount; colIndex++) {
if ($.inArray(colIndex, self.options.excludeColsFromExport) == -1 && tableApi.column(colIndex).visible()) {
data.push('"' + self.convertToText(tableApi.rows(selectorModifier).data()[rowIndex]) + '"');
}
}
rows.push(data.join(","));
}
Thoughts on how to resolve?