草庐IT

jQuery 数据表 : Delay search until 3 characters been typed OR a button clicked

coder 2023-07-06 原文

有没有输入3个字符后才开始搜索的选项?

我为显示 20,000 个条目的同事编写了一个 PHP 脚本,他们提示说,在输入单词时,前几个字母会导致所有内容卡住。

另一种方法是通过单击按钮而不是通过字符输入来开始搜索。

下面是我当前的代码:

$("#my_table").dataTable( {
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bAutoWidth": false,
        "aoColumns": [
                /* qdatetime */   { "bSearchable": false },
                /* id */          null,
                /* name */        null,
                /* category */    null,
                /* appsversion */ null,
                /* osversion */   null,
                /* details */     { "bVisible": false },
                /* devinfo */     { "bVisible": false, "bSortable": false }
        ],
        "oLanguage": {
                "sProcessing":   "Wait please...",
                "sZeroRecords":  "No ids found.",
                "sInfo":         "Ids from _START_ to _END_ of _TOTAL_ total",
                "sInfoEmpty":    "Ids from 0 to 0 of 0 total",
                "sInfoFiltered": "(filtered from _MAX_ total)",
                "sInfoPostFix":  "",
                "sSearch":       "Search:",
                "sUrl":          "",
                "oPaginate": {
                        "sFirst":    "<<",
                        "sLast":     ">>",
                        "sNext":     ">",
                        "sPrevious": "<"
                },
                "sLengthMenu": 'Display <select>' +
                        '<option value="10">10</option>' +
                        '<option value="20">20</option>' +
                        '<option value="50">50</option>' +
                        '<option value="100">100</option>' +
                        '<option value="-1">all</option>' +
                        '</select> ids'
        }
} );

最佳答案

版本 1.10 的解决方案 -

在此处寻找完整答案但没有找到之后,我写了这篇文章(利用文档中的代码,以及此处的一些答案)。

下面的代码用于延迟搜索,直到至少输入 3 个字符:

// Call datatables, and return the API to the variable for use in our code
// Binds datatables to all elements with a class of datatable
var dtable = $(".datatable").dataTable().api();

// Grab the datatables input box and alter how it is bound to events
$(".dataTables_filter input")
    .unbind() // Unbind previous default bindings
    .bind("input", function(e) { // Bind our desired behavior
        // If the length is 3 or more characters, or the user pressed ENTER, search
        if(this.value.length >= 3 || e.keyCode == 13) {
            // Call the API search function
            dtable.search(this.value).draw();
        }
        // Ensure we clear the search if they backspace far enough
        if(this.value == "") {
            dtable.search("").draw();
        }
        return;
    });

关于jQuery 数据表 : Delay search until 3 characters been typed OR a button clicked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5548893/

有关jQuery 数据表 : Delay search until 3 characters been typed OR a button clicked的更多相关文章

随机推荐