Ale*_*ber 73 jquery datatables
只有在输入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'
}
} );
Run Code Online (Sandbox Code Playgroud)
Ric*_*ard 74
注意:这是针对更早版本的数据表,请参阅jQuery datatables v1.10及更高版本的答案.
这将修改输入框的行为,仅在按下任何一个返回或搜索中至少有3个字符时进行过滤:
$(function(){
var myTable=$('#myTable').dataTable();
$('.dataTables_filter input')
.unbind('keypress keyup')
.bind('keypress keyup', function(e){
if ($(this).val().length < 3 && e.keyCode != 13) return;
myTable.fnFilter($(this).val());
});
});
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到它:http://jsbin.com/umuvu4/2.我不知道为什么dataTables人员都同时绑定了keypress和keyup,但是我要重写它们以保持兼容,尽管我认为keyup已经足够了.
希望这可以帮助!
ran*_*ame 62
版本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;
});
Run Code Online (Sandbox Code Playgroud)
小智 32
为什么不试试这个Stony的答案扩展版:)
var searchWait = 0;
var searchWaitInterval;
$('.dataTables_filter input')
.unbind('keypress keyup')
.bind('keypress keyup', function(e){
var item = $(this);
searchWait = 0;
if(!searchWaitInterval) searchWaitInterval = setInterval(function(){
if(searchWait>=3){
clearInterval(searchWaitInterval);
searchWaitInterval = '';
searchTerm = $(item).val();
oTable.fnFilter(searchTerm);
searchWait = 0;
}
searchWait++;
},200);
});
Run Code Online (Sandbox Code Playgroud)
这将延迟搜索,直到用户停止输入.
希望能帮助到你.
Cha*_*ehn 10
以下是如何使用版本1.10中的api更改来处理它
var searchbox = $('#promogrid_filter input');
var pgrid = $('#promogrid').DataTable();
//Remove default datatable logic tied to these events
searchbox.unbind();
searchbox.bind('input', function (e) {
if(this.value.length >= 3) {
pgrid.search(this.value).draw();
}
if(this.value == '') {
pgrid.search('').draw();
}
return;
});
Run Code Online (Sandbox Code Playgroud)
这是一个类似插件的脚本,可以扩展数据表.
jQuery.fn.dataTableExt.oApi.fnSetFilteringEnterPress = function ( oSettings ) {
var _that = this;
this.each( function ( i ) {
$.fn.dataTableExt.iApiIndex = i;
var
$this = this,
oTimerId = null,
sPreviousSearch = null,
anControl = $( 'input', _that.fnSettings().aanFeatures.f );
anControl
.unbind( 'keyup' )
.bind( 'keyup', function(e) {
if ( anControl.val().length > 2 && e.keyCode == 13){
_that.fnFilter( anControl.val() );
}
});
return this;
} );
return this;
}
Run Code Online (Sandbox Code Playgroud)
用法:
$('#table').dataTable().fnSetFilteringEnterPress();
Run Code Online (Sandbox Code Playgroud)
要做的是在用户在搜索框中输入最小字符后调用服务器调用,您可以按照Allan的建议:
自定义fnSetFilteringDelay()插件API函数,在设置过滤器之前在字符串长度上添加额外条件,同时考虑空字符串输入以清除过滤器
因此,对于至少3个字符,只需将插件中的第19行更改为:
if ((anControl.val().length == 0 || anControl.val().length >= 3) && (sPreviousSearch === null || sPreviousSearch != anControl.val())) {
Run Code Online (Sandbox Code Playgroud)
这适用于DataTables 10.0.4:
var table = $('#example').DataTable();
$(".dataTables_filter input")
.unbind()
.bind('keyup change', function(e) {
if (e.keyCode == 13 || this.value == "") {
table
.search(this.value)
.draw();
}
});
Run Code Online (Sandbox Code Playgroud)
小智 5
我的数据表版本1.10.10
我做了一些更改,现在可以使用了。因此,我正在共享,因为很难使其适用于1.10.10版本。感谢cale_b,Stony和Sam Barnes。查看代码,看看我做了什么。
var searchWait = 0;
var searchWaitInterval;
$('.dataTables_filter input')
.unbind() // leave empty here
.bind('input', function(e){ //leave input
var item = $(this);
searchWait = 0;
if(!searchWaitInterval) searchWaitInterval = setInterval(function(){
if(searchWait >= 3){
clearInterval(searchWaitInterval);
searchWaitInterval = '';
searchTerm = $(item).val();
oTable.search(searchTerm).draw(); // change to new api
searchWait = 0;
}
searchWait++;
},200);
});
Run Code Online (Sandbox Code Playgroud)
小智 5
以前的解决方案都不适合我,所以我制作了这个修改版本,还添加了去抖功能。与所有最新版本完美配合。
您可以简单地更改或删除最小字符限制和去抖超时值。
jQuery(document).on( 'init.dt', function (e, settings) {
var dti = jQuery('.dataTables_filter input');
var api = new jQuery.fn.dataTable.Api( settings );
var dbn = null;
dti.off().on('input', function(e) {
clearTimeout( dbn );
var str = this.value;
dbn = setTimeout(function(){
if(str.length > 2 || e.keyCode == 13) api.search( str ).draw();
if(str == '') api.search( '' ).draw();
}, 300);
return;
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65820 次 |
| 最近记录: |