How to tally occurrences of a string in a dynamically populated column?
How to tally occurrences of a string in a dynamically populated column?
I am trying to tally the occurrences of a string in a column that is dynamically populated using:
{"data": null,
"render": function(data,type,row) {
var today = new Date();
var t1 = today.getFullYear() + "-03-31";
var end = moment(t1);
var start = moment(data.dob);
var year = end.diff(start, 'years', true);
if(year < 8){
return ("J");
}else{
if(year < 11){
return ("C");
}else{
if(year < 14){
return ("S");
}else{
if(year < 18){
return ("V");
}else{
if(year < 26){
return ("R");
}else{
return ("L");
}
}
}
}
}
},
},
I am then using the following to tally:
var filteredData2 = cubPackDetailsTable
.column( 8 )
.data()
.filter( function ( value, index ) {
return value == "J" ? true : false;
} ).count();
alert("filteredData2: " + filteredData2);
This is returning 0 (zero). When I replace "return value == "J" ? true : false;" with "return value = "J" ? true : false;" it returns 106, which is the total number of column rows (there are only three occurrences of "J").
I have tried unsuccessfully to recreate the issue at: live.datatables.net/siviwiru/1/edit. My table is using json from an ajax call rather than html.
Kind regards,
Glyn
This question has an accepted answers - jump to answer
Answers
columns.renderneeds to go insidecolumnsorcolumnDefs, not at the top level of the initialisation.Colin
See example here of summing column.
For rendered data you will need to use
cells().render()instead ofcolumn().data(). Something like this:Kevin
Thank you very much Kevin :-)