【发布时间】:2017-08-30 22:59:20
【问题描述】:
我有一个 jquery 网格,上面有一个搜索框。
Search: <input type="text" value="" id="searchInput" placeholder="Enter Document Type"/>
在 Search Keystroke 事件中,一切正常,直到用户输入一个不返回值的字符串。然后网格显示其通常的“未找到记录”。从这时起,搜索就中断了。
我研究了原因,发现 postData 对象/属性不再包含原始的 ajax 返回的网格行。因此,后续搜索会尝试过滤空网格。
此时如何强制重新加载我的网格(location.reload() 在哪里)?重新加载页面不会这样做,因为这是部分页面,并且会强制用户返回没有网格的前一页面。
<script type="text/javascript">
$(document)
.ready(function() {
'use strict';
showWebTour();
getAllAttachments();
$('#searchInput')
.on('change keyup paste',
function() {
var searchtext = $(this).val();
var grid = $('#documentList');
if ($("#documentList tr").length == 0 && searchtext.length == 0) {
location.reload();
}
var postdata = grid.jqGrid('getGridParam', 'postData');
// BUG: When Search returns no records, postData no longer exists so next search always fails
var myfilter = {
groupOp: "OR",
rules: []
};
myfilter.rules.push({
field: "DocumentType",
op: "cn",
data: searchtext
});
$.extend(postdata,
{
filters: myfilter
});
$("#gbox_documentList").show();
if ($('#empty-documentList', $('#gbox_documentList').parent()).length) {
$('#empty-documentList', $('#gbox_documentList').parent()).remove();
}
grid.jqGrid('setGridParam',
{
search: searchtext.length > 2,
postData: postdata
});
grid.trigger("reloadGrid",
[
{
page: 1
}
]);
});
});
function getAllAttachments() {
if (typeof ($('#refreshDocs') != 'undefined')) {
$('#refreshDocs').off('click');
}
$.ajax({
url:
'@Url.Action("AttactmentViewDocuments", "Document")?databaseId=@Model.LoanIdentifier.DatabaseId&loanRecordId=@Model.LoanIdentifier.LoanId',
dataType: "json",
type: 'GET',
beforeSend: function () {
$('.all-documents-loading')
.css({
"display": "block"
});
$('.attached-documents-loading')
.css({
"display": "block"
});
$('.generated-documents-loading')
.css({
"display": "block"
});
},
success: function (data) {
bindDocumentList(data.GridData);
$('.jqgfirstrow').hide();
},
error: function(xhr, response, status) {
/* Let user know that could not load data */
},
complete: function() {
$('.all-documents-loading')
.css({
"display": "none"
});
$('.attached-documents-loading')
.css({
"display": "none"
});
$('.generated-documents-loading')
.css({
"display": "none"
});
}
});
}
【问题讨论】:
-
你能解释一下搜索是如何工作的,它是从数据库中生成新数据还是在客户端过滤
标签: jquery ajax search jqgrid reload