sort column by checkbox state?

sort column by checkbox state?

mihomesmihomes Posts: 165Questions: 23Answers: 0
edited January 2014 in General
I have a checkbox column in my table and wondering if it is possible to sort it by the status of the checkbox (checked/unchecked). The column can be either of the two states :

[code]


or not checked

Replies

  • mihomesmihomes Posts: 165Questions: 23Answers: 0
    I found an example to work with:

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

    [/code]

    for the column in question :

    [code]
    "columns": [
    {
    "data": "activity_id",
    "class": "center",
    "searchable": false,
    "orderDataType": "dom-checkbox"
    }
    ]

    [/code]

    , but I am getting a 'TypeError: oSettings.oApi._fnGetTrNodes is not a function' error in the console. Any ideas? I am using 1.10 version with server-side.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    This is the updated example for 1.10: https://github.com/DataTables/DataTables/blob/master/examples/plug-ins/dom_sort.html#L37

    Btw - with server-side processing, all sorting is done by the server. I'm not sure that this will work at all with server-side processing enabled (I'd be surprised if it did!). To have this work with server-side processing enabled, you'd need to send the checkbox information to the server and have it sort - messy...

    Allan
  • mihomesmihomes Posts: 165Questions: 23Answers: 0
    It didn't even cross my mind about the sorting happening on the server-side. Do I have any options available to me to enable this option?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Nope sorry - I've not actually come across this requirement (at least not that I recall) to have server-side processing and checkboxes. There isn't anything int he pre-built PHP libraries for DataTables that would help with that I'm afraid, you'll need to modify them. As I say, I don't expect that to be very nice since you are mixing PHP sorting with SQL sorting...

    Allan
  • mihomesmihomes Posts: 165Questions: 23Answers: 0
    Well, I wouldn't say its a requirement, but was something I thought would be great for the user. I am using the checkboxes as selectors for deletion and saving their state in an array for when redraws happen. I started thinking about it and thought it would be nice to sort by check state so the user can get a quick overview of what they actually checked if need be rather than browsing all rows/pages. I guess for now I will leave this out unless I can think of something else. Thanks for the info Allan.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Thinking about it, I'm not sure that this actually is possible with server-side processing, at least not without a fair amount of code... Server-side processing deleted the rows that it isn't using - so as soon as you change page, your previous checkboxes are gone - completely gone.

    That's the whole point of server-side processing - it only retains what it needs for the current display!

    So you'd need to track checkboxes for rows which don't exist in the table...

    At which point you'd been a heck of a lot easier just using client-side processing.

    How many rows are you working with?

    Allan
  • mihomesmihomes Posts: 165Questions: 23Answers: 0
    I think maybe you have a different view of what I was trying to do. First column is a checkbox for that row. Users can select the checkbox which stores its value in an array. They can then hit the delete button to delete the row(s) from the server and I redraw the table. Obviously those rows are gone when there is a redraw and I clear my array as well.

    The reason I wanted to allow sorting was BEFORE they hit delete. Say the user wants to remove a row here and there then wants to review what they selected before hitting the delete button. The easiest way to do that would be sorting rows by their checked state.

    I am saving the checked status of the rows so no matter how many times you page, search, etc the rows will stay checked until you uncheck them or they are deleted. I am doing that with drawcallback and checking the rows against my array to set checked status or not.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    > I am saving the checked status of the rows so no matter how many times you page, search, etc the rows will stay checked until you uncheck them or they are deleted. I am doing that with drawcallback and checking the rows against my array to set checked status or not.

    Okay great - that will resolve that problem I was talking about in my last post. It would still require that checked state information to be sent to the server on a draw request that sorts by that column though.

    Allan
This discussion has been closed.