Applying both columns.render and columnDefs.render
Applying both columns.render and columnDefs.render
I'm setting up a table that has some orthogonal data. I am looking to use columns.render to calculate an average of an array of values, then i was hoping that columnDefs.render would apply the correct number formatting to it, however it seems to be ignoring my columnDefs function - is this the correct behaviour? Is there a way to achieve applying both without duplicating my columnDefs function?
HTML:
<table class='table' id='creators'>
<thead>
<tr>
....
<th class='number'>Avg Camp. Views</th>
....
</tr>
</thead>
</table>
JS:
columns: [
....
{
data: 'avg_camp_views',
defaultContent: 0,
render: function(data, type, row) {
if(data) {
return data.reduce((a, b) => a + b, 0) / data.length;
} else {
return 0;
}
}
}
]
columnDefs: [
{
targets: 'number',
render: function(data, type) {
if(type == 'display') {
if(data > 1000) {
return numeral(data).format('0.0a');
} else {
return data;
}
} else {
return data;
}
}
}
]
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Answers
Only one of the
columns
andcolumnDefs
should be added, as the one will over-write the other. You can merge those two functions into a single one, which should do the trick.Colin