【问题标题】:getting a script to run for multiple datatables让脚本为多个数据表运行
【发布时间】:2016-07-07 18:14:45
【问题描述】:

我有几个表是我使用 Jade 创建的,此外还有 bootstrap 和 jQuery DataTables。我已经包含了一些代码来使单个列过滤成为可能;但是,它仅在第一个表上完全有效,即使它们都由应用该函数的相同 ID 标识。代码如下:

script.
        $(document).ready(function() {
            $('.datatable tfoot th').each( function () {
                var title = $(this).text();
                $(this).html( '<input type="text" placeholder="Search ' +title+'" />');
            });

            var table = $('.datatable').DataTable();

            table.columns().every( function () {
                var that = this;

                $( 'input', this.footer() ).on( 'keyup change', function () {
                    if ( that.search() !== this.value) {
                        that
                            .search( this.value )
                            .draw();
                    }
                } );
            } );
        } );

根据可以在 DataTables API 中找到的示例:https://datatables.net/examples/api/multi_filter.html

我的表都用 ID .datatable 标识,并且它们具有与代码一起使用的相应页眉和页脚。两个表都显示文本框作为函数的结果,但只有第一个实际执行过滤。任何关于我如何成功地为多个表使用此功能的建议将不胜感激,谢谢!

我的完整代码如下:

extends layout

block content
head
    meta(charset='utf-8')
    meta(http-equiv='X-UA-Compatible', content='IE=edge')
    meta(name='viewport', content='width=device-width, initial-scale=1')
    link(href='css/bootstrap.min.css', rel='stylesheet')
    link(rel='stylesheet', href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css')
    script(src='https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js')
    script(src='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js')
    title Bootstrap Example
body
    script(src='http://code.jquery.com/jquery.js')
    script(src='js/bootstrap.min.js')
    .container
        .jumbotron
            .mark
                h2 Bootstrap/Datatables Test Page
        .megasearch
            h3 Search All Tables
            input#Search_All
        br
        br



        // TABLE ONE 
        table.datatable.table.table-hover.table-striped.table-bordered
            thead
                tr
                    th #
                    th Name
                    th Type
                    th Battery
            tfoot
                tr
                    th #
                    th Name
                    th Spice
                    th Approval 
            tbody
                tr
                    th(scope='row') 1
                    td BOSS RC-1
                    td Loop
                    td Yes
                tr
                    th(scope='row') 2
                    td Line 6 DL4
                    td Delay
                    td Yes
                tr
                    th(scope='row') 3
                    td Polara
                    td Reverb
                    td Yes
        br
        br              
        // TABLE TWO 
        table.datatable.table.table-hover.table-striped.table-bordered
            thead
                tr
                    th #
                    th Name
                    th Spice
                    th Approval
            tfoot
                tr
                    th #
                    th Name
                    th Spice
                    th Approval                     
            tbody
                tr
                    th(scope='row') 1
                    td Pickle
                    td No
                    td Yes
                tr
                    th(scope='row') 2
                    td Jalapeno
                    td Yes
                    td Yes
                tr
                    th(scope='row') 3
                    td Pickled Onions
                    td No
                    td Yes
                tr
                    th(scope='row') 3
                    td Pickled Onions
                    td No
                    td Yes                              
                tr
                    th(scope='row') 3
                    td Pickled Onions
                    td No
                    td Yes  
                tr
                    th(scope='row') 3
                    td Pickled Onions
                    td No
                    td Yes  
                tr
                    th(scope='row') 3
                    td Pickled Onions
                    td No
                    td Yes  
                tr
                    th(scope='row') 3
                    td Pickled Onions
                    td No
                    td Yes  
                tr
                    th(scope='row') 3
                    td Pickled Onions
                    td No
                    td Yes  
                tr
                    th(scope='row') 3
                    td Pickled Onions
                    td No
                    td Yes  
                tr
                    th(scope='row') 3
                    td Pickled Onions
                    td No
                    td Yes                                                                                                  

    // SCRIPTS REQUIRED FOR DATATABLES AND FUNCTION FOR SEARCH_ALL      
    script(src='https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css')
    script(src='https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js')
    script(src='https://cdn.datatables.net/1.10.12/js/jquery.dataTables.js')
    script.
        $(document).ready(function() {
            $('.datatable').DataTable({
                "pagingType": "full_numbers"
            });


            // THIS IS THE GLOBAL SEARCH CODE
            $.fn.dataTableExt.oApi.fnFilterAll=function(oSettings,sInput,iColumn,bRegex,bSmart){
                var settings = $.fn.dataTableSettings;
                for (var i = 0; i < settings.length; i++) {
                    settings[i].oInstance.fnFilter(sInput, iColumn, bRegex, bSmart);
                }
            };

            var table = $(".datatable").dataTable();


            $("#Search_All").keyup(function () {
                // Filter on the column (the index) of this element
                table.fnFilterAll(this.value);
            });
        });

    script.
        $(document).ready(function() {
            $('.datatable tfoot th').each( function () {
                var title = $(this).text();
                $(this).html( '<input type="text" placeholder="Search ' +title+'" />');
            });

            var table = $('.datatable').DataTable();

            table.columns().every( function () {
                var that = this;

                $( 'input', this.footer() ).on( 'keyup change', function () {
                    if ( that.search() !== this.value) {
                        that
                            .search( this.value )
                            .draw();
                    }
                } );
            } );
        } );

【问题讨论】:

  • 1) .datatable 不是 ID;这是一堂课。 2) 如果您使用 DataTables 的类名进行选择,则要使用的正确类是 .dataTable(大写为 T)。如果无法查看您的表结构,就很难判断可能是什么问题。
  • 我刚刚将我的完整代码添加到帖子中。

标签: jquery html twitter-bootstrap datatables pug


【解决方案1】:

您刚刚将该类添加到数据表中。对两者都使用唯一的 id 属性。比如datatable1、datatable2。

然后将你的代码包装在一个以 id 作为参数的函数中:

function initMyDataTable(id){
        $('#'+id+' tfoot th').each( function () {
            var title = $(this).text();
            $(this).html( '<input type="text" placeholder="Search ' +title+'" />');
        });

        var table = $('#'+id).DataTable();

        table.columns().every( function () {
            var that = this;

            $( 'input', this.footer() ).on( 'keyup change', function () {
                if ( that.search() !== this.value) {
                    that
                        .search( this.value )
                        .draw();
                }
            } );
        } );
}

现在您可以使用所需表的 ID 调用此函数:

initMyDataTable('datatable1');
initMyDataTable('datatable2');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2017-12-12
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    相关资源
    最近更新 更多