How to use sorttable.js to Sort a Column of Checkboxes

90 views Asked by At

I have a column of checkboxes that represent selected "favorites" in a large table. I'm attempting to use sorttable.js to sort the table so the rows that are checked can be brought to the top of the table. I'm not having any luck. I've tried the "sorttable.innerSortFunction.apply" snippet from the docs but it caused an error: sorttable.js:211 Uncaught TypeError: node.getAttribute is not a function. Any suggestions?

1

There are 1 answers

0
Dov Bear On

I happen to use sorttable.js albeit without checkboxes (CBs). I guess it would involve: The CB's .onclick event that would update its .checked (true/false) status to the parent TD cell's sorttable_customkey value as true or false.

I.e. (assuming CB INPUT is directly under TD):

CBobj.onclick = function() { this.parentNode.sorttable_customkey = this.checked }

or, more standardized:

CBobj.onclick = function() { this.parentNode.setAttribute("sorttable_customkey", this.checked) }

(You would obviously load the default TD sorttable_customkey=true/false values in accordance with your checkboxes from the server, but you can loop them on the client side with JS)