Tuesday, May 18, 2010

Checkbox select all jQuery plugin for a table

 * File: jquery.checkboxselection.js
 * Plugin to create checkbox in the table
 * It needs table has correct structure (at least with head and body) and
 * there are checkboxs in row's first cell both in header in body

(function($) {
    $.fn.checkboxSelection = function() {
        return this.each(function() {
            var table = this;
            var headCheckbox = $('thead th:first :checkbox', table).click(function() {
                $('tbody tr', table).find('td:first :checkbox').attr('checked', this.checked);
            var checkSelection = function() {
                var trs = $('tbody tr', table).find('td:first');
                headCheckbox.attr('checked', trs.find('input:checked').length == trs.find(':checkbox').length);
            $('tbody tr', this).find('td:first :checkbox').click(checkSelection);
            <th><input type="checkbox"></th>
            <td><input type="checkbox" name="a[1]"></td>
            <td>John Smith</td>
            <td><input type="checkbox" name="a[2]"></td>
            <td>Jack Daniels</td>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.