重新加载网格不适用于多个 jqgrid
我正在使用jqgrid。我的页面有三个选项卡,每个选项卡包含不同的网格。所有网格都有不同的 id。选项卡的内容是通过 AJAX 请求延迟获取的。现在,在渲染所有三个网格之后,我尝试通过函数重新加载网格,
jQuery("#myOffersTable").trigger('reloadGrid');
仅加载最后一次重新加载的网格,并且它不适用于其他网格。
例如,如果网格加载序列为:1-2-3,则此代码仅适用于网格 3 但如果 seq 是 3-2-1 那么它只适用于 1。
但是如果我尝试使用导航栏上的重新加载按钮重新加载网格,它工作得很好。
更新:
我正在使用 Struts2 jQuery 插件。它使用 jqGrid 3.6.4 我使用ajax加载json数据。
以下是我的网格的定义。
<div id='t1'>
<s:url id="offersurl" action="offers"/>
<sjg:grid
id="offerstable"
caption="Customer Examples"
autoencode="false"
dataType="json"
href="%{offersurl}"
pager="true"
navigator="true"
navigatorAdd="false"
navigatorDelete="false"
navigatorEdit="false"
navigatorSearch="false"
gridModel="offers"
rowList="10,15,20"
rowNum="15"
rownumbers="true"
onCompleteTopics="addAcceptButtons"
filter="true"
>
<sjg:gridColumn name="id" index="id" title="ID" formatter="integer" sortable="false" search="false"/>
<sjg:gridColumn name="offeror" index="offeror" title="Offeror" sortable="true" search="false"/>
<sjg:gridColumn name="itemOffered" index="itemOffered" title="ItemOffered" sortable="false" search="true" searchoptions="{sopt:['eq']}"/>
<sjg:gridColumn name="quantityOffered" index="quantityOffered" title="QuantityOffered" sortable="false" search="true" searchoptions="{sopt:['eq','lt','gt']}"/>
<sjg:gridColumn name="expectedItem" index="expectedItem" title="ExpectedItem" sortable="false" search="true" searchoptions="{sopt:['eq']}"/>
<sjg:gridColumn name="expectedQuantity" index="expectedQuantity" title="ExpectedQuantity" sortable="false" search="true" searchoptions="{sopt:['eq','lt','gt']}"/>
<sjg:gridColumn name="acceptOffer" index="acceptOffer" title="Accept Offer" search="false"/>
</sjg:grid>
</div>
我有三个这样的网格,它们都有不同的 ID 之类的东西。
每个网格上方都有一个搜索按钮,它使用参数 sel 调用以下函数。sel 是对应于每个网格的 1,2 或 3
function search(sel)
{
alert("search");
if(sel==1)
{
tradeOffer = $("#games").val();
var srchValue = $("#srchoptions").val();
$.ajaxSetup({
data: {'gameId': tradeOffer},
});
jQuery("#offerstable").jqGrid('setGridParam',{url:"offers.action?q=1&srch="+srchValue,page:1});
//jQuery("#offerstable").trigger('reloadGrid');
$("#offerstable").trigger("reloadGrid");
}
else if(sel==2)
{
myTradeOfferGame = $("#my").val();
$.ajaxSetup({
data: {'gameId': myTradeOffer},
});
jQuery("#myOffersTable").jqGrid('setGridParam',{url:"offers.action?q=1",page:1});
jQuery("#myOffersTable").trigger('reloadGrid');
}
else if(sel==3)
{
acceptedTradeOfferGame = $("#accepted").val();
$.ajaxSetup({
data: {'gameId': acceptedTradeOffer},
});
jQuery("#acceptedtable").jqGrid('setGridParam',{url:"offers.action?q=1",page:1});
jQuery("#acceptedtable").trigger('reloadGrid');
}
}
该函数会为每个网格调用,但
jQuery("#acceptedtable").trigger('reloadGrid');
仅适用于最后加载的网格。
I am using jqgrid. My page has three tabs and each tab contains a different grid. All grids have different ids. The content of tabs is fetched via AJAX request lazily. Now after all three grids are rendered and I try to reload grid via function
jQuery("#myOffersTable").trigger('reloadGrid');
Only the grid which loaded last reloads and it doesn't work for other grids.
For example, if grids load seq is : 1-2-3 then this code will only work for grid 3
but if seq is 3-2-1 then it will work only for 1.
But if I try reloading grids using reload button on navigator bar it works fine.
Update:
I am using Struts2 jQuery Plugin.It uses jqGrid 3.6.4
I load json data using ajax.
Below is the definition of my grid.
<div id='t1'>
<s:url id="offersurl" action="offers"/>
<sjg:grid
id="offerstable"
caption="Customer Examples"
autoencode="false"
dataType="json"
href="%{offersurl}"
pager="true"
navigator="true"
navigatorAdd="false"
navigatorDelete="false"
navigatorEdit="false"
navigatorSearch="false"
gridModel="offers"
rowList="10,15,20"
rowNum="15"
rownumbers="true"
onCompleteTopics="addAcceptButtons"
filter="true"
>
<sjg:gridColumn name="id" index="id" title="ID" formatter="integer" sortable="false" search="false"/>
<sjg:gridColumn name="offeror" index="offeror" title="Offeror" sortable="true" search="false"/>
<sjg:gridColumn name="itemOffered" index="itemOffered" title="ItemOffered" sortable="false" search="true" searchoptions="{sopt:['eq']}"/>
<sjg:gridColumn name="quantityOffered" index="quantityOffered" title="QuantityOffered" sortable="false" search="true" searchoptions="{sopt:['eq','lt','gt']}"/>
<sjg:gridColumn name="expectedItem" index="expectedItem" title="ExpectedItem" sortable="false" search="true" searchoptions="{sopt:['eq']}"/>
<sjg:gridColumn name="expectedQuantity" index="expectedQuantity" title="ExpectedQuantity" sortable="false" search="true" searchoptions="{sopt:['eq','lt','gt']}"/>
<sjg:gridColumn name="acceptOffer" index="acceptOffer" title="Accept Offer" search="false"/>
</sjg:grid>
</div>
I have three such grids all have different ids and all that stuff.
There is a search button above each grid which calls the following function with parameter sel.sel is 1,2 or 3 corresponding to each grid
function search(sel)
{
alert("search");
if(sel==1)
{
tradeOffer = $("#games").val();
var srchValue = $("#srchoptions").val();
$.ajaxSetup({
data: {'gameId': tradeOffer},
});
jQuery("#offerstable").jqGrid('setGridParam',{url:"offers.action?q=1&srch="+srchValue,page:1});
//jQuery("#offerstable").trigger('reloadGrid');
$("#offerstable").trigger("reloadGrid");
}
else if(sel==2)
{
myTradeOfferGame = $("#my").val();
$.ajaxSetup({
data: {'gameId': myTradeOffer},
});
jQuery("#myOffersTable").jqGrid('setGridParam',{url:"offers.action?q=1",page:1});
jQuery("#myOffersTable").trigger('reloadGrid');
}
else if(sel==3)
{
acceptedTradeOfferGame = $("#accepted").val();
$.ajaxSetup({
data: {'gameId': acceptedTradeOffer},
});
jQuery("#acceptedtable").jqGrid('setGridParam',{url:"offers.action?q=1",page:1});
jQuery("#acceptedtable").trigger('reloadGrid');
}
}
The function gets called for each grid but
jQuery("#acceptedtable").trigger('reloadGrid');
works for only grid loaded last.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,在代码中定义变量
myTradeOfferGame
和acceptedTradeOfferGame
(在else if(sel==2)
和else if 内部(sel==3)
),但使用myTradeOffer
和acceptedTradeOffer
。看起来像是错误。第二:
else if(sel==2)
和else if(sel==3)
内部的 url 与第一个表中的情况不同:URL 是静态的,因此为什么每次都要设置这个值?也许您想在所有网址中添加$("#srchoptions").val()
部分?你应该自己解决这些问题。在您的代码中,我们可以看到,您使用
$.ajaxSetup
来更改$.ajax
的全局设置。如果您更改此设置 3 次,则只会保存最后一次。如果一次刷新时只能使用三种设置中的一种,那么$.ajaxSetup
仍然不是最好的方法。 jqGrid有参数ajaxGridOptions
,它设置每个表$.ajax
的参数(参见设置 jQuery jqGrid 执行的请求的内容类型)。此外,jqGrid(每个实例)都有一个参数
postData
,它将作为data
参数转发到$.ajax
。所以你可以在jqGrid定义中使用这个参数。如果您希望放置为postData
的数据在每个trigger('reloadGrid')
期间重新加载您可以使用函数定义postData
。$.ajax
的默认行为是测试data
参数的字段是否是 function,如果是的话,$.ajax
调用这个函数获取值。因此,您的代码可能如下所示:顺便说一句,如果您使用 HTTP GET 进行数据请求,则
data
参数 (postData
) 中的参数将仅附加< /strong> 到 url(像平常一样放置“?”和“&”)。最终代码可能类似于以下内容:
和
First of all in your code you define variables
myTradeOfferGame
andacceptedTradeOfferGame
(inside ofelse if(sel==2)
andelse if(sel==3)
), but usemyTradeOffer
andacceptedTradeOffer
. It looks like errors.Second: The urls inside of
else if(sel==2)
andelse if(sel==3)
look another as in the first table: URLs are static, so why one should set this value every time? Probably you want to add in all urls the part with$("#srchoptions").val()
? You should fix these problem yourself.In your code one can see, that you use
$.ajaxSetup
which change global settings of$.ajax
. If you change this 3 times only the last one will be saved. If only one from three setting work at one refresh,$.ajaxSetup
is nevertheless not the best way. jqGrid has parameterajaxGridOptions
, which set parameters of$.ajax
per table (see Setting the content-type of requests performed by jQuery jqGrid).Moreover jqGrid (every instance) has a parameter
postData
, which will be forward to$.ajax
asdata
parameter. So you can use this parameter in the jqGrid definition. If you want that the data which you place aspostData
will be reloaded during everytrigger('reloadGrid')
you can just definepostData
using function. The default behavior of$.ajax
is to test whether the field ofdata
parameter is function, it it is so,$.ajax
call this function the get the value. So your code could look like following:By the way if you use HTTP GET for data request, the parameters from
data
parameter (postData
) will be just appended to the url (with placing '?' and '&' like one do this usual).The final code can be something like following:
and