How to sum multiple columns fitered by custom filter?
How to sum multiple columns fitered by custom filter?
![markzzz](https://secure.gravatar.com/avatar/922639f2170a63b90665ad9fbf909dd6/?default=https%3A%2F%2Fvanillicon.com%2F922639f2170a63b90665ad9fbf909dd6_200.png&rating=g&size=120)
Hi there!
I'm summing some columns where I place the class sum
this way:
var dataTableMultipleColumnsSum = function () {
var api = this.api();
api.columns('.dum').every(function () {
var total = api
.column( this.index() )
.data()
.reduce(function (a, b) {
return ParseNumber(a) + ParseNumber(b);
}, 0);
$(this.footer()).html(total + " total")"
);
});
}
But, once I filter the rows using a custom filter:
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
var columnFilterID = 1;
var selectedClinic = parseInt($('.dataTables_customToolbar select').val());
var dataClinicID = parseInt(data[columnFilterID]) || 0;
if (isNaN(selectedClinic) || dataClinicID == selectedClinic) {
return true;
}
return false;
}
);
I can still see the total of all rows, not the ones being filtered, for each column.
How would you achieve this?
Thanks
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Hi @markzzz ,
Easily done, just change the
column()
line to beCheers,
Colin
I see, thanks! What's the difference between that and:
It seems both works![:neutral: :neutral:](https://datatables.net/forums/resources/emoji/neutral.png)
filter
is just old&inferior -search
is new&improved and should be used in preference. This page here gives a bit more infoC
Thanks, it works perfect!
Another question colin (if you can): what if I would like to do some "sum" only if another column (at the same row) is "true"? Tried this:
But it returns weird stuff. The external column is at position [8], and its called PaymentCaseStudyIsPaid (which contains true, false, or null).
Where am I wrong?
Thanks so much
Another attempt:
But once filtered, it consider the index of all rows, not after the filter applied.
Hi @markzzz ,
Take a look at this example here - this is only counting the search results if the city is 'London', so similar to yours. If you search for 'Developer', it's easy to see.
Hope that helps,
Cheers,
Colin
Hi @colin,
uhm not really the same. Here you have single columns. I need to iterate for all columns that have the .sum class. So its NxM operation, without passing the column ID.
But I think I could easily use the this.index() in the top, and than use that index.
But I'm not sure I can access to
var d = this.data();
using array []. In fact d[index] give me error. I need d.NameParameter. Any clues?Use can use a jQuery expression to get the columns that match a class - so something like
Cheers,
Colin
Here's the code I have:
It should work. But I can't access to
row[columnIndex1
(orrow[columnIndex + 1]
) sincevar row = this.data();
return an object, not an array.Tried
.toArray()
, but it says its not a function.Quite strange, because in your example![:open_mouth: :open_mouth:](https://datatables.net/forums/resources/emoji/open_mouth.png)
var d = this.data();
returns an arrayP.s: I'm retrieving data with Ajax:
Here's my JSON:
Can be that? Not sure what should I do. Convert object to arrays? Uhm...
Hi,
Take a look at this example, it's similar to that last one, but this time it's using objects as you are. Here, I've named (
columns.name
) each of the columns, which you can then access in the sums:This should work for you, unless I'm being daft here.
Cheers,
Colin
That's the way I did using
.data().PaymentCaseStudyIsPaid
in the example above.But I need dynamic ID, I can't pass the name of the param I want, because must be dynamic and work for every kind of column.
It would change for each column (and the next of it, which is invisible and contain "flag" value).
I'm trying with somethings like this:
but I don't feel elegant at all. I need to call columnsSettings every time and such...
Any others idea? Not sure if I can retrieve the column's name in other ways.
Maybe init a global object on init?
The fact is that this will execute after the column inits, so in callback like
render
this will fail at the beginning.Hi,
The key here is going to be the selector for getting only the rows that you want the data for to sum. There are a few ways to do that such as using a
row-selector
as a function or using thefilter()
method - let's go with a row selector first:With
filter()
:I don't think there will be any noticeable difference in performance between them.
Allan