数据表draw()方法不适用于列过滤器

dtl*_*lvd 1 jquery datatables jquery-2.0 datatables-1.10

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

我有一个表单,在单击搜索按钮时显示包含数据的表.该表有8列,其中3列我想添加一个文本输入,使用该文本输入应用了具有列数据的过滤器.为了更好地理解我的需求,JsFiddle显示了一个工作列过滤器.

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

有我的代码:

<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>
Run Code Online (Sandbox Code Playgroud)

// 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();
});
Run Code Online (Sandbox Code Playgroud)

result所用aaData是一个JSON我上搜索休息方法的AJAX成功.我在那个成功方法上填充了表格.

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

谢谢您的帮助.

Joh*_*ohn 6

这是工作小提琴

首先,您的搜索无效,因为您将bFilter设置为false.然后只需删除此行或将此参数设置为true:

bFilter: true,
Run Code Online (Sandbox Code Playgroud)

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

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

希望能帮助到你.