sort by checkbox column

sort by checkbox column

gstargstar Posts: 1Questions: 0Answers: 0
edited June 2009 in General
Hi I have an issue i wonder if you could help me with.

how do you go about sorting a table based on a row containing check boxes

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I thought I was losing my mind for a minute there - I was sure there was two questions and then when I looked back there was only one! Just seen the edited tag...!

    Have a peak at this thread which covers something similar to this. You'll need to use the API to get the DOM elements which you are sorting on, and then get their values to sort upon.

    Another option is to add event listeners to each checkbox and change a value in a hidden column (you can use http://datatables.net/usage#iDataSort for this) or hidden element (with custom filtering).

    So many options :-)

    Allan
  • giorgio79giorgio79 Posts: 43Questions: 0Answers: 0
    Hi Allan,

    What thread are you referring to? :)

    I tried sorting checkbox columns expecting checked boxes to show on top (or bottom) but no luck. Any copy and paste stuff I can use?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I've no idea to be honest!

    However, here is an example which does live DOM sorting with checkboxes (or any other DOM element): http://datatables.net/examples/plug-ins/dom_sort.html

    Allan
  • giorgio79giorgio79 Posts: 43Questions: 0Answers: 0
    Wow, this stuff is pure awesomeness :D

    Love it, thanks Allan.

    Expect to see an explosion in usage as there is a Drupal module coming your way :P
    http://drupal.org/project/datatables
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Superb - I look forward to seeing the results :-)

    Regards,
    Allan
  • punchipunchi Posts: 6Questions: 1Answers: 0
    is there any way (e.g. http://datatables.net/examples/plug-ins/dom_sort.html ) to sort the checked first ?? you should click twice to see the checkboxed first...
  • DanvDanv Posts: 1Questions: 0Answers: 0
    @punchi

    In the function who is sorting the checkboxes, change this.checked == true to false ;)

    Like this =>

    [code]

    /* Create an array with the values of all the checkboxes in a column */
    $.fn.dataTableExt.afnSortData['dom-checkbox'] = function(oSettings, iColumn) {
    var aData = [];
    $('td:eq(' + iColumn + ') input', oSettings.oApi._fnGetTrNodes(oSettings)).each(function() {
    aData.push(this.checked == false ? "1" : "0");
    });
    return aData;
    }

    [/code]
  • qdataqdata Posts: 24Questions: 0Answers: 0
    I am also having the same problem sorting the check box column. I see the arrows moving, but there is no sort. Where and what coding do you put in the "Smoothness" theme?
This discussion has been closed.