Sumation of formatted values in rows/columns
Sumation of formatted values in rows/columns
I am using below code to sum up values in a datatable. This works perfectly fine, as long as the value in the column is without formatting or any other content, like labels.
I have removed everything from the column now, but would need to format the column-cell like this:
<td style="font-weight: bold; text-align: right;">
@String.Format("{0:N0}", @Math.Round((item.KgPerHour * 24 * 365) / 1000))
</td>
But the "@String.Format..." prevents the summation as shown below. Is there a solution, a workaround?
Thank you very much.
"paging": true,
"autoWidth": true,
"footerCallback": function (row, data, start, end, display) {
var api = this.api();
nb_cols = api.columns().nodes().length;
var j = 7;
var numFormat = $.fn.dataTable.render.number('\,', '.', 0, '').display;
while (j < nb_cols) {
var pageTotal = api
.column(j, { search: 'applied' })
.data()
.reduce(function (a, b) {
return Number(a) + Number(b);
}, 0);
$(api.column(j).footer()).html(numFormat(pageTotal));
j++;
}
},
Answers
You could apply the "String" format only after a "draw" has been completed:
https://datatables.net/reference/event/draw
And get rid of the "String" format whenever a draw will be started:
https://datatables.net/reference/event/preDraw
This might be relevant as well:
https://datatables.net/reference/api/rows().invalidate()
See the FooterCallback example for one why to remove formatting.
Kevin