直到输入3个字符或点击一个按钮

直到输入3个字符或点击一个按钮

本文介绍了jQuery DataTables:延迟搜索,直到输入3个字符或点击一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有选择,只有在输入3个字符后才能开始搜索?

Is there please an option to start the search only after 3 characters have been typed in?

我已经为同事编写了一个PHP脚本,显示了20,000个条目,抱怨,当打字时,前几个字母会导致所有内容冻结(在Windows XP上使用最新的Chrome浏览器)。

I have written a PHP-script for colleagues displaying 20,000 entries and they complain, that when typing a word, the first few letters cause everything to freeze (using latest Chrome browser on Windows XP).

我在尚未找到解决方案。

I've searched in the DataTables forum, haven't found a solution yet.

另一种方法是让搜索由点击的按钮开始,而不是通过字符输入。

An alternative would be to have the search to be started by a button clicked and not by character typing.

以下是我当前的代码: pt

Below is my current code:

$("#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 -

Solution for version 1.10 -

在这里找到完整的答案,找不到一个,我写了(使用文档中的代码,这里有一些答案)。

After looking here for a complete answer and not finding one, I've written this (utilizing code from the documentation, and a few answers here).

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

The below code works to delay searching until at least 3 characters are entered:

// 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 DataTables:延迟搜索,直到输入3个字符或点击一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:58