【问题标题】:Datatable draw() method not working on column filterDatatable draw() 方法不适用于列过滤器
【发布时间】:2016-08-30 09:40:53
【问题描述】:

花了几天时间尝试了我在互联网上找到的许多解决方案后,我 在这里问。

当点击搜索按钮时,我的表单会显示一个包含数据的表格。该表有 8 列,我想在其中 3 列上添加一个文本输入,使用该文本输入应用具有列数据的过滤器。为了更好地了解我的需求,请访问JsFiddle showing a working column filter

所以,我尝试了上面链接和Datatable exemple 的解决方案,但没有成功,找不到我做错了什么。

这是我的代码:

<table id="EquipmentTable" class="table table-striped table-bordered bottom-buffer" width="100%">
    <thead>
        <tr>
            <th><input type="checkbox" name="select_all" value="1" id="checkAll" class="text-center" onclick="$.fn.backboneSearch.checkAllResult()"></th>
            <th>Equipement</th>
            <th>Famille d'équipement</th>
            <th>Gamme d'équipement</th>
            <th>Etat</th>
            <th>UI</th>
            <th>Site de stockage</th>
            <th>Salle technique</th>
            <th></th>
        </tr>
    </thead>
    <tfoot id="backboneSearchtfoot">
        <tr id="filterrow">
            <th></th>
            <th id="textFilter1" class="textFilter"></th>
            <th id="textFilter2" class="textFilter"></th>
            <th id="textFilter3" class="textFilter"></th>
            <th class="listFilter"></th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
        </tr>
    </tfoot>
</table>

// Setup - add a text input to each footer cell
$('#EquipmentTable tfoot th.textFilter').each(function (i) {
    $(this).html('<input type="text" data-index="' + i + '" />');
});

equipmentTable = $('#EquipmentTable').DataTable({
    aaData: result,
    aoColumns: [
        { mData: 'Identifier' },
        { mData: 'Mnemo' },
        { mData: 'FamGam.Family' },
        { mData: 'FamGam.Gamme' },
        { mData: 'dataState.Libelle' },
        { mData: 'IdentifierUI' },
        { mData: 'TechnicalRoom.InterventionUnitySenderSite' },
        { mData: 'IdentifierTechnicalRoom' },
    ],
    bDestroy: true,
    bFilter: false,
    bRetrieve: true,
    buttons: [{
        className: 'btn-warning',
        columns: [1, 2, 3, 4, 5, 6],
        extend: 'excel',
        fieldSeparator: ';',
        text: '<span class="glyphicon glyphicon-export"></span> Export'
    }],
    dom: 'Bfrtip',
    language: { /*not useful to show*/ },
    stateSave: true,
    bProcessing: true
});

$(equipmentTable.table().container()).on('keyup', 'tfoot th.textFilter input', function () {
    equipmentTable.column($(this).data('index'))
                  .search(this.value)
                  .draw();
});

aaData使用的result是我在ajax上搜索Rest方法成功得到的json。我在该成功方法上填充表格。

所以我的问题是:我做错了什么或误解了什么? 我试图将对象equipmentTable.column($(this).data('index')).search(this.value) 与示例中返回的内容进行比较并获得等效对象。这就是为什么我几乎可以肯定问题出在 draw() 方法上。

感谢您的帮助。

【问题讨论】:

    标签: jquery datatables datatables-1.10 jquery-2.0


    【解决方案1】:

    这是工作的fiddle

    首先,您的搜索不起作用,因为您将 bFilter 设置为 false。然后只需删除此行或将此参数设置为 true :

    bFilter: true,
    

    但还不够。用于绘制输入文本列的循环将不起作用,因为列索引从 0 开始。然后,如果您将第一列设置为第二列,并在第一个输入上进行搜索,则排序将在列 0 上完成。然后我在您的数据索引中添加了 +1 :

    $(equipmentTable.table().container()).on('keyup', 'tfoot tr th.textFilter input', function () {
        equipmentTable.column($(this).data('index') + 1)
                      .search(this.value)
                      .draw();
    });
    

    希望对你有帮助。

    【讨论】:

    • 谢谢您的回答。我试过了,也有同样的问题。 myTable.column( colIdx ).search( this.value ) 返回的对象与我的代码返回的对象完全相同,但 draw() 似乎什么也没做。
    • 在此之前,您可以尝试在事件委托 keyup 中添加 tr 吗? tfoot tr th.textFilter input
    • 我也尝试了 tr,但没有成功。我做了一个 JSFiddle:jsfiddle.net/6vcd4ct5
    • 好的,我知道了,我会更新我的答案并给你新的小提琴
    • 哇!非常感谢,它正在工作!我没想到 bFilter 会成为问题。
    猜你喜欢
    • 1970-01-01
    • 2018-11-09
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    相关资源
    最近更新 更多