试图理解 jquery 的 jqgrid。如何更改 AJAX 调用的 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1209620/
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
Trying to understand jqgrid for jquery. How do I change the URL for my AJAX call?
提问by johnny
I have everything working from the tutorial, replacing it with my data, but it is a hardcoded situation. For example, in the tutorial, which is what I have working, I have:
我有教程中的所有内容,用我的数据替换它,但这是一个硬编码的情况。例如,在教程中,这是我的工作,我有:
jQuery(document).ready(function(){
jQuery("#list").jqGrid({
url:'http://127.0.0.1/products/index.php/mystuff/test/1/5/productname/desc',
datatype: 'xml',
mtype: 'GET',
colNames:['ProductID','title', 'description','price','tax','notes'],
colModel :[
{name:'invid', index:'invid', width:55},
{name:'invdate', index:'invdate', width:90},
{name:'amount', index:'amount', width:80, align:'right'},
{name:'tax', index:'tax', width:80, align:'right'},
{name:'total', index:'total', width:80, align:'right'},
{name:'note', index:'note', width:150, sortable:false} ],
pager: jQuery('#pager'),
rowNum:10,
rowList:[10,20,30],
sortname: 'title',
sortorder: "desc",
viewrecords: true,
imgpath: 'http://127.0.0.1/products/public/themes/basic/images',
caption: 'Product List'
});
});
But the url is hard coded to pull up data. The tutorial has:
但是 url 是硬编码来提取数据的。教程有:
jQuery(document).ready(function(){
jQuery("#list").jqGrid({
url:'example.php',
datatype: 'xml',
mtype: 'GET',
colNames:['Inv No','Date', 'Amount','Tax','Total','Notes'],
colModel :[
{name:'invid', index:'invid', width:55},
{name:'invdate', index:'invdate', width:90},
{name:'amount', index:'amount', width:80, align:'right'},
{name:'tax', index:'tax', width:80, align:'right'},
{name:'total', index:'total', width:80, align:'right'},
{name:'note', index:'note', width:150, sortable:false} ],
pager: jQuery('#pager'),
rowNum:10,
rowList:[10,20,30],
sortname: 'id',
sortorder: "desc",
viewrecords: true,
imgpath: 'themes/basic/images',
caption: 'My first grid'
});
});
Obviously, the data in my querystring will change and I won't always want the same url of: http://127.0.0.1/products/index.php/mystuff/test/1/5/productname/desc
显然,我的查询字符串中的数据会改变,我不会总是想要相同的网址:http: //127.0.0.1/products/index.php/mystuff/test/1/5/productname/desc
And, the tutorial with example.php needs querystring variables so it can get the right data, but they never show that part that I can find. So how do I change the querystring? How do I send items dynmically? Also, the pager buttons send stuff like this because of the hardcode url:
而且,example.php 的教程需要查询字符串变量,以便它可以获得正确的数据,但它们从未显示我可以找到的那部分。那么如何更改查询字符串呢?如何动态发送项目?此外,由于硬编码 url,寻呼机按钮会发送这样的内容:
How can I just have a url like /test/the right variables? How can I change the querystring?
我怎么能有一个像 /test/正确的变量这样的网址?如何更改查询字符串?
Thank you for any help. I hope it is not too obvious. I found setGridParam({url:newvalue})but did not understand how to use it.
感谢您的任何帮助。我希望这不是太明显。我找到了setGridParam({url:newvalue})但不明白如何使用它。
Here is the documentation. The tutorial is at the first part, top.
这是文档。本教程在第一部分,顶部。
EDIT: Thanks for the below. I am trying to see how they use the setGridParam in their tutorial with example.php. They simply have example.php and I cannot see how they got the querystring variables there from the html page to the example.php page. setGridParam may be the right way to accomplish this but it is no where in the example so I cannot figure out how they passed the initial querystring variables over.
编辑:感谢以下内容。我想看看他们如何在 example.php 的教程中使用 setGridParam。他们只有example.php,我看不到他们是如何从html页面到example.php页面获取查询字符串变量的。setGridParam 可能是完成此操作的正确方法,但它在示例中没有出现,所以我无法弄清楚它们是如何传递初始查询字符串变量的。
I know that I can do this:
我知道我可以这样做:
function gridSearch()
{
$("#list").setGridParam(
{
url:"http://127.0.0.1/products/index.php/mystuff/test/1/5/productname/desc",
page:1
}
).trigger("reloadGrid");//Reload grid trigger
return false
}
<input id="sData" class="EditButton" type="submit" value="Search" onClick="return gridSearch()"/>
and that that will reload the grid (I tested it; it works), but how did they send over items in the tutorial in the first place? What if I do not want a button to reload but just pass in variables for it to initially load?
这将重新加载网格(我测试过它;它有效),但是他们首先是如何发送教程中的项目的?如果我不希望按钮重新加载,而只是传入变量以使其初始加载,该怎么办?
回答by redsquare
After you have created your grid you can build up a url and then specify this and trigger the grid to reload.
创建网格后,您可以建立一个 url,然后指定它并触发网格重新加载。
jQuery("#list").jqGrid().setGridParam({url : 'newUrl'}).trigger("reloadGrid")
回答by Jeff Gao
You can try this:
你可以试试这个:
jQuery("#list")
.jqGrid()
.setGridParam({
data:'xml',
url : 'newUrl'
})
.trigger("reloadGrid");
回答by Ankit Adlakha
Suppose you have written a jqgrid js code in a js function. So in that method pass your url dynamically in a variable urlGetLstIn jqgrid code we have to specify some attributes:
假设你已经在一个js函数中编写了一个jqgrid js代码。因此,在该方法中,在变量urlGetLst 中动态传递您的 url 在 jqgrid 代码中,我们必须指定一些属性:
url: urlGetLst,//(will be dynamic)
//and
loadonce: false
With this option it will every time give a call to specified urland refresh the jqgrid with fresh data.
使用此选项,它将每次调用指定的 url并使用新数据刷新 jqgrid。
And before calling jqgrid load function with urlcall/execute below code
在使用urlcall/execute 下面的代码调用 jqgrid 加载函数之前
$("#jqgridtab").GridUnload();