如何使用 jQuery 通过选项卡式导航刷新 JSON 数据?
尝试让相当简单的 UI 正常工作时遇到一些问题。我想也许这里有人可以提供一些建议并为我指明正确的方向。这就是我正在发生的事情。
1) 使用 getJSON 从 PHP 页面中提取 JSON 字符串(多维数组)。 2)使用2个jQuery.each语句迭代JSON数据并将数据显示到页面。第一个 jQuery.each 创建我的侧面导航(列出计算机实验室名称)。第二个 jQuery.each 创建一个包含机器、其状态和日期的数据表。
因此,我遇到的问题是我希望侧面导航充当选项卡和数据表中的选项卡。我可以让它工作得很好。这就是我遇到问题的地方......我希望我的 JSON 数据每 2 分钟刷新一次,所以我使用 setInterval 再次获取 JSON 字符串。这最终会破坏我的选项卡/导航。我理解它为什么会崩溃,但对于如何修复它或编码的替代方法我有点迷失。
JSON 字符串看起来像这样......
{
"Labs": [
{
"name": "Computer Lab 1",
"host": [
{
"name": "c1ms",
"status": "up",
"date": "8/25/11 02:05 PM"
}
],
"downcount": 0
},
{
"name": "Computer Lab 2",
"host": [
{
"name": "berk1",
"status": "up",
"date": "8/26/11 08:55 AM"
},
{
"name": "berk2",
"status": "up",
"date": "8/26/11 08:50 AM"
},
{
"name": "berk3",
"status": "up",
"date": "8/26/11 08:50 AM"
},
{
"name": "berk4",
"status": "up",
"date": "8/26/11 08:55 AM"
}
],
"downcount": 0
},
{
"name": "Computer Lab 3",
"host": [
{
"name": "pc1",
"status": "up",
"date": "8/26/11 08:50 AM"
},
{
"name": "pc2",
"status": "up",
"date": "8/26/11 08:55 AM"
},
{
"name": "pc3",
"status": "up",
"date": "8/30/11 12:20 AM"
},
{
"name": "pc4",
"status": "up",
"date": "8/26/11 08:50 AM"
},
{
"name": "pc5",
"status": "up",
"date": "8/26/11 08:55 AM"
}
],
"downcount": 1
},
{
"name": "Computer Lab 4",
"host": [
{
"name": "mac1",
"status": "up",
"date": "8/22/11 03:05 PM"
},
{
"name": "mac2",
"status": "up",
"date": "8/22/11 03:10 PM"
},
{
"name": "mac3",
"status": "up",
"date": "8/22/11 03:05 PM"
},
{
"name": "mac4",
"status": "up",
"date": "8/23/11 12:20 PM"
},
{
"name": "mac5",
"status": "up",
"date": "8/16/11 01:30 PM"
},
{
"name": "mac6",
"status": "up",
"date": "8/22/11 03:05 PM"
}
],
"downcount": 0
}
]
}
---到目前为止的代码 ---
<script type="text/javascript">
$(document).ready(function() {
setInterval('$("#table").trigger("getJson")', 3000);
$("#table").bind("getJson", function(event){
$.getJSON('json.php', function(data, textStatus, jqXHR){
$("#table").trigger("drawTable", data);
});
})
$("#table").bind("drawTable", function(event, json){
//build the table, json is your jsonData from the call
$.each(json.Service, function(i,object){
$.each(object.host, function(e,o){
$('#table').append("<tr><td width='30%'>"+o.name+"</td><td width='10%'><span class='status-"+o.status+"'>"+o.status+"</span></td><td width='60%'>"+o.date+"</td></tr>");
});
});
});
$(".tableRow").live("click", function(event){
//sidenavigation code
$.each(json.Service, function(n, nav) {
$('#db-sidenav ul').append("<li>"+nav.name+"</li>");
});
});
});
</script>
Having some problems trying to get a rather simple UI working properly. Thought maybe someone here could lend some advice and point me in the right direction. Here's what I've got going on.
1) Pulling in a JSON string (multidimensional array) from a PHP page using getJSON.
2) Using 2 jQuery.each statements to iterate through JSON data and display the data to the page. The first jQuery.each creates my side navigation (lists computer lab names). The second jQuery.each creates a data table with the machines, their status, and a date.
So where I'm running into a problem is I want to my side navigation to act as tabs and tab through the data table. I can get that working fine. Here is where I run into a problem.....I want my JSON data to refresh every 2 mins so I'm using setInterval to go out and get the JSON string again. This ends up breaking down my tabs/navigation. I understand why it breaks down but I'm kind of lost as far as how to fix it or an alternative way to code this.
The JSON string looks like this.....
{
"Labs": [
{
"name": "Computer Lab 1",
"host": [
{
"name": "c1ms",
"status": "up",
"date": "8/25/11 02:05 PM"
}
],
"downcount": 0
},
{
"name": "Computer Lab 2",
"host": [
{
"name": "berk1",
"status": "up",
"date": "8/26/11 08:55 AM"
},
{
"name": "berk2",
"status": "up",
"date": "8/26/11 08:50 AM"
},
{
"name": "berk3",
"status": "up",
"date": "8/26/11 08:50 AM"
},
{
"name": "berk4",
"status": "up",
"date": "8/26/11 08:55 AM"
}
],
"downcount": 0
},
{
"name": "Computer Lab 3",
"host": [
{
"name": "pc1",
"status": "up",
"date": "8/26/11 08:50 AM"
},
{
"name": "pc2",
"status": "up",
"date": "8/26/11 08:55 AM"
},
{
"name": "pc3",
"status": "up",
"date": "8/30/11 12:20 AM"
},
{
"name": "pc4",
"status": "up",
"date": "8/26/11 08:50 AM"
},
{
"name": "pc5",
"status": "up",
"date": "8/26/11 08:55 AM"
}
],
"downcount": 1
},
{
"name": "Computer Lab 4",
"host": [
{
"name": "mac1",
"status": "up",
"date": "8/22/11 03:05 PM"
},
{
"name": "mac2",
"status": "up",
"date": "8/22/11 03:10 PM"
},
{
"name": "mac3",
"status": "up",
"date": "8/22/11 03:05 PM"
},
{
"name": "mac4",
"status": "up",
"date": "8/23/11 12:20 PM"
},
{
"name": "mac5",
"status": "up",
"date": "8/16/11 01:30 PM"
},
{
"name": "mac6",
"status": "up",
"date": "8/22/11 03:05 PM"
}
],
"downcount": 0
}
]
}
---Code so far---
<script type="text/javascript">
$(document).ready(function() {
setInterval('$("#table").trigger("getJson")', 3000);
$("#table").bind("getJson", function(event){
$.getJSON('json.php', function(data, textStatus, jqXHR){
$("#table").trigger("drawTable", data);
});
})
$("#table").bind("drawTable", function(event, json){
//build the table, json is your jsonData from the call
$.each(json.Service, function(i,object){
$.each(object.host, function(e,o){
$('#table').append("<tr><td width='30%'>"+o.name+"</td><td width='10%'><span class='status-"+o.status+"'>"+o.status+"</span></td><td width='60%'>"+o.date+"</td></tr>");
});
});
});
$(".tableRow").live("click", function(event){
//sidenavigation code
$.each(json.Service, function(n, nav) {
$('#db-sidenav ul').append("<li>"+nav.name+"</li>");
});
});
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以考虑使用 jQuery 的 .live() 功能。
http://api.jquery.com/live/
每次刷新 DOM 元素时,事件处理程序将绑定您添加的导航功能。
希望这会有所帮助。
you might consider using the .live() feature of jQuery.
http://api.jquery.com/live/
this way each time your DOM elements are refreshed, the event handler will bind the navigation function that you have added.
Hope this will help.
修复常量表附加
html 的表
js 建议
旧答案
fix the constant table appending
html for table
js advice
Old Answer