在 jqGrid 加载时触发客户端过滤
我试图让 JqGrid 在完成数据加载后进行一些客户端过滤(和排序)。我可以正确设置搜索字段,但调用 TriggerToolbar() 似乎没有任何效果。
$("#list").GridUnload();
var mygrid = $("#list").jqGrid({
url: '@Url.Action("GetSearchCriteriaWithNoComponents", "SearchCriteria")',
postData: { BookingSiteId: function () { return $("#BookingSiteId option:selected").val(); }, MethodId: function () { return $("#MethodId option:selected").val(); } },
datatype: 'json',
mtype: 'Post',
colNames: ['Id', 'PID', 'Ori', 'Dest', 'Conn', 'Pos', 'Method', 'Billing', 'Filter', 'Pattern', 'Class', 'Wildcard', 'Components', 'Comment'],
colModel: [
{ name: 'Id', index: 'Id', width: 30, hidden: true },
{ name: 'PID', index: 'PID', width: 35 },
{ name: 'Ori', index: 'Ori', width: 35 },
{ name: 'Dest', index: 'Dest', width: 35 },
{ name: 'Conn', index: 'Conn', width: 35 },
{ name: 'Pos', index: 'Pos', width: 35 },
{ name: 'Method', index: 'Method', width: 50 },
{ name: 'Billing', index: 'Billing', width: 45, search: true, stype: 'text', searchoptions: { sopt: ['cn']} },
{ name: 'Filter', index: 'Filter', width: 90, search: true, stype: 'text', searchoptions: { sopt: ['cn']} },
{ name: 'Pattern', index: 'Pattern', width: 100, search: true, stype: 'text', searchoptions: { sopt: ['cn']} },
{ name: 'Class', index: 'Class', width: 40 },
{ name: 'Wildcard', index: 'Wildcard', width: 50 },
{ name: 'Components', index: 'Components', width: 80, search: true, stype: 'text', searchoptions: { sopt: ['cn']} },
{ name: 'Comment', index: 'Comment', width: 75, search: true, stype: 'text', searchoptions: { sopt: ['cn']} }
],
pager: '#pager',
rowNum: 25,
rowTotal: 1000,
rowList: [25, 50, 100],
sortname: 'Origin',
sortorder: "asc",
viewrecords: true,
loadonce: true,
multiselect: true,
ignoreCase: true,
gridview: true,
height: "100%",
caption: 'Subscribed Search Criteria without Components'
}).jqGrid('navGrid', '#pager', { add: false, edit: false, del: false, search: false, refresh: false }
).jqGrid('navButtonAdd', '#pager', { caption: "", buttonicon: "ui-icon-search", onClickButton: function () { mygrid[0].toggleToolbar() }, position: "first", title: "Toggle Search Toolbar", cursor: "pointer" })
.jqGrid('navButtonAdd', '#pager', { caption: "", buttonicon: "ui-icon-refresh", onClickButton: function () { mygrid[0].clearToolbar() }, title: "Clear Search Toolbar", cursor: "pointer" }
);
$("#list").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false });
if(firstrun = true)
{
$("#gs_PID").val('AA');
mygrid[0].triggerToolbar();
firstrun = false;
}
else
{
mygrid[0].toggleToolbar(); //hide the toolbar by default
}
总体思路是固定值将由视图模型中的某些内容填充。
我们还希望在执行 gridUnload() 和后续重新加载时能够选择保存和恢复过滤器以及排序选择,但一次只有一个障碍。 带有@前缀的东西(例如@Url.Action)是Razor标记,它在页面发送到浏览器之前进行处理。
I'm trying to get a JqGrid to do some client-side filtering (and sorting) just after it has finished loading in the data. I can set the search-field correctly, but calling TriggerToolbar() doesn't seem to have any effect.
$("#list").GridUnload();
var mygrid = $("#list").jqGrid({
url: '@Url.Action("GetSearchCriteriaWithNoComponents", "SearchCriteria")',
postData: { BookingSiteId: function () { return $("#BookingSiteId option:selected").val(); }, MethodId: function () { return $("#MethodId option:selected").val(); } },
datatype: 'json',
mtype: 'Post',
colNames: ['Id', 'PID', 'Ori', 'Dest', 'Conn', 'Pos', 'Method', 'Billing', 'Filter', 'Pattern', 'Class', 'Wildcard', 'Components', 'Comment'],
colModel: [
{ name: 'Id', index: 'Id', width: 30, hidden: true },
{ name: 'PID', index: 'PID', width: 35 },
{ name: 'Ori', index: 'Ori', width: 35 },
{ name: 'Dest', index: 'Dest', width: 35 },
{ name: 'Conn', index: 'Conn', width: 35 },
{ name: 'Pos', index: 'Pos', width: 35 },
{ name: 'Method', index: 'Method', width: 50 },
{ name: 'Billing', index: 'Billing', width: 45, search: true, stype: 'text', searchoptions: { sopt: ['cn']} },
{ name: 'Filter', index: 'Filter', width: 90, search: true, stype: 'text', searchoptions: { sopt: ['cn']} },
{ name: 'Pattern', index: 'Pattern', width: 100, search: true, stype: 'text', searchoptions: { sopt: ['cn']} },
{ name: 'Class', index: 'Class', width: 40 },
{ name: 'Wildcard', index: 'Wildcard', width: 50 },
{ name: 'Components', index: 'Components', width: 80, search: true, stype: 'text', searchoptions: { sopt: ['cn']} },
{ name: 'Comment', index: 'Comment', width: 75, search: true, stype: 'text', searchoptions: { sopt: ['cn']} }
],
pager: '#pager',
rowNum: 25,
rowTotal: 1000,
rowList: [25, 50, 100],
sortname: 'Origin',
sortorder: "asc",
viewrecords: true,
loadonce: true,
multiselect: true,
ignoreCase: true,
gridview: true,
height: "100%",
caption: 'Subscribed Search Criteria without Components'
}).jqGrid('navGrid', '#pager', { add: false, edit: false, del: false, search: false, refresh: false }
).jqGrid('navButtonAdd', '#pager', { caption: "", buttonicon: "ui-icon-search", onClickButton: function () { mygrid[0].toggleToolbar() }, position: "first", title: "Toggle Search Toolbar", cursor: "pointer" })
.jqGrid('navButtonAdd', '#pager', { caption: "", buttonicon: "ui-icon-refresh", onClickButton: function () { mygrid[0].clearToolbar() }, title: "Clear Search Toolbar", cursor: "pointer" }
);
$("#list").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false });
if(firstrun = true)
{
$("#gs_PID").val('AA');
mygrid[0].triggerToolbar();
firstrun = false;
}
else
{
mygrid[0].toggleToolbar(); //hide the toolbar by default
}
The general idea is that the fixed value will be populated with something from the viewmodel.
We would also like to have the option of saving and restoring filter and sorting selection for when we do gridUnload() and subsequent reloads, but one hurdle at a time.
stuff with @prefixes such as @Url.Action is Razor markup, which is handled before the page is sent to the browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
datatype: 'json'
与loadonce: true
结合使用。因此,您应该在loadComplete
事件句柄内调用mygrid[0].triggerToolbar()
。仅在加载数据后才应过滤数据。也许您应该将带有triggerToolbar
的代码放在setTimeout
方法中,以便在将datatype
更改为'local 后开始过滤'
在loadonce: true
期间。因此,您的loadComplete
事件句柄的代码可能如下:此外,我不完全理解您如何使用您发布的代码片段以及为什么需要使用
gridUnload
方法。更新: jqGrid 的免费 jqGrid 分支支持
forceClientSorting: true
选项,在显示第一页数据之前,强制对从服务器返回的数据进行排序和可选过滤。它使得上面的代码不再需要。相反,可以设置postData.filters
使用,请参阅 演示。
You use
datatype: 'json'
in combination withloadonce: true
. So you should callmygrid[0].triggerToolbar()
inside of theloadComplete
event handle. Only after the data are loaded you should filter the data. Probably you should place the code withtriggerToolbar
inside ofsetTimeout
method to start the filtering already after thedatatype
will be changed to'local'
duringloadonce: true
. So the code of yourloadComplete
event handle could be about the following:Additionally I don't full understand how you use the code fragment which you posted and why you need to use
gridUnload
method.UPDATED: Free jqGrid fork of jqGrid supports
forceClientSorting: true
option, which force sorting and optional filtering of the data, returned from the server, before displaying the first page of data. It makes unneeded the above code. Instead of that one can setpostData.filters
usesee the demo.