jquery ui 选项卡 +网格对象 - ajax 发送多个查询
我不太擅长 jquery,并且遇到了这个问题:
我使用 jquery 选项卡为:
<li><a href="content.php?div=Production">Production</a></li>
<li><a href="content.php?div=Digital">Digital</a></li>
声明为:
$(function() {
$( "#tabs" ).tabs({
load: function(event, ui) {
console.log("load event ran");
$('a', ui.panel).live("click", function() {
$(ui.panel).load(this.href);
return false;
});
}
});
$("#tabs").bind('tabsshow',function(event, ui) {
window.location = ui.tab;
})
});
在 content
中我有网格定义 - 每个选项卡都有它自己的< /strong> 要加载的表:
switch($div){
case "Production":
$tab = '1';
include('div_master.php');
break;
case "Digital":
$tab = '2';
include('div_digital.php');
break;
default:
include('div_master.php');
break;
}
在上面的 php 之后,我得到了 grid 对象加载数据库结果:
$(function() {
var grid = $(".grid").loadGrid({ ....});
});
我也将网格对象存储在变量中,所以我可以访问sql查询
,这样我就可以通过按页面上的按钮将其导出到Excel:
$('#toexcel').live("click",function() {
var sql = grid.data().sql;
$.ajax({
url: "toExcel.php",
data: "sql="+encodeURIComponent(sql),
});
sql = '';
});
});
我的问题是,当我从新鲜加载第一个选项卡时,将一些结果导出到Excel,然后更改为第二个选项卡 -该按钮从第一个选项卡导出相同的查询! 显然,尽管重新加载了网格,但 var sql = grid.data().sql;
并未使用新数据进行更新。
无论我在选项卡之间更改多少次,第一个选项卡始终有效, 但第二个只有在页面/DOM 完全重新加载时才有效。
我该如何纠正这个问题?
编辑 2 - 我为两个表设置了单独的网格 - 仍然存在相同的问题。
我真的不认为问题在于重用网格对象 - 我认为问题在于 jquery UI - 选项卡,特别是如何处理重新加载 DOM。
添加
请参阅 Firebug 控制台以获取请求: 看看刚加载时 - ajax 仅向 toExcel 发送 1 个请求,但在切换选项卡(div-m - 第一个选项卡,div-d - 第二个选项卡)后,它发送 2 个请求。根据选项卡,它会取消第一个选项卡的第二个查询,第二个选项卡的第一个查询 - 这就是为什么第一个选项卡始终有效!
i'm not so good at jquery and i've got this issue:
i'm using jquery tabs as:
<li><a href="content.php?div=Production">Production</a></li>
<li><a href="content.php?div=Digital">Digital</a></li>
declared as:
$(function() {
$( "#tabs" ).tabs({
load: function(event, ui) {
console.log("load event ran");
$('a', ui.panel).live("click", function() {
$(ui.panel).load(this.href);
return false;
});
}
});
$("#tabs").bind('tabsshow',function(event, ui) {
window.location = ui.tab;
})
});
in content
i have grid definitions - each tab has it's own table to load:
switch($div){
case "Production":
$tab = '1';
include('div_master.php');
break;
case "Digital":
$tab = '2';
include('div_digital.php');
break;
default:
include('div_master.php');
break;
}
after above php i've got grid object loading db results:
$(function() {
var grid = $(".grid").loadGrid({ ....});
});
i also store grid object in a variable so i can access sql query
so i can export it to excel by pressing a button on a page:
$('#toexcel').live("click",function() {
var sql = grid.data().sql;
$.ajax({
url: "toExcel.php",
data: "sql="+encodeURIComponent(sql),
});
sql = '';
});
});
my problem is that when i load fist tab from fresh, export some results to excel, then change to second tab - the button exports the same query from the first tab!
so obviously var sql = grid.data().sql;
is not updated with the new data despite grid being reloaded.
first tab always works no matter how many times i change between tabs,
but second works only when page/DOM is completely reloaded.
how do i correct this?
Edit 2 - i have separated grids for both tables - still having the same issue.
i really don't think that the problem is with reusing the grid object - i think that the problem is with jquery UI - tabs, specifically how the handle reloading DOM.
ADDITION
please see firebug console for requests:
see when freshly loaded - ajax only send 1 request to toExcel, but after switching a tab (div-m - 1st tab, div-d - 2nd tab) it sends 2 requests. and depending on the tab it cancels either 2nd query for fist tab, 1st query for second tab - that's why 1st tab always working!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新: 代码被精简到骨子里! ;)
你的代码应该是这样的:
解释:
当使用 jquery-ui 选项卡时,充当
tab
的标签不会被触发( jquery 阻止它的点击事件,因为它实际上用作选项卡而不是链接),而且您的 html-tabs 代码将无法按预期工作(不会切换选项卡)导致
标记
hash
(href="#something"
) 应该匹配它的id
(id="something"
)相应的容器(在本例中为 div ),而是将每个锚点关联到一个直接的 url 查询(content.php?..
),该查询永远不会被触发,在这种情况下也永远不会被触发也可以切换任何选项卡。因此,简单的解决方案是让
标记充当选项卡,方法是放回
#
+id
(如我的示例中所示)并挂钩tabsui
事件并从那里继续。UPDATED: code stripped to the bone! ;)
your code should be like this:
Explanation:
when using jquery-ui tabs, the
<a>
tag that act astab
is not fired ( jquery prevent it's click event, cause it is actually used as tab and not as link ), also your html-tabs code will not work as expected ( will not switch tabs ) cause the<a>
taghash
(href="#something"
) should match theid
(id="something"
) of it's respective container ( in this case div ), instead you have associated to each anchor a direct url-query (content.php?..
) that, again, will never be fired and in this case will never switch any tab too.so the simple solution is to let the
<a>
tag act as tab by putting back the#
+id
as in my example and hook the tabsui
event and procede from there.我可能会在这里遗漏一些细节,但也许值得将每个选项卡加载到自己的 .grid 中?这样就不可能从另一个选项卡错误地加载数据
将每个选项卡设置为具有完全不同的类的自己的网格:
然后以相同、完全独立的方式使用 .loadGrid():
然后在每个选项卡中包含一个 excel 导出按钮:
...
我确信代码可以更好,但从简单开始并使其工作,然后在工作后添加复杂性和功能。
更新:显示代码后添加了更多具体细节
I might be missing some details here, but maybe its worth loading each tab into its own .grid? that way there is no way the data can be mistakenly loaded from another tab
Set each tab into its own grid with completely differing classes:
Then .loadGrid() in the same, completely separate manner:
Then include an excel export button within each tab:
...
Im sure the code could be nicer but start simple and make it work, then add complexity and functionality once its working.
UPDATED: added more specific details after the code was shown
我可能会遗漏一些明显的东西,但我不知道你在哪里更新 digitalGrid.data().sql;除了默认值之外的任何内容。也许您想使用ajax的Success或Complete回调来重新加载数据。也就是说,可能会遗漏一些代码或处理此问题的网格工具固有的某些内容。
伪代码:
I could be missing something obvious but I don't see where you are updating digitalGrid.data().sql; to anything but the default. Perhaps you want to use Success or Complete callback of ajax to reload the data. That said, there might be some code left out or something inherent to the grid tool that handles this.
Psuedo code:
从我在 OpenJS Grid 中看到的情况来看,该插件返回一个元素数组,因为您将其绑定到“.grid”。
在您的代码中,您有两次这样的情况,因此您将收到两个元素。
您可以尝试一下这是否有效:
From what I can see in OpenJS Grid, the plugin is returning an array of elements because you're binding it to ".grid".
In your code you have this two times, so you'll receive two elements.
Can you try if this will work: