我正在使用jQuery的Datatables插件。我正在为ASP.Net项目使用服务器端处理功能。
每当我尝试在全局搜索中键入某些内容时,它都会令人沮丧,我键入的每个字母都会调用服务器端方法并为我带来结果。
当要过滤的数据很大时,它会更加令人沮丧。
在Enter键的按键上有任何选项或方法可以调用搜索方法,而在任何按键上都没有?
要做的只是解除绑定DataTables放在输入框上的keypress事件处理程序,然后添加您自己的事件处理程序,当检测到返回键(keyCode 13)时,它将调用fnFilter。
$("div.dataTables_filter input").keyup( function (e) {
if (e.keyCode == 13) {
oTable.fnFilter( this.value );
}
} );
其他
$(document).ready(function() {
var oTable = $('#test').dataTable( {
"bPaginate": true,
"bLengthChange": true,
"bFilter": true,
"bSort": true,
"bInfo": true,
"bAutoWidth": true } );
$('#test_filter input').unbind();
$('#test_filter input').bind('keyup', function(e) {
if(e.keyCode == 13) {
oTable.fnFilter(this.value);
}
});
} );
我也尝试了Techie的代码。当然,我也收到了错误消息fnFilter is not a function
。实际上,oTable.fnFilter(this.value);
通过替换行oTable.search( this.value ).draw();
就可以完成这项工作,但是在我的情况下,取消绑定/绑定功能是在初始化服务器端搜索表之前执行的。因此,我将unbind / bind函数放入initComplete
回调函数中,一切正常:
$(document).ready(function() {
var oTable = $('#test').dataTable( {
"...": "...",
"initComplete": function(settings, json) {
$('#test_filter input').unbind();
$('#test_filter input').bind('keyup', function(e) {
if(e.keyCode == 13) {
oTable.search( this.value ).draw();
}
});
}
});
});
我最终在Datatables(v1.10.15)中做到了这一点。如果输入为空,我还防止退格键和删除按钮发送搜索请求。
$.extend( $.fn.dataTable.defaults, {
"initComplete": function(settings, json) {
var api = this.api();
var textBox = $('#datatable_filter label input');
textBox.unbind();
textBox.bind('keyup input', function(e) {
if(e.keyCode == 8 && !textBox.val() || e.keyCode == 46 && !textBox.val()) {
// do nothing ¯\_(ツ)_/¯
} else if(e.keyCode == 13 || !textBox.val()) {
api.search(this.value).draw();
}
});
}
});
以下是使用1.10版中的api更改来处理它的方法
//prevents form submissions if press ENTER in textbox
$(window).keydown(function (event) {
if (event.keyCode == 13) {
event.preventDefault();
return false;
}
});
var searchbox = $('#ordergrid_filter input');
searchbox.unbind();
searchbox.bind('keyup', function (e) {
if (e.keyCode == 13) {
ogrid.search(this.value).draw();
}
});
var uitool = '';
searchbox.on("mousedown", function () {
uitool = 'mouse';
});
searchbox.on("keydown", function () {
uitool = 'keyboard';
});
//Reset the search if the "x" is pressed in the filter box
searchbox.bind('input', function () {
if ((this.value == "") && (ogrid.search() != '') && (uitool == 'mouse')) {
ogrid.search('').draw();
return;
}
});
这是我设法做到的方法:
$(document).on('focus', '.dataTables_filter input', function() {
$(this).unbind().bind('keyup', function(e) {
if(e.keyCode === 13) {
oTable.search( this.value ).draw();
}
});
});
终于用这种方式工作了
var oTable = $('#table-name').dataTable({
processing: true,
serverSide: true,
ajax: "file.json",
initComplete: function() {
$('#table-name_filter input').unbind();
$('#table-name_filter input').bind('keyup', function(e) {
if(e.keyCode == 13) {
oTable.fnFilter(this.value);
}
});
},
....
干杯
您可以使用jQuery。
// get the global text
var globalSearch = $("#txtGlobal").val();
// then put them in the search textboxes
$("input[type='search']").val(globalSearch);
// trigger keyup event on the datatables
$("input[type='search']").trigger("keyup.DT");
$("input[type='search']")
将获得所有搜索文本框。
就是这样的代码,它工作得很好!
$(function() {
var table = $('#DataTable1').DataTable({
proccessing: true,
searching: true,
paging: true,
serverSide: true,
initComplete: function() {
$('.dataTables_filter input').unbind();
$('.dataTables_filter input').bind('keyup', function(e){
var code = e.keyCode || e.which;
if (code == 13) {
table.search(this.value).draw();
}
});
},
ajax: {
url: '@Url.Action("Paginacao")',
type: 'POST'
},
language: {
url: '/plugins/datatables/lang/Portuguese-Brasil.json'
},
columns:
[
{ "data": "id", visible: false },
{ "data": "nome", "autoWidth": true },
{ "data": "cnpj", "autoWidth": true },
{
"render": function(data, type, full, meta) {
return '<a href=@Url.Action("Editar", "Usuario")?id='+full.id+'><b><i class=\"fa fa-edit bigfonts\"></i> Editar</b></a>';
}
}
]
});
});
本文地址:http://jquery.askforanswer.com/shujubiaozaienterjianerbushirenheanjianshangdequanjusousuo.html
文章标签:datatables , jquery , search
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
文章标签:datatables , jquery , search
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!