繁体   English   中英

允许用户在列搜索栏、数据表中输入正则表达式

[英]Allow user to type regular expression into column search bar, datatables

我希望能够在我的各个列搜索栏中键入正则表达式,例如^.{2}06.{4,}$ 作为参考,当我使用 DataTables 的客户端版本时,我能够做到这一点,但是由于切换到服务器端来处理我的更大的表,我无法复制它。

这是我从https://www.datatables.net/examples/api/multi_filter.html构建的示例的链接

这是我当前用于表初始化的代码

var table = $('#results').dataTable({
        processing: true,
        serverSide: true,
        ajax: 'getTable.php',
        orderCellsTop: true,
        fixedHeader: true,
        "search" : { //this is what I have changed from the DataTables example//*
            "regex": true                                                     //*
        },                                                                    //*
        createdRow: function ( row ) {                                        //*
            $(row).addClass("hover");                                         //*
        },                                                                    //*
        //***********************************************************************
        initComplete: function () {
            this.api()
                .columns()
                .every(function () {
                    var that = this;
                    
                    $('input', this.footer()).on('keyup change clear', function () {
                        if (that.search() != this.value){
                            that.search( this.value ).draw();
                            
                            //I have also tried setting the optional regex value of the 
                            //search function to true with no success
                            //that.search( this.value , true ).draw();
                        }
                    });
                });
        }

    });

如果有人可以帮助我,将不胜感激

查看官方手册中Server-Side Processing页面的Sent Parameters部分。

您将看到两件相关的事情:

search[regex] - 如果全局过滤器应被视为高级搜索的正则表达式,则为 true,否则为 false。

和:

columns[i][search][regex] - 指示该列的搜索词是否应视为正则表达式 (true) 或不 (false) 的标志。

因此,您的服务器端路由处理程序需要解析响应中发送的参数,以便为每个列过滤器提取这些布尔值。 这些值将确定是否应将列搜索词视为正则表达式。

您还需要使用 DataTables searchCols选项来指示应将哪些列的搜索词视为正则表达式 -您不能只使用search: { "regex": true }选项,因为这仅适用于全局搜索字段

一个例子:

searchCols: [
  { regex: false },
  { regex: true },
  { regex: true },
  { regex: true },
  { regex: true },
  { regex: true }
]

上面的示例假设一个包含 6 列的表,并且它们的所有搜索词(第一列除外)都将被视为正则表达式。

如上所述,可以在columns[i][search][regex]的请求正文中找到数据。


数据表文档中有一个警告值得重复:

与全局搜索一样,出于性能原因,通常服务器端处理脚本不会在大型数据集上执行正则表达式搜索,但在技术上是可行的,并且由您的脚本自行决定。


最后注:

当您使用serverSide: true时,客户端(DataTables)搜索逻辑将被忽略。 这包括您的问题中包含的 DataTables search() API 调用:

that.search( this.value ).draw();

这是对 DataTable 使用服务器端处理的基本要点之一:分页、排序和过滤的所有逻辑都在服务器上处理 - DataTables 不提供任何逻辑。 你必须自己提供所有这些逻辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM