如何从Json数据构建菜单(在aspx页面中) [英] How to build Menus from Json data (in aspx page)

查看:84
本文介绍了如何从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);


              }
             }

推荐答案

.connection.dataHub.client.onReceiveCategories = function(catList)
{
if (catList!= null & & catList.length > 0
{
categorieHTML = ' ';
var builddata = function()
{
alert(catList);
var source = [];
var items = [];
// 构建分层来源。
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;
}
其他 {
items [id] = {parentid:parentid,label:label,item:item};
source [id] = items [id];
}
}
返回来源;

}
var source = builddata();



var buildUL = function(parent,items){
.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){
// 创建LI元素并将其附加到父元素。
var li =
.each(items, function () { if (this.label) { // create LI element and append it to the parent element. var li =


< li> + .label + < / li>);
li.appendTo(parent);
// 如果有子项,则调用buildUL函数。
如果 .items&& .items.length > 0 ){
var ul =
("<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 =


这篇关于如何从Json数据构建菜单(在aspx页面中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆