api.column().data() call on rendered column
api.column().data() call on rendered column
mRender
Posts: 151Questions: 26Answers: 13
So I'm pretty sure my issue is that I'm using the column().data() function on a column that the data is set to null. But when I set it to anything else and then render it, I get data that I don't want.
columns: [
{ data: "tbl_types.type_desc" },
{ data: "tbl_pexpenses.description" },
{ data: "tbl_pexpenses.cost" },
{ data: "tbl_pexpenses.quantity" },
{ data: null,
render: function ( data, type, row ) {
return (data.tbl_pexpenses.cost*data.tbl_pexpenses.quantity);
} }
],
footerCallback: function ( row, data, start, end, display ) {
var api = this.api(), data;
// Remove the formatting to get integer data for summation
var intVal = function ( i ) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
data = api.column( 4 ).data();
total = data.length ?
data.reduce( function (a, b) {
return intVal(a) + intVal(b);
} ) :
0;
// Update footer
$( api.column( 4 ).footer() ).html(
'$'+ total
);
}
Is there any way to make the column data: data.tbl_pexpenses.cost*data.tbl_pexpenses.quantity <<< this?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Good question - the
column().data()
method will get the raw data for that column. In this case,null
as you point out. What you could do is use thecolumn().cache()
method to get the data that DataTables has stored internally for ordering or filtering, which from your function will contain the result you want.Regards,
Allan
Thanks for the reply, cache did not work I'm afraid. Here is my full code in case you see something that could possibly help in this case.
http://108.160.144.86/pexpenses.php?PID=1 in case you need the webpage to look at it.
Try using
search
rather thanorder
- the order information isn't populated until it is needed for a sort (sorry - I forgot about that in my previous post).If I run the following on the console on your page it returns the expected data:
$('#pexpenses').DataTable().column( 4 ).cache('search');
Allan
Amazing. Remind me to send you a present on your birthday :)