sum and a percentage on a column
sum and a percentage on a column
Rich_Walker
Posts: 9Questions: 3Answers: 0
Hi
I have a table that i need to work out difference between A & B as percentage for quantity. I have this example http://live.datatables.net/pekarebe/7/edit
I can see the line return x + y; but when i change it to x / y; it breaks. What am i doing wrong?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
I was getting a syntax error on that page. Using
return (x / y);
allows it to run: http://live.datatables.net/pekarebe/8/editAllan
Thank you allan.
Allan
One more question please. I have just been told i need to show the percentage difference (on its own row as well as sum).
I have added another api column call with the following but can't work out how to put on another row and
.reduce(function (a, b) {
var x = parseFloat(a) || 0;
var y = parseFloat(b) || 0;
var z = parseFloat(0) || 0;
return z - x / y * 100;
}, 0);
http://live.datatables.net/pekarebe/17/edit
I'm also looking for an example how to work out the percentage but i'm struggling to find an example that works.
You would need to address the second row in the footer directly (using jQuery or querySelectorAll) as DataTables doesn't currently provide an API method to access the second row for the footer.
Something like
$('#example tfoot tr:eq(2) th:eq(3)').html( calculationResult );
would do it.Allan
@allan Sorry if i seem a bit slow. Are you saying if i replace
<code>$(this.footer('#example tfoot tr th.text-input')).html(sum);</code>
with this <code>$('#example tfoot tr:eq(2) th:eq(3)').html( calculationResult );</code>
It will do the trick?
Thanks
Rich
More or less. In my code the column index isn't dynamic so you would need to replace the
th:eq(3)
with the column index you want to replace the data for (this.index()
inside thecolumns().every()
function will give you that index).Allan