How can I get a total from a custom filter (without draw())?
How can I get a total from a custom filter (without draw())?
I am currently trying to get a total from a filter DataTable() instance, regardless of any search queries a user has performed and without also modifying/drawing the displayed dataTable. It would essentially act identical to the total in the example documentation here, but with a filter: https://datatables.net/examples/advanced_init/footer_callback.html
For my particular filter, I am trying to filter the results on a particular column for anything that starts with "01". The problem is that no matter what I do, I can't seem to get the result from my filter. It is always the total across all pages. I have tried using things like {search: 'applied'}, {filter: 'applied'}, I've tried not even using a filter and just using .search().
At present, I have this for my total:
let result = (specialTable.table(i).column(3).data()
.filter(function (value, index) {
console.log(value);
console.log(value.startsWith("01"));
return value.startsWith("01") ? true : false;
})).column(1, {filter: 'applied'})
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0);
For my filter, I am using value.startsWith("01") to look for any values that start with 01 in column 3. This is similar to the https://datatables.net/reference/api/filter() example. Yes, I know I can break this up, but it wasn't working so I tried to get the result with one single line. Here it is looking a bit more readable:
specialTable.table(i).column(3).data()
.filter(function (value, index) {
return value.startsWith("01") ? true : false;
});
let result = specialTable.column(1, {filter: 'applied'})
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0);
My understanding was that using a filter returns an API instance, and then I could use that API instance to get a total with the reduce function. Unfortunately, although I am getting a value, as I said before it is the total of all columns. I have created an example here with my problem:
https://jsfiddle.net/0nekaczb/
Whenever the user selects a month, it calls the updateTotals() function. From the console.log() statements, you can see that it is correctly identifying the rows in each column that are true and those that are false. However, when the console.log() shows the final totals, they are incorrect. It is showing [20.009999999999998, 152.97, 3054.9300000000003] instead of [13.76, 107.97, 0].
Why is this not working for me?
This question has an accepted answers - jump to answer
Answers
The
filter()
PI does not affect the filtering of the table. It simply returns Javascript data.You won't be able to use
column().data().filter()
because you want the data in a different column from the column you are testing. You will need to userows().data().filter()
. You can usereduce()
from the result offilter()
. Inreduce()
useb[1]
to access the numeric column. Like this:https://jsfiddle.net/26L4rzqn/
Kevin
Thank you, this resolved my issue completely!