Jquery 数据表将搜索过滤器添加到 header 仅针对几列

Jquery Datatables add search filter to header for a few columns only

我在 table 的 header 中添加了一个搜索框,该搜索框由 Datatables 插件生成。它在 header 中的每一列上方放置了一个搜索框。我只需要将搜索过滤器应用于几列。假设第 0 列和第 6 列需要搜索过滤器,而其他列不需要。有没有办法做到这一点?我无法在任何地方找到关于如何实现这一目标的答案,而且我是编码新手,所以我还没有找到实现这一目标的方法。下面的代码我用来创建搜索 header.

$('#OBTable thead tr')
        .clone(true)
        .addClass('filters')
        .appendTo('#OBTable thead');
                
                var table = $('#OBTable').dataTable({
                
                 "ajax": {
                    "url": "SelectData.php",
                    "dataSrc": "data",
                },
                 orderCellsTop: true,
                 select: true,
                   initComplete: function () {
            var api = this.api();
 
            // For each column
            api
                .columns()
                .eq(0)
                .each(function (colIdx) {
                    // Set the header cell to contain the input element
                    var cell = $('.filters th').eq(
                        $(api.column(colIdx).header()).index()
                    );
                    var title = $(cell).text();
                    $(cell).html('<input type="text" placeholder="' + title + '" />');
 
                    // On every keypress in this input
                    $(
                        'input',
                        $('.filters th').eq($(api.column(colIdx).header()).index())
                    )
                        .off('keyup change')
                        .on('keyup change', function (e) {
                            e.stopPropagation();
 
                            // Get the search value
                            $(this).attr('title', $(this).val());
                            var regexr = '({search})'; //$(this).parents('th').find('select').val();
 
                            var cursorPosition = this.selectionStart;
                            // Search the column for that value
                            api
                                .column(colIdx)
                                .search(
                                    this.value != ''
                                        ? regexr.replace('{search}', '(((' + this.value + ')))')
                                        : '',
                                    this.value != '',
                                    this.value == ''
                                )
                                .draw();
 
                            $(this)
                                .focus()[0]
                                .setSelectionRange(cursorPosition, cursorPosition);
                        });
                });
        },

数据表columns() API function can be given a column selector。这可以采取各种形式。一种选择是提供一个数组,其中包含要包含搜索过滤器的列的索引。

因此,例如您可以指定:

.columns( [0, 6] )

我建议你也看看下面官方例子中的方法:

您还应该删除 eq(0) 调用 - 我不认为这是有效的 DataTables API 函数。

正如您从文档中看到的那样,您还可以使用其他选项 - 例如在相关标题单元格中提供 class 名称 - 但索引数组可能是最简单的方法。


更新

follow-up问题是:

now the rest of the columns contain the name of the column header.. Is there a way to leave those empty?

您没有向我们展示您的 HTML table 长什么样,所以这是一个猜测,基于您从一个标题行开始的假设...我认为这是一个合理假设...

如果您想避免重复列标题,则需要在执行 colone().

后清除克隆标题行的内容

一种方法如下:

$('#OBTable thead tr:eq(1) th').text("");

这意味着克隆的行开始时任何标题单元格中都没有值。

这需要在执行克隆操作的命令之后,创建 DataTable 之前添加。