Search code examples
javascriptjquerytablesorter

Selector for filtered items with jQuery tableSorter plugin


This got into my head lately and honestly I think its worth asking. Here is the thingy..

I have a table , just like any other ordinary table with jquery tablesorter plugin with the filter widget. On the very right of the table columns I've put a checkbox and above that column, on the table headers on that columhn I have another checkbox which has a function linked to it, so that when it gets clicked all the checkboxes get updated with this checkbox's value . This is not very fancy or sophisticated and I have two way of accomplishing this .. either using a jquery selector or plain old javascript.

so here is what I want todo .. I want to filter elements of the table, then click the checkbox on the header , and I want to affect the row's checkboxes that were filtered with the plugin.

anyone has something to say about this? thanks .


Solution

  • I already have a demo set up for that here

    $( function() {
        // using .on() which requires jQuery 1.7+
        $( 'table' ).on( 'tablesorter-initialized', function() {
    
            // class name to add on tr when checkbox is checked
            var highlightClass = 'checked',
            // resort the table after the checkbox is modified?
            resort = true,
            // if a server side database needs to be updated, do it here
            serverCallback = function( table, inputElement ) {},
    
            $table = $( this ),
            c = this.config,
            wo = c && c.widgetOptions,
            // include sticky header checkbox; if installed
            $sticky = c && wo.$sticky || '',
            doChecky = function( c, col ) {
                $table
                    .children( 'tbody' )
                    .children( 'tr:visible' )
                    .children( 'td:nth-child( ' + ( parseInt( col, 10 ) + 1 ) + ' )' )
                    .find( 'input[type=checkbox]' )
                    .each( function() {
                        this.checked = c;
                        $( this ).trigger( 'change' );
                    });
            };
    
            $table
                .children( 'tbody' )
                .on( 'change', 'input[type=checkbox]', function() {
                    // ignore change if updating all rows
                    if ( $table[0].ignoreChange ) { return; }
                    var col, $this = $( this );
                    $this.closest( 'tr' ).toggleClass( highlightClass, this.checked );
                    $this.trigger( 'updateCell', [ $this.closest( 'td' ), resort ] );
                    // if your server side database needs more parameters, add them here sent to the callback
                    serverCallback( $table[0], this );
                    // uncheck header if any checkboxes are unchecked
                    if ( !this.checked ) {
                        $table.add( $sticky ).find( 'thead input[type=checkbox]' ).prop( 'checked', false );
                    }
                })
                .end()
                .add( $sticky )
                .find( 'thead input[type=checkbox]' )
                // Click on checkbox in table header to toggle all inputs
                .on( 'change', function() {
                    // prevent updateCell for every cell
                    $table[0].ignoreChange = true;
                    var c = this.checked,
                        col = $( this ).closest( 'th' ).attr( 'data-column' );
                    doChecky( c, col );
                    // update main & sticky header
                    $table.add( $sticky ).find( 'th[data-column=' + col + '] input[type=checkbox]' ).prop( 'checked', c );
                    $table.children( 'tbody' ).children( 'tr:visible' ).toggleClass( highlightClass, c );
                    // update all at once
                    $table[0].ignoreChange = false;
                    $table.trigger( 'update', [ resort ] );
                })
                .on( 'mouseup', function() {
                    return false;
                });
    
        });
    
        $('table').tablesorter({
            theme: 'blue',
            widgets: ['zebra', 'stickyHeaders','filter'],
            headers: {
                0: { sorter: 'checkbox' }
            }
        });
    });
    

    Just make sure to include the parser-input-select.js file