jQuery jqGrid 触发 reloadGrid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2901587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery jqGrid trigger reloadGrid
提问by JK.
I'm using a jqGridto display the results of a search. When the search button is clicked it does this:
我正在使用jqGrid来显示搜索结果。单击搜索按钮时,它会执行以下操作:
$("#Search").jqGrid('setGridParam', { url: url }).trigger("reloadGrid");
Where url
contains the search params, for example:
其中url
包含搜索参数,例如:
var url ="/search?first=joe&last=smith"
The web server is receiving this URL and responding appropriately. But on the client side it throws this error in jqgrid.min.js line 21:
Web 服务器正在接收此 URL 并正确响应。但是在客户端,它会在 jqgrid.min.js 第 21 行中抛出此错误:
Syntax error:
}); b.fn.jqGrid = function(f) {
What can I do to fix this? I'm using jqGrid sucessfully in many other places, but this is the only one where I'm changing the URL and reloading.
我能做些什么来解决这个问题?我在许多其他地方成功地使用了 jqGrid,但这是我更改 URL 并重新加载的唯一地方。
回答by Dan Heberden
Try using the non minified version on this page to see more context of why it's surrounding. What you're seeing there is a where parsing is halting; I suspect your error is further up. This way you can see if the current url is being used and what's throwing it off.
尝试使用此页面上的非缩小版本以查看更多关于其周围原因的上下文。你看到的是解析停止的地方;我怀疑你的错误更进一步。通过这种方式,您可以查看当前 url 是否正在被使用以及是什么导致它被丢弃。
回答by Oleg
It seems to me that the error which you have in jqgrid.min.js corresponds an error in uncompressed version of jqGrid direct at the beginning of .jqGrid('setGridParam', { url: url })
(see line 82 of grid.base.js). It is the part of the so named "New API" introduced in 3.6 version of jqGrid. The code fragment started with following lines:
在我看来,您在 jqgrid.min.js 中的错误对应于 jqGrid 的未压缩版本中直接在开头的错误.jqGrid('setGridParam', { url: url })
(请参阅grid.base.js 的第 82行)。它是 jqGrid 3.6 版本中引入的所谓“新 API”的一部分。代码片段以以下几行开头:
$.fn.jqGrid = function( pin ) {
if (typeof pin == 'string') {
var fn = $.fn.jqGrid[pin];
if (!fn) {
throw ("jqGrid - No such method: " + pin);
}
var args = $.makeArray(arguments).slice(1);
return fn.apply(this,args);
}
//...
I am not sure why you have a "syntax error", bu I recommend you to verify, that the id of the grid is really "Search". If you will don't find an error add more information in your question. For example: which version of jQuery you use? Including of a code fragment and the order of JavaScripts which you load would be also helpful.
我不确定您为什么会出现“语法错误”,但我建议您验证一下,网格的 id 确实是“搜索”。如果您找不到错误,请在您的问题中添加更多信息。例如:您使用哪个版本的 jQuery?包含代码片段和加载的 JavaScript 的顺序也会有所帮助。
回答by David
Instead of setting the url you should try something like this.
您应该尝试这样的事情,而不是设置 url。
I am using this for custom drop downs that I add to the grid. Basicaly I conditionaly add 2 drop downs to the top toolbar section of the grid for quick searching.
我将此用于添加到网格的自定义下拉列表。基本上我有条件地向网格的顶部工具栏部分添加 2 个下拉菜单,以便快速搜索。
var toolbarspan = $("<span>");
if (tblDef.State != null) {
$("<label>").attr("for", "selectState").append(" State: ").appendTo("#t_colch")
$("<select>").attr("id", "selectState")
.append($("<option>").attr({selected: true, value: "O"}).append("Open"))
.append($("<option>").attr("value", "C").append("Closed"))
.append($("<option>").attr("value", "B").append("Both"))
.change(selChange)
.appendTo("#t_colch")
}
$("<label>").attr("for", "selectInActive").append(" InActive: ").appendTo("#t_colch")
$("<select>").attr("id", "selectInActive")
.append($("<option>").attr({selected: true, value: "0"}).append("Active"))
.append($("<option>").attr("value", "1").append("InActive"))
.append($("<option>").attr("value", "B").append("Both"))
.change(selChange)
.appendTo("#t_colch");
}
If you wanted your 2 fields in the top toolbar as well you will need to add the following to your table options.
如果您还希望顶部工具栏中的 2 个字段,则需要将以下内容添加到表格选项中。
toolbar: [true, "top"],
First add this to your table definition.
首先将其添加到您的表定义中。
beforeRequest: myBeforeRequest(this),
Then define your myBeforeRequest function something like this.
然后像这样定义你的 myBeforeRequest 函数。
function myBeforeRequest(grid) {
$(grid).jqGrid("setPostDataItem", "InActive", 0);
var chkVal="";
if ($("#selectInActive").length > 0)
{
chkVal = $("#selectInActive").val();
if (chkVal == "B") $(grid).jqGrid("removePostDataItem", "InActive");
else $(grid).jqGrid("setPostDataItem", "InActive", chkVal);
}
if (tblDef.State != null) {
$(grid).jqGrid("setPostDataItem", "StateCol", tblDef.State);
$(grid).jqGrid("setPostDataItem", "State", "O");
if($("#selectState").length > 0)
{
chkVal = $("#selectState").val();
if (chkVal == "B") $(grid).jqGrid("removePostDataItem", "State");
else $(grid).jqGrid("setPostDataItem", "State", chkVal);
}
}
}
You could do the same for your 2 searching fields even clearing them from the parameters if they are blank. This would result in the same url that you are manualy editing now. GetRecords.aspx?InActive=0&State=O&StateCol=9 is what I am currently getting at the server.
您可以对 2 个搜索字段执行相同操作,即使它们为空白,也可以从参数中清除它们。这将导致您现在手动编辑的网址相同。GetRecords.aspx?InActive=0&State=O&StateCol=9 是我目前在服务器上得到的。
回答by Ro.
Refresh method works Ok for me. It is refreshing Dates (to and from) in each call.
刷新方法对我来说还可以。它在每次通话中刷新日期(到和从)。
The last two lines of code do all the magic.
最后两行代码发挥了所有作用。
I′m using it like this:
我是这样使用它的:
function refreshGrid() {
var gridSel = "#analyticsTbl";
var fromDt = jQuery('#dpFrom').datepicker().val();
var toDt = jQuery('#dpTo').val();
var url = 'myService.asmx/MyWebMethod?fromdt=' + fromDt + '&' + 'todt=' + toDt;
jQuery(gridSel).jqGrid({
url: url,
mtype: "GET",
datatype: "xml",
colNames: ['Product', 'Group', 'Score', 'Date'],
colModel: [
{ name: 'Product', index: 'Product', sortable: true },
{ name: 'Group', index: 'Group', sortable: true },
{ name: 'Score', index: 'Score', sortable: true },
{ name: 'Date', index: 'Date', sortable: true }
],
viewrecords: true,
sortorder: "desc",
caption: "Report",
hidegrid: false,
autowidth: true,
height: "100%"
});
jQuery(gridSel).jqGrid('navGrid', '#pgwidth', { edit: false, add: false, del: false });
jQuery(gridSel).jqGrid('setGridParam', { url: url });
jQuery(gridSel).trigger("reloadGrid");
}