I'm using a jqGrid to display the results of a search. When the search button is clicked it does this:
$("#Search").jqGrid('setGridParam', { url: url }).trigger("reloadGrid");
Where url
contains the search params, for example:
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:
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.
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.
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:
$.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.
Instead of setting the url you should try something like this.
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.
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.
toolbar: [true, "top"],
First add this to your table definition.
beforeRequest: myBeforeRequest(this),
Then define your myBeforeRequest function something like this.
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.
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");
}