本文介绍了如何从Json数据构建菜单(在aspx页面中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<script type="text/javascript">
var cat, categorieHTML;
$.connection.dataHub.client.onReceiveCategories = function (catList)
{
if (catList != null && catList.length > 0)
{
categorieHTML = '';
var builddata = function ()
{
alert(catList);
var source = [];
var items = [];
// build hierarchical source.
for (i = 0; i < catList.length; i++) {
alert(catList.length);
var item = catList[i];
var label = item["text"];
var parentid = item["parentid"];
var id = item["id"];
alert(item["text"]);
if (items[parentid]) {
var item = { parentid: parentid, label: label, item: item };
if (!items[parentid].items) {
items[parentid].items = [];
}
items[parentid].items[items[parentid].items.length] = item;
items[id] = item;
}
else {
items[id] = { parentid: parentid, label: label, item: item };
source[id] = items[id];
}
}
return source;
}
var source = builddata();
var buildUL = function (parent, items) {
$.each(items, function () {
if (this.label) {
// create LI element and append it to the parent element.
var li = $("<li>" + this.label + "</li>");
li.appendTo(parent);
// if there are sub items, call the buildUL function.
if (this.items && this.items.length > 0) {
var ul = $("<ul></ul>");
ul.appendTo(li);
buildUL(ul, this.items);
}
}
});
}
var ul = $("<ul></ul>");
ul.appendTo("#mainNav");
buildUL(ul, source);
}
}
推荐答案
这篇关于如何从Json数据构建菜单(在aspx页面中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!