我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換

          jstree 主頁 :
          http://www.jstree.com/

          其中提供了一種從后臺(tái)取數(shù)據(jù)渲染成樹的形式:
           $("#mytree").tree({
                data  : 
          {
                  type  : 
          "json",
                  url : 
          "${ctx}/user/power!list.do"
                }

          }
          );

          對于url中返回的值必須是它定義的json數(shù)據(jù)形式:

          $("#demo2").tree({
            data  : 
          {
              type  : 
          "json",
              json  : [ 
                
          { attributes: { id : "pjson_1" }, state: "open", data: "Root node 1", children : [
                  
          { attributes: { id : "pjson_2" }, data: { title : "Custom icon", icon : "../media/images/ok.png" } },
                  
          { attributes: { id : "pjson_3" }, data: "Child node 2" },
                  
          { attributes: { id : "pjson_4" }, data: "Some other child node" }
                ]}

                
          { attributes: { id : "pjson_5" }, data: "Root node 2" } 
              ]
            }

          }
          );


          這里需要一個(gè)從后臺(tái)實(shí)例集合轉(zhuǎn)換為它規(guī)定的json數(shù)據(jù)的形式.

          /**
               * 無限遞歸獲得jsTree的json字串
               * 
               * 
          @param parentId
               *            父權(quán)限id
               * 
          @return
               
          */

              
          private String getJson(long parentId)
              
          {
                  
          // 把頂層的查出來
                  List<Action> actions = actionManager.queryByParentId(parentId);
                  
          for (int i = 0; i < actions.size(); i++)
                  
          {
                      Action a 
          = actions.get(i);
                      
          // 有子節(jié)點(diǎn)
                      if (a.getIshaschild() == 1)
                      
          {
                          str 
          += "{attributes:{id:\"" + a.getAnid()
                                  + "\"}
          ,state:\"open\",data:\"" + a.getAnname() + "\" ,";
                          str += "children:[";
                          
          // 查出它的子節(jié)點(diǎn)
                          List<Action> list = actionManager.queryByParentId(a.getAnid());
                          
          // 遍歷它的子節(jié)點(diǎn)
                          for (int j = 0; j < list.size(); j++)
                          
          {
                              Action ac 
          = list.get(j);
                              
          //還有子節(jié)點(diǎn)(遞歸調(diào)用)
                              if (ac.getIshaschild() == 1)
                              
          {
                                  
          this.getJson(ac.getParentid());
                              }

                              
          else
                              
          {

                                  str 
          += "{attributes:{id:\"" + ac.getAnid()
                                          + "\"}
          ,state:\"open\",data:\"" + ac.getAnname()
                                          
          + "\" " + "   }
          ";
                                  if (j < list.size() - 1)
                                  
          {
                                      str 
          += ",";
                                  }

                              }

                          }

                          str 
          += "]";
                          str 
          += "   }";
                          
          if (i < actions.size() - 1)
                          
          {
                              str 
          += ",";
                          }

                      }
                  }
                  
          return str;
              }

          調(diào)用:
          @org.apache.struts2.convention.annotation.Action(results =
              
          { @Result(name = "success", location = "/main/user/action-list.jsp") })
              
          public String list()
              
          {
                  String str 
          = "[";
                  
          // 從根開始
                  str += this.getJson(0);
                  str 
          += "]";
                  
          this.renderJson(str);
                  
          return null;
              }

          其中Action是菜單類或權(quán)限類等的實(shí)體。
          效果圖:



          posted on 2009-05-05 17:09 々上善若水々 閱讀(17625) 評論(27)  編輯  收藏

          評論

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          exttree最好了!配合dwr ok
          2009-05-05 22:50 | 大羅卜

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          Action ac = list.get(j);
          //還有子節(jié)點(diǎn)(遞歸調(diào)用)
          if (ac.getIshaschild() == 1)
          {
          this.getJson(ac.getParentid());
          }
          else
          {

          help
          2009-05-05 23:56 | web tasar?m?

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          @org.apache.struts2.convention.annotation.Action(results =
          { @Result(name = "success", location = "/main/user/action-list.jsp") })
          public String list()
          {
          String str = "[";
          // 從根開始
          str += this.getJson(0);
          str += "]";
          this.renderJson(str);
          return null;
          }
          2009-05-05 23:56 | yuz estetigi

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          $("#demo2").tree({
          data : {
          type : "json",
          json : [
          { attributes: { id : "pjson_1" }, state: "open", data: "Root node 1", children : [
          { attributes: { id : "pjson_2" }, data: { title : "Custom icon", icon : "../media/images/ok.png" } },
          { attributes: { id : "pjson_3" }, data: "Child node 2" },
          { attributes: { id : "pjson_4" }, data: "Some other child node" }
          ]},
          { attributes: { id : "pjson_5" }, data: "Root node 2" }
          ]
          }
          });
          2009-05-05 23:57 | Burun Estetigi

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          這個(gè)樹真的很不錯(cuò), 還有內(nèi)置的右鍵菜單及事件調(diào)用. 比 xloadtree 好看.
          2009-05-06 09:28 | BeanSoft

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          不知道怎么回事,我用這個(gè)樹的時(shí)候,發(fā)現(xiàn)很多問題,最有意思的是,我用不壓縮的能用,用那個(gè)min版竟然報(bào)錯(cuò)。
          2009-05-06 13:38 | 陽衡鋒

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          @陽衡鋒
          請確認(rèn)已經(jīng)導(dǎo)入了
          <script type="text/javascript" src="path-to-jsTree/_lib.js"></script>

          我用<script type="text/javascript" src="path-to-jsTree/tree_component.min.js"></script>試了下,在我的工程里沒發(fā)現(xiàn)問題.ff和ie均測試。 
          2009-05-07 08:26 | 々上善若水々

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          沒,我用<script type="text/javascript" src="path-to-jsTree/tree_component.min.js"></script>跑得好好的,換成
          <script type="text/javascript" src="path-to-jsTree/tree_component.min.js"></script>就報(bào)錯(cuò)了。我發(fā)郵件給jstree的作者,他要我去下載最新版,下載下來的還是一樣。不知道最新的版本時(shí)候解決了這個(gè)問題。
          2009-05-07 12:47 | 陽衡鋒

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換[未登錄]  回復(fù)  更多評論   

          能不能把右鍵菜單給取消?
          2009-09-07 21:37 | 風(fēng)

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          @風(fēng)
          可以,請自行參考jstree api
          2009-09-09 13:37 | 々上善若水々

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          @陽衡鋒
          作者在最新版里提供的下載確實(shí)是報(bào)錯(cuò)的,jquery.xslt.js這個(gè)插件不知道為什么被作者給換掉了,貌似也不是最新版的插件,在火狐下是好的,ie下解析xml是有問題的,你從插件的官方下載最新的替換就沒事了,或者用作者老版本的這個(gè)插件。
          2009-09-12 16:58 | 周萬峰

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          @org.apache.struts2.convention.annotation.Action(results =
          { @Result(name = "success", location = "/main/user/action-list.jsp") })
          public String list()
          {
          String str = "[";
          // 從根開始
          str += this.getJson(0);
          str += "]";
          this.renderJson(str);
          return null;
          }
          這個(gè)action 中 的 this.renderJson 引用那個(gè)類庫?
          2013-08-22 08:52 | javaWeb

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          sdaf
          2014-03-04 15:18 | @fei

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          json轉(zhuǎn)換為json樹形數(shù)據(jù),怎么寫啊,其中字段里有兩個(gè)節(jié)點(diǎn),一個(gè)是ID,一個(gè)是parentID,怎么樹形顯示啊?
          2014-03-04 15:20 | @fei

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          {ID: "pjson_2"}, Veri: {title: "Özel simge", simgesi: ".. / media / images / ok.png"}},
          {Özellikleri: {ID: "pjson_3"}, Veri: "Çocuk dü?üm 2"},
          {Özellikleri: {ID: "pjson_4"}, Veri: "Di?er Baz? Çocuk dü?üm"}
          ]},

          http://otomatikrentacar.com
          2014-06-24 03:24 | Otomatik Rent A Car

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          : "pjson_2"}, Veri: {title: "Özel simge", simgesi: ".. / media / images / ok.png"}},
          {Özellikleri: {ID: "pjson_3"}, Veri: "Çocuk dü?üm 2"},
          {Özellikleri: {ID: "pjson_4"}, Veri: "Di?er Baz? Çocuk
          http://ilkyazreklam.com
          2014-07-14 21:00 | ilkyaz reklam

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          : "pjson_2"}, Veri: {title: "Özel simge", simgesi: ".. / media / images / ok.png"}},
          {Özellikleri: {ID: "pjson_3"}, Veri: "Çocuk dü?üm 2"},
          {Özellikleri: {ID: "pjson_4"}, Veri: "Di?er Baz? Çocuk "}
          http://ilkyazreklam.com
          2014-07-14 21:01 | ilkyaz reklam

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          ukash bozdurma icin zel simge", simgesi: ".. / media / images / ok.png"}},
          {Özellikleri: {ID: "pjson_3"}, Veri: ") Result(name = "success", location = "/main/user/action-list.jsp") })
          public String list()
          {
          String str = "[";
          // 從根開始
          str += this.getJson(0);
          str += "]";
          this.renderJson
          2014-07-14 21:05 | ukash bozdurma

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          likleri: {ID: "pjson_3"}, Veri: ") Sonuç (name =" Ba?ar? ", Location =" / ana / kullan?c? / Aksiyon-list.jsp ")})
          Kamu Dize listesi ()
          {
          Dize str = "[";
          / / root gelen
          str + = this.getJson (0);
          str + = ""]; http://www.hepsiukash.com/p/ukash-bozdurma.html
          2014-07-14 21:06 | ukash bozdurma

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          Bahis, Poker, Casino, Rulet, Tavla, Batak, ?ddaa, Okey, Android Bahis, ?phone Bahis, Best10 CAsino, Bets10 Casino


          http://www.androidbahis.com/
          2014-07-15 09:33 | ahmet katar

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          { attributes: { id : "pjson_2" }, data: { title : "Custom icon", icon : "../media/images/ok.png" }

          + "\"},state:\"open\",data:\"" + a.getAnname() + "\" ,";

          { @Result(name = "success", location = "/main/user/action-list.jsp") })


          https://www.oddsring404.com/livecasino?wm=8308867
          2014-07-25 09:31 | OddsRing Giri?

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          Kilercio?lu evden eve nakliyat ?ehir içi ve ?ehirler aras? ta??mac?l?k için kurumsal web sayfam?z http://www.sehirlerarasievdenevetasimacilik.com.tr
          2014-08-05 01:42 | ?ehirleraras? nakliyat

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          kilercio?lu nakliyat e?ya depolama ofis ta??ma ve kurumsal ta??mac?l?k hizmetleri. http://www.istanbulizmirevdenevenakliye.com
          2014-08-05 01:44 | e?ya depolama

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          Kurumsal ta??mac?l?k i? yeri ta??ma ofis ta??mac?l??? ve evden eve nakliyat
          2014-08-05 01:46 | ofis ta??ma ?irketleri

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          ts2.convention.annotation.Action(results =
          { @Result(name = "success", location = "/main/user/action-list.jsp") })
          public String list()

          http://www.bozdurmak.com/
          2014-08-08 05:54 | ukash bozdurma

          #  re  回復(fù)  更多評論   

          額r熱r
          2014-08-22 15:52 | f'f

          # re: 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換  回復(fù)  更多評論   

          d : "pjson_3" }, data: "Child node 2" },
          { attributes: { id : "pjson_4" }, data: "Some other child node" }
          ]},
          { attributes: { id : "pj
          2015-04-18 15:37 | https://iddaaoyunlari.com/

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 万全县| 且末县| 商水县| 米泉市| 通山县| 尚义县| 蒙城县| 景德镇市| 随州市| 霍邱县| 晋城| 盖州市| 廊坊市| 古蔺县| 和政县| 大新县| 禹州市| 晋中市| 呼图壁县| 兴和县| 剑川县| 赣州市| 仲巴县| 绥芬河市| 盐源县| 翼城县| 台州市| 潮州市| 台南市| 赞皇县| 友谊县| 通海县| 焉耆| 五大连池市| 安阳县| 莱阳市| 乌兰浩特市| 饶平县| 肥城市| 长沙市| 新野县|