javascript - 使用 jQuery tableSorter 插件过滤项目的选择器

标签 javascript jquery tablesorter

这最近进入了我的脑海,说实话,我认为值得问一下。这是事情..

我有一个表,就像任何其他带有过滤器小部件的 jquery tablesorter 插件的普通表一样。在表格列的最右边,我放置了一个复选框,在该列的上方,在该列的表格标题上,我有另一个复选框,它有一个链接到它的函数,这样当它被单击时,所有复选框都会更新为该复选框的值。 这不是很花哨或复杂,我有两种方法来完成这个..要么使用 jquery 选择器,要么使用普通的旧 JavaScript。

所以这就是我想要做的..我想过滤表的元素,然后单击标题上的复选框,并且我想影响使用插件过滤的行的复选框。

有人对此有话要说吗? 谢谢。

最佳答案

我已经为此设置了一个演示 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' }
        }
    });
});

只需确保包含 parser-input-select.js file

关于javascript - 使用 jQuery tableSorter 插件过滤项目的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26028209/

相关文章:

Jquery - 基于组合框的过滤列表框

javascript - 如何在javascript中检测复选框是否被选中

jquery - 如何使用 tablesorter 确定在表中单击了哪一列?

JQuery tablesorter 自定义解析器到所有 header ?

javascript - jQuery Tablesorter 不排序列

javascript - Vue - 深入观察一系列对象并计算变化?

javascript - Firebase 云功能在发送电子邮件之前结束

javascript - JQuery计算中的简单条件

javascript - 重置元素 'odd' 或 'even' 状态?

javascript - 尝试上传到 Amazon S3 时出现 "Unsupported body payload object"