mData, mRender and ordering

mData, mRender and ordering

giorgioponzagiorgioponza Posts: 3Questions: 0Answers: 0
edited October 2012 in DataTables 1.9
Hello, i'm trying to port my 'old' 1.8 datatables to new 1.9.
The table is populated by Ajax and the source is JSON. Each row is defined as follow:
[code]{"date":"17.06.12 10:49:26","id":166,"odometer":4560}[/code]
I define the 'odometer' column in 'aoColumnDefs' with the "mData": "odometer". In this way, if i order by 'odometer' column, everything is working ok. If i add a "mRender" like this
[code]"mRender": function (data, type, full) { return addCommas(data, '.', ','); }[/code]
then the order is done using the RENDERED cell value and not the raw one [addCommas simply adds thousand separator for easy reading, but in italy the char used is . and this is the decimal separator for english].

Before 1.9, i used the "bUseRendered" option, but now is deprecated. If i've readed right the docs, the field on which the order is done should be the one defined with "mData", because "mRender" is only used for showing the raw value in a different way.
So my question is: why the order is done using the value in 'mRender' and not the one in 'mData' ? Am i missing some configuration?
I prepared a small test on this page http://nasar.movimatica.com/Test.jsp. The second table has a wrong order on column 'odometer'

Thank you so much
Giorgio Ponza

Replies

  • allanallan Posts: 63,394Questions: 1Answers: 10,451 Site admin
    So the way to do this in 1.9+ is to use the `type` parameter that is passed into mRender:

    [code]
    "mRender": function (data, type, full) {
    if ( type === 'display' ) {
    return addCommas(data, '.', ',');
    }
    return data;
    }
    [/code]

    With that the unformatted data will be used for sorting, filtering and type detection, while the formatted data will be used for display only. You can modify the function as you wish to use other options as well. See: http://datatables.net/blog/Orthogonal_data

    Allan
  • giorgioponzagiorgioponza Posts: 3Questions: 0Answers: 0
    Stupid me, i had to read the whole docs :D
    With your help, now it works as expected.
    Many many thanks
  • deanodeano Posts: 2Questions: 0Answers: 0
    Hi,
    I used to use "bUseRendered" along with "fnRender" to display something different to the underlying data in the table, but use the original data when exporting to file with TableTools. I tried using the code that Allen has suggested above, expecting it to work as before, but TableTools will render the cell data as it is displayed in the table. Any Ideas how I can fix this to use only the pre-rendered data for TableTools? I have tried both mData and mRender.
    Thanks
  • allanallan Posts: 63,394Questions: 1Answers: 10,451 Site admin
    Currently, TableTools will just use the display rendered data. I think I'll expand it to have a 'tabletools' data type at some point, but at the moment the way to do that is to change _fnGatherData in TableTools.

    Allan
  • deanodeano Posts: 2Questions: 0Answers: 0
    Ok thanks Allan
This discussion has been closed.