【问题标题】:jQuery datatable column filter with sort带有排序的jQuery数据表列过滤器
【发布时间】:2019-10-28 16:07:44
【问题描述】:

我实现了一个小网格来测试 JQuery DataTable 列过滤器,但是当我执行站点时,网格标题和包含输入字段的行都包含排序类属性

我正在使用这个版本的 Jquery 和 bootstrap

  1. jquery版本:v2.1.4
  2. JQuery 数据表:1.10.19
  3. 引导程序:v4

我的代码JS代码:

var table = $('#datatable1').DataTable({
    "orderCellsTop": true,
    "responsive": true
});

$('#datatable1 thead tr').clone(true).appendTo('#datatable1 thead');
$('#datatable1 thead tr:eq(1) th').each(function (i) {
    var title = $(this).text();
    $(this).html('<input type="text" class="col-md-11 form-control" placeholder="Filtrar ' + title + '" />');

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

// Select2
$('.dataTables_length select').select2({ minimumResultsForSearch: Infinity });

});

我的html:

    <table id="datatable1" class="table display responsive nowrap table-bordered">
        <thead>
            <tr>
                <th class="wd-15p">First name</th>
                <th class="wd-15p">Last name</th>
                <th class="wd-20p">Position</th>
                <th class="wd-15p">Start date</th>
                <th class="wd-10p">Salary</th>
                <th class="wd-25p">E-mail</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger</td>
                <td>Nixon</td>
                <td>System Architect</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
                <td>t.nixon@datatables.net</td>
            </tr>
            <tr>
                <td>Garrett</td>
                <td>Winters</td>
                <td>Accountant</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
                <td>g.winters@datatables.net</td>
            </tr>
            <tr>
                <td>Ashton</td>
                <td>Cox</td>
                <td>Junior Technical Author</td>
                <td>2009/01/12</td>
                <td>$86,000</td>
                <td>a.cox@datatables.net</td>
            </tr>
        </tbody>
    </table>

【问题讨论】:

    标签: javascript jquery html datatables


    【解决方案1】:
    var table = $('#datatable1').DataTable({
        "orderCellsTop": true,
        "responsive": true
    });
    
    $('#datatable1 thead tr')
      .clone(true)
      .find('th')
        .removeClass('sorting_asc sorting_asc sorting')
        .off('click')
        .end()
      .appendTo('#datatable1 thead');
    
    $('#datatable1 thead tr:eq(1) th').each(function (i) {
        var title = $(this).text();
        $(this).html('<input type="text" class="col-md-11 form-control" placeholder="Filtrar ' + title + '" />');
    
        $('input', this).on('keyup change', function () {
            if (table.column(i).search() !== this.value) {
                table
                    .column(i)
                    .search(this.value)
                    .draw();
            }
        });
    });
    

    【讨论】:

    • 我整个下午都在努力完成这项工作。干得好!!!!
    • 您也可以使用 Datatable API,例如当您执行 table.column(i).search() 时,table 引用是 DataTable,但如果您要在其他地方引用,您可以稍后再执行 $('#datatable1).dataTable().api() .只是灵活性的另一个指针。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 2019-05-08
    • 2013-09-22
    相关资源
    最近更新 更多