mData never executing 'sort' branch
mData never executing 'sort' branch
Pretty much as described, using datatables 1.9.4. The code in the 'set' and 'display' branches works properly, but type never equals 'sort' or 'filter'. Any idea why this would be? It would make sense if bSortable was false, but I don't see why else this would happen. Since this is more of a general question I won't post the entire code sample, but for reference, here is the relevant object from aoColumnDefs:
[code]
{
"aTargets": [ 9 ],
"mData": function (data, type, val) {
console.log(type);
if ( type === 'set') {
data.viralPercent = val;
data.viralPercent_display = checkRate(data, 'viralPercent');
data.viralPercent_sort = (data.rawPageviews < 250) ? -1 : data.rawPageviews;
}
else if ( type === 'display' ) {
return data.viralPercent_display;
}
else if ( type === 'sort' ) {
return data.viralPercent_sort;
}
return data.viralPercent;
},
"bSortable": true,
"asSorting": sorting("viralPercent"),
"helpText": ""
}, [/code]
The console.log produces the following:
[code]
undefined
set
type
display
[/code]
[code]
{
"aTargets": [ 9 ],
"mData": function (data, type, val) {
console.log(type);
if ( type === 'set') {
data.viralPercent = val;
data.viralPercent_display = checkRate(data, 'viralPercent');
data.viralPercent_sort = (data.rawPageviews < 250) ? -1 : data.rawPageviews;
}
else if ( type === 'display' ) {
return data.viralPercent_display;
}
else if ( type === 'sort' ) {
return data.viralPercent_sort;
}
return data.viralPercent;
},
"bSortable": true,
"asSorting": sorting("viralPercent"),
"helpText": ""
}, [/code]
The console.log produces the following:
[code]
undefined
set
type
display
[/code]
This discussion has been closed.
Replies
Allan
I tried that, type is still never becoming 'sort' though. :-/
Alex
Allan
In all my other columns mData is a string pointing at the property within the JSON object where that column should be getting data. But because in this column mData is a function rather than a string, this is what mDataProp looks like in my query header:
...
mDataProp_7:slideshow
mDataProp_8:vidType
mDataProp_9:function
mDataProp_10:searchRefCount
mDataProp_11:socialRefCount
...
A) When mData is a function, how does datatables know what to use for the 'val' parameter? My understanding is that aTarget refers to which column in the HTML you're targeting, but how does 'val' get assigned to a column in your JSON object?
B) When sorting, our codebase currently uses the integer passed by iSortCol (e.g. 9) and then appends it to the string 'mDataProp_' in order to get the name of the column to sort by for the SQL query. Clearly if mDataProp_x will now just be 'function' when mData is a function then this will no longer work... In this case, is this still a good way to get the name of the column to sort by from datatables, rather than having to store a separate dictionary for every single query?
Alex
If you are using mData to format the data, change to using mRender and then mData can be used for the string property reference.
Allan
The reason I switched to mData from fnRender was because I wanted a way to filter certain values when sorting.
The scenario is basically I have a column with the bounce rate percentages for different pieces of web content, so if you sort by the content with the lowest bounce rate percentage then I don't want to see all content with only 1 pageview and 0 bounces or whatever... Essentially when sorting I would want to filter out content with less than 500 page views or whatever.
I can probably find some hacky way to do that with mRender, but mData would clearly be a much better solution. For now I'll probably just hard code the column lookups, the only issue is having to change them in two places. And also the fact that I still can't figure out why type never gets set to 'sort', even when I'm clearly sorting. I'll work on putting together a code sample, but it'll just take a couple days because it's a fairly complicated table.
Alex
Allan