mData never executing 'sort' branch

mData never executing 'sort' branch

Alex3917Alex3917 Posts: 11Questions: 0Answers: 0
edited January 2014 in General
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]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Have you sorted the table of column 10? The sort data won't be obtained until it is needed.

    Allan
  • Alex3917Alex3917 Posts: 11Questions: 0Answers: 0
    Allan,

    I tried that, type is still never becoming 'sort' though. :-/

    Alex
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Works okay here: http://live.datatables.net/ofodud/edit . Can you link to a test case please.

    Allan
  • Alex3917Alex3917 Posts: 11Questions: 0Answers: 0
    edited January 2014
    edit: Problem isn't actually fixed, still working on it.
  • Alex3917Alex3917 Posts: 11Questions: 0Answers: 0
    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
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I'm presuming you are using server-side processing? If so, you nee to 'decode' the submitted mDataProp_{i} value into the column index for look up.

    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
  • Alex3917Alex3917 Posts: 11Questions: 0Answers: 0
    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
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited January 2014
    mRender, not fnRender (the later of which is removed in DataTables 1.10). mRender is basically another layer to mData - one which doesn't need to deal with the setting of data, and they are typically used together.

    Allan
This discussion has been closed.