2

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

我有一个表格,当点击搜索按钮时,它会显示一个包含数据的表格。该表有 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>

// 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();
});

使用resultaaData是一个json,我在搜索Rest方法的ajax成功。我在该成功方法上填充表格。

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

谢谢您的帮助。

4

1 回答 1

8

这是工作小提琴

首先,您的搜索不起作用,因为您将 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();
});

希望能帮助到你。

于 2016-08-30T10:13:03.980 回答