Recalculate colum on ordering
HTML
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<input type="text" id="initialInput" value="20"/>
<table id="mytable">
<thead><tr></tr><th>column1</th>
<th>column2</th>
<th>column3</th></tr>
</thead>
<tbody>
<tr>
<td>val1</td>
<td>5</td>
<td></td>
</tr>
<tr>
<td>val2</td>
<td>2</td>
<td></td>
</tr>
<tr>
<td>val3</td>
<td>3</td>
<td></td>
</tr>
</tbody>
</table>
JavaScript
// JavaScript source code
// find elements
var initVal = $("#initialInput").val() * 1;
var myTable = $('#mytable').DataTable({
select: {
info: false
},
ordering: true,
columns: [
{
"data": "column1",
"name": "column1",
"orderable": true,
},
{
"data": "column2",
"name": "column2",
"orderable": false,
},
{
"data": "column3",
"type": "formatted-num",
"className":"calculated",
"orderable": true,
"render": function (data, type, row, meta) {
console.log("fired from render");
var count = 0;
var api = $.fn.dataTable.Api(meta.settings);
var calculatedCol3 = 0;
//si la table n'est pas vide
if (api.rows().count() > 0) {
//si le montant authorise est 0 ou non saisie
if (initVal == 0 || typeof initVal == 'undefined') {
return 0;
}
else {
var sumCol2 = 0;
for (var i = 0; i < api.rows().count() ; i++) {
var rowi = api.row(i).data()
sumCol2 += rowi.column2 * 1;
}
calculatedCol3 = initVal - sumCol2 * 1;
return calculatedCol3;
}
}
}
}]
});
function processColumn(tableJq, initVal, colTindex, colSindex) {
var montantRestant = 0;
if (tableJq.rows().count() > 0) {
if (initVal == 0 || typeof initVal == 'undefined') {
return;
}
else {
var tableData = tableJq.rows().data().toArray();
for (var j = 0; j <...