YUI Datatable – select all with checkbox column

I am using YUI (version 2.4.1) datatable for tabular data rendering. I needed to allow the users to have the ability to select one or more rows using checkboxes and then submit them to the server for further action. Up to this point you can find sample code on YUI website or other sites if you search around.

For a better user experience I also needed to give them links to ‘Select All’ and ‘Unselect All’ which did exactly that. This I was not able to find on the web (I am sure it is there somewhere). Since I finally figured it out, I thought it only fair to blog-it so others can find. Code is quite simple.

In the sample below I will only highlight the code necessary to make this happen. For more details on YUI datatable refer to Yahoo site.

To start off lets add a check box to the table. In the sample below the first column is the check box column.

In my case the data is coming via. JSON and I need the default case to be select all. Thus my JSON result set will have checked = true for all rows.

Next here is the code for ‘select all’ and ‘unselect all’. You can be smarter about this and use one function if you care.

The links on the web page.