简体   繁体   English

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

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

I would like to be able to type a regular expression, for example ^.{2}06.{4,}$ into my individual column search bars.我希望能够在我的各个列搜索栏中键入正则表达式,例如^.{2}06.{4,}$ For reference I was able to do this when I was using the client side version of DataTables, but since switching over to server side to handle my larger table I haven't been able to replicate this.作为参考,当我使用 DataTables 的客户端版本时,我能够做到这一点,但是由于切换到服务器端来处理我的更大的表,我无法复制它。

Here is a link to the example I have built off of https://www.datatables.net/examples/api/multi_filter.html这是我从https://www.datatables.net/examples/api/multi_filter.html构建的示例的链接

And here is my current code for the initialization of the table这是我当前用于表初始化的代码

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

    });

If anyone could help me with this it would be greatly appreciated如果有人可以帮助我,将不胜感激

Look at the Sent Parameters section of the Server-Side Processing page in the official manual.查看官方手册中Server-Side Processing页面的Sent Parameters部分。

You will see two things of relevance:您将看到两件相关的事情:

search[regex] - true if the global filter should be treated as a regular expression for advanced searching, false otherwise. search[regex] - 如果全局过滤器应被视为高级搜索的正则表达式,则为 true,否则为 false。

And:和:

columns[i][search][regex] - Flag to indicate if the search term for this column should be treated as regular expression (true) or not (false). columns[i][search][regex] - 指示该列的搜索词是否应视为正则表达式 (true) 或不 (false) 的标志。

So, your server-side route handler needs to parse the sent parameters in the response, to extract these booleans for each of your column filters.因此,您的服务器端路由处理程序需要解析响应中发送的参数,以便为每个列过滤器提取这些布尔值。 These values will determine if the column search term should be treated as a regex or not.这些值将确定是否应将列搜索词视为正则表达式。

You also need to use the DataTables searchCols option to indicate which of your columns' search terms should be treated as regexes - you cannot only use the search: { "regex": true } option because that is only for the global search field .您还需要使用 DataTables searchCols选项来指示应将哪些列的搜索词视为正则表达式 -您不能只使用search: { "regex": true }选项,因为这仅适用于全局搜索字段

An example:一个例子:

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

The above example assumes a table with 6 columns, and all of their search terms - except for the first column - are to be treated as regexes.上面的示例假设一个包含 6 列的表,并且它们的所有搜索词(第一列除外)都将被视为正则表达式。

The data can be found in the request body at columns[i][search][regex] , as noted above.如上所述,可以在columns[i][search][regex]的请求正文中找到数据。


There is a warning in the Data Tables documentation which is worth repeating:数据表文档中有一个警告值得重复:

As with global search, normally server-side processing scripts will not perform regular expression searching for performance reasons on large data sets, but it is technically possible and at the discretion of your script.与全局搜索一样,出于性能原因,通常服务器端处理脚本不会在大型数据集上执行正则表达式搜索,但在技术上是可行的,并且由您的脚本自行决定。


Final Note:最后注:

Client-side (DataTables) search logic is ignored when you use serverSide: true .当您使用serverSide: true时,客户端(DataTables)搜索逻辑将被忽略。 That includes the DataTables search() API call included in your question:这包括您的问题中包含的 DataTables search() API 调用:

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

That is one of the fundamental points about using server-side processing for your DataTable: All logic for paging, sorting and filtering is handled on the server - and none of it is provided by DataTables.这是对 DataTable 使用服务器端处理的基本要点之一:分页、排序和过滤的所有逻辑都在服务器上处理 - DataTables 不提供任何逻辑。 You have to provide all that logic yourself.你必须自己提供所有这些逻辑。

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

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