我正在使用DataTables.net来显示我的aspx网站的主要内容。我有几个按钮来确定应该加载到表中的数据。
按钮1设置iFilterBy ='3',而按钮2设置iFilterBy ='2'。
第一次,分别点击其中任何一个,工作正常。但是,如果我先按下按钮1,并且单击按钮2 - iFilterBy仍设置为按钮1的值,在这种情况下为“3”。
再次单击按钮2,工作正常。
表结构是:
var getMessageDate = function(button1Orbutton2) {
oMessageDate = $("#tble").dataTable({
"sDom": "<'row-fluid'<'span6'lT><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
"aLengthMenu": [
[5, 25, 50, 100, 250, -1],
[5, 25, 50, 100, 250, "All"]
],
"iDisplayLength": 5,
"bSortClasses": true,
"bPaginate": true,
"bAutoWidth": false,
"bProcessing": true,
"bServerSide": true,
"bDestroy": true,
"sAjaxSource": "SOMEPAGE,
"sScrollY ": "300px ",
"bScrollCollapse ": true,
"sPaginationType ": "bootstrap ",
"bDeferRender ": true,
"fnServerParams ": function (aoData) {
aoData.push({ "name ": "iFilterBy ", "value ": button1Orbutton2 });
},
" fnRowCallback ": function(nRow, aoData) {
},
"fnServerData ": function(sSource, aoData, fnCallback) {
$.ajax({
"dataType ": 'json',
"contentType ": " application / json; charset = utf - 8 ",
"type ": "GET ",
"url ": sSource,
"data ": aoData,
"error ":
function(xhr) {
var contentType = xhr.getResponseHeader("Content - Type ");
if (xhr.status === 401 && contentType.toLowerCase().indexOf("
text / html ") >= 0) {
window.location.reload();
}
},
"success ":
function(msg) {
fnCallback(jQuery.parseJSON(msg.d));
$("#tble ").show();
}
});
},
});
};
我省略了很多细节,但完整的代码库确实有效。
调用按钮是这样的:
$('#button1').click(function (event) {
event.preventDefault();
if (oMessageDate.fnGetData().length == 0) {
getMessageDate("2");
// Get the data specific just for the numbers.
} else {
// Filter what is already there.
oMessageDate.fnFilter("Some text detected");
}
});
所以,我的问题是为什么在设置iFilterBy
值后需要两次点击?
答案 0 :(得分:1)
目前尚不清楚从哪里获取button1Orbutton2
的值,但是如果它是隐藏的字段......
$('#button1').click(function (event) {
event.preventDefault();
$('#button1Orbutton2').val('3');
oMessageDate.fnDraw();
}
$('#button2').click(function (event) {
event.preventDefault();
$('#button1Orbutton2').val('1');
oMessageDate.fnDraw();
}
所以这里发生的是当你点击其中一个按钮时,会为隐藏字段分配一个过滤值。然后fnDraw()
使用iFilterBy
fnServerParams
过滤参数重绘表格