sum a column based on a condition in another column
sum a column based on a condition in another column
Hi,
Firstly wow! hasn't DataTables come along since the last time I used it! Amazing work. It's brilliant!
Now my question.
My data represents income and expenses in a table. Each row can either be income or expense. So I would like to sum the income and sum the expenses. I have set up the plugin and can sum the current page or all the pages.
However, my data looks like this:
Amount | Direction | |||
---|---|---|---|---|
123.44 | income | |||
34.55 | expense | |||
233.5 | expense |
And this works to sum all the data in Amount:
var sum = table.column(0).data().sum();
alert(sum);
But I need a sum for all rows where the nth row is 'income' or 'expense'.
Is this possible and can anyone explain how?
TIA
Gus
Answers
I worked it out with this code which adds a button. Click the button to see some totals.
Hi Gus,
Thanks for updating your thread - good to hear you have a method to do what you want.
I was wondering if the code could be reduced a little, and think that using the selectors with their new function ability (from 1.10.3+) might reduce the LOC slightly. For example:
So we are using the
cells()
method and passing in the first parameter (the row selector) as a function - it will decide if the row is included or not based on the data logic check. Second parameter is the column for the cell and third is to get only the filtered rows, matching what you have above.Allan
thanks Allan
that is a lot more compact. I will try it out.
cheers
Gus
I can't get your code to work at all
I get "Uncaught type error: undefined is not a function" error on this line:
```js var sum = table.cells(function (index, data, node) {
Any ideas?
Should be:
See the FAQs ;-)
Allan
ah yes!
"Very likely you are using a jQuery object rather than a DataTables API instance. The form $().dataTable() will return a jQuery object, while $().DataTable() returns a DataTables API instance. "