簡體   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