Problems sorting by mRender-ed checkbox column

Problems sorting by mRender-ed checkbox column

ryanmrossryanmross Posts: 4Questions: 0Answers: 0
edited December 2013 in General
Thanks so much for making this plugin. It really is great!

I'm pulling my hair out over one last issue... I've got a table that populates its data through ajax, and I have a column that uses mRender to convert the ajax data pulled in to a checkbox.

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

I also set up a custom live sort function like you have on this page:
http://datatables.net/examples/plug-ins/dom_sort.html

Here it is (with a console.log outputting the results):

[code]
/* Create an array with the values of all the checkboxes in a column */
$.fn.dataTableExt.afnSortData['dom-checkbox'] = function ( oSettings, iColumn )
{
console.log(
$.map( oSettings.oApi._fnGetTrNodes(oSettings), function (tr, i) {
return $('td:eq('+iColumn+') input', tr).prop('checked') ? '1' : '0';
});
);

return $.map( oSettings.oApi._fnGetTrNodes(oSettings), function (tr, i) {
return $('td:eq('+iColumn+') input', tr).prop('checked') ? '1' : '0';
});
}
[/code]


But it seems like using both mRender and the custom afnSortData functions together results in the sorting not working.

I've determined that if I remove the mRender and just populate my table by hand in the html, the sorting will work just fine.

Here's the debug output for this page:
http://debug.datatables.net/inaguv

The only thing I can see in the debug that's strange is under Tables > Full Table State > aoData > "_aSortData". It looks like the sort data isn't being properly inserted into that object. On an example page where the sorting works correctly, that object is populated with a 1 or 0 according to the state of the checkbox. But on the page with mRender functioning, it's populated with nothing.

Any idea why it wouldn't be passing that info?

Thanks,
Ryan

Replies

  • allanallan Posts: 63,204Questions: 1Answers: 10,415 Site admin
    Urgh - looks like my recent forum upgrade is stripping html from messages... Sorry about that.

    However, I see the issue. The good news is that this has been fixed in 1.10-dev: http://live.datatables.net/akavuc/edit#javascript,html . The bad news is that the DOM sorting functions will need to be updated since the private API that they used before has been removed in 1.10... It was poor of me using a private API before, but this is fixed in the DOM sorting functions in the example I linked to.

    1.10 is pre-beta (although not by much) and can be downloaded as the nightly from http://datatables.net/download .

    If you try it, let me know how you get on with it!

    Allan
  • ryanmrossryanmross Posts: 4Questions: 0Answers: 0
    edited December 2013
    I think I figured it out actually... It required switching from mRender to mData like so:

    "mData": function ( source, type, val ) {
    if (type === 'display') {
    if (source.approved == "1") {
    return '/* put the checkbox in here with checked="checked" */';
    } else {
    return '/* put the checkbox in here without checked="checked" */';
    }
    } else if (type === 'set') {
    source.approved = val;
    return;

    } else
    {
    return source.approved;
    }
    }


    Seems to work. Do you see any issues with that?

    I couldn't get version 1.10 to work for me on first shot.
  • allanallan Posts: 63,204Questions: 1Answers: 10,415 Site admin
    > Seems to work. Do you see any issues with that?

    Looks okay to me.

    > I couldn't get version 1.10 to work for me on first shot.

    I would hugely appreciate if you could link me to a page showing the error with 1.10 so I can fix it before 1.10 is released.

    Allan
  • ryanmrossryanmross Posts: 4Questions: 0Answers: 0
    Here's what I got when I switched to 1.10:

    http://debug.datatables.net/idafeq
  • allanallan Posts: 63,204Questions: 1Answers: 10,415 Site admin
    Took me ages to get to it - sorry - but I've just resolved the debugger error with 1.10 (was actually a bug in 1.10).

    What I think might be the problem is that you need to update the checkbox sort plug-in that you are using - try this: https://github.com/DataTables/Plugins/blob/master/sorting/custom-data-source/dom-checkbox.js

    Allan
  • avinashabhi5avinashabhi5 Posts: 1Questions: 0Answers: 0
    I modified mRender to mData but it still not sorting the check-box.Any suggestions sorting of check-box.
  • allanallan Posts: 63,204Questions: 1Answers: 10,415 Site admin
    Please link to a test case. As you can see here, it should work fine with live DOM sorting and the correct plug-ins: http://next.datatables.net/examples/plug-ins/dom_sort.html
This discussion has been closed.