JAVA

          人生若只如初見,何事秋風悲畫扇。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            50 隨筆 :: 25 文章 :: 157 評論 :: 0 Trackbacks

          其實網上現在用JS來實現一個樹形的代碼多得不成樣子了.不過偶覺得這個還算可以的(至少比我第一次寫的那個強多了o_o)

          我在WEB中JS初始化如下:

          < ?script?language? = ? " ?javascript? " ? > ?
          Show_Root??
          = ??? 2 ?
          Insert_ID??
          = ??? " ?content? " ?
          Target_Text??
          = ??? " ?ContentCortrol? " ?
          Root_Name??
          = ??? " ?n0? " ?
          var ?myurl?? = ??? " ?http://www.aygfsteel.com/jkallen? " ?;

          ?
          // ?我以當節點處于1,?4?,7的時候作為父節點處理
          var ?n0?? = ??? new ??Node(? " ?n0? " ?,? " ?節點0? " ?,myurl,? " ?none? " ?);
          var ?n1?? = ??n0.createChild(? " ?n1? " ?,? " ?節點1? " ?,myurl,? " ?none? " ?)????;????
          var ?n2?? = ??n1.createChild(? " ?n2? " ?,? " ?節點2? " ?,myurl,? " ?none? " ?)????;
          var ?n3?? = ??n1.createChild(? " ?n3? " ?,? " ?節點3? " ?,myurl,? " ?none? " ?)????;
          var ?n4?? = ??n0.createChild(? " ?n4? " ?,? " ?節點4? " ?,myurl,? " ?none? " ?)????;
          var ?n5?? = ??n4.createChild(? " ?n5? " ?,? " ?節點5? " ?,myurl,? " ?none? " ?)????;
          var ?n6?? = ??n4.createChild(? " ?n6? " ?,? " ?節點6? " ?,myurl,? " ?none? " ?)????;
          var ?n7?? = ??n0.createChild(? " ?n7? " ?,? " ?節點7? " ?,myurl,? " ?none? " ?)????;
          var ?n8?? = ??n7.createChild(? " ?n8? " ?,? " ?節點8? " ?,myurl,? " ?none? " ?)????;
          var ?n9?? = ??n0.createChild(? " ?n9? " ?,? " ?節點9? " ?,myurl,? " ?none? " ?)????;

          loadPage(?
          " ?n0? " ?)
          ?
          </ ?script? > ?

          看起來相當笨拙.但這僅是為了表現JS罷了,其實各種node完全可以在bean中實現才是(node value也正是從DB是取才是啊)!

          先來看效果啦:



          其中一些主要的JS代碼分別如下:
          Node的定義:

          ? function ??Node(name,?text,?link,?status)
          {
          ????
          this .name?? = ??name
          ????
          this .text?? = ??text
          ????
          this .link?? = ??link
          ????
          this .status?? = ??status
          ????
          ????
          this .depth?? = ??? 0 ?
          ????
          this .order?? = ??? 1 ?
          ????
          this .parent?? = ??? "" ?
          ????
          this .folder?? = ??? 0 ?
          ????
          this .closed?? = ??? 1 ?
          ????????????
          ????
          this .childCount?? = ??? 0 ?
          ????
          this .childArray?? = ??? new ???Array?()
          ????
          ????
          this .isRoot?? = ??isRoot
          ????
          this .isFolder?? = ??isFolder
          ????
          this .isClosed?? = ??isClosed
          ????
          this .isHeadChild?? = ??isHeadChild
           ?????..
          ?????}
          ?


          產生子節點:

          ? function ??expandChild(iRoot)
          {
          ????
          var ?sOutput?? = ?? this .getMyBranch(iRoot)
          ????sOutput??
          = ??? " ?<tr?bgcolor='#FFFFFF'><td?class='text'?width=260>? " ??? + ??sOutput
          ?????
          if ??(iRoot)
          ????
          {
          ????????iDepth??
          = ??? 0 ?
          ????}

          ?????
          else ?
          ????
          {
          ????????iDepth??
          = ??? 1 ?
          ????}


          ?????
          if ??( ! ? this .isFolder())
          ????
          {
          ????????sOutput??
          += ??getImageStr(? 3 ?)?? + ??? " ?<a?href='? " ??? + ?? this .link?? + ??? " ?'?target='? " ??? + ??Target_Text?? + ??? " ?'>? " ?
          ?????????????????
          + ??? " ?<span?id='? " ??? + ?? this .name?? + ??? " ?'?class='node'?onclick=changeColor('? " ??? + ?? this .name?? + ??? " ?')>? " ?
          ?????????????????
          + ?? this .text?? + ??? " ?</span></a></td>? " ?
          ????}

          ?????
          else ?
          ????
          {
          ?????????
          if ??( this .depth?? >= ??iDepth)
          ????????
          {
          ????????????
          var ?iIconID?? = ??? 0 ?
          ?????????????
          if ??( this .isLastChild())?iIconID?? = ??? 1 ?
          ?????????????
          if ??( this .depth?? == ??iDepth)
          ????????????
          {
          ?????????????????
          if ??( this .isHeadChild())?iIconID?? = ??? 2 ?
          ?????????????????
          if ??( this .isSingleton())?iIconID?? = ??? 3 ?
          ????????????}

          ????????????????sOutput??
          += ??? " ?<a?href='JavaScript:? " ??? + ?? this .name?? + ??? " ?.changeState()'>? " ?
          ?????????????????????
          + ??getImageStr(? 1 ?,? this .isClosed()?? * ??? 4 ??? + ??iIconID)?? + ??? " ?</a>? " ?
          ????????????sOutput??
          += ??getImageStr(? 2 ?,? this .isClosed())
          ????????????????????
          ?????????????
          if ??( this .link?? == ??? "" ?)
          ????????????
          {
          ?????????????????
          // ?sOutput??+=???"?<a?href='JavaScript:?"???+??this.name??+???"?.changeState()'>?"?
          ????????????}

          ?????????????
          else ?
          ????????????
          {
          ????????????????sOutput??
          += ??? " ?<a?href='? " ??? + ?? this .link?? + ??? " ?'?target='? " ??? + ??Target_Text?? + ??? " ?'>? " ?
          ????????????}

          ????????????
          ????????????sOutput??
          += ??? " ?<span?id='? " ??? + ?? this .name?? + ??? " ?'?class='node'? " ?
          ?????????????
          if ??( this .link?? == ??? "" ?)
          ????????????
          {
          ????????????????sOutput??
          += ??? " ?>? " ?
          ????????????}

          ?????????????
          else ?
          ????????????
          {
          ????????????????sOutput??
          += ??? " ??onclick=changeColor('? " ??? + ?? this .name?? + ??? " ?')>? " ?
          ????????????}

          ????????????sOutput??
          += ?? this .text?? + ??? " ?</span></a></td>? " ??? + ??? " ?<td?width=40>? " ??? + ??getStatusImageStr( this )?? + ??? " ?</td>? " ?????????}

          ????????sOutput??
          = ??sOutput?? + ??? " ?</tr>? " ?
          ?????????
          if ??( ! ? this .isClosed())
          ????????
          {
          ?????????????
          for ??( var ?i?? = ??? 0 ?;?i? < ?? this .childCount;?i?? ++ ?)
          ????????????????sOutput??
          += ?? this .childArray[i].expandChild(iRoot)
          ????????}

          ????}

          ????
          return ?sOutput
          }
          ?


          展開父節點:

          ? function ??expandChild(iRoot)
          {
          ????
          var ?sOutput?? = ?? this .getMyBranch(iRoot)
          ????sOutput??
          = ??? " ?<tr?bgcolor='#FFFFFF'><td?class='text'>? " ??? + ??sOutput
          ?????
          if ??(iRoot)
          ????
          {
          ????????iDepth??
          = ??? 0 ?
          ????}

          ?????
          else ?
          ????
          {
          ????????iDepth??
          = ??? 1 ?
          ????}


          ?????
          if ??( ! ? this .isFolder())
          ????
          {
          ????????sOutput??
          += ??getImageStr(? 3 ?)?? + ??? " ?<a?href='? " ??? + ?? this .link?? + ??? " ?'?target='? " ??? + ??Target_Text?? + ??? " ?'>? " ?
          ?????????????????
          + ??? " ?<span?id='? " ??? + ?? this .name?? + ??? " ?'?class='node'?onclick=changeColor('? " ??? + ?? this .name?? + ??? " ?')>? " ?
          ?????????????????
          + ?? this .text?? + ??? " ?</span></a></td>? " ?
          ????}

          ?????
          else ?
          ????
          {
          ?????????
          if ??( this .depth?? >= ??iDepth)
          ????????
          {
          ????????????
          var ?iIconID?? = ??? 0 ?
          ?????????????
          if ??( this .isLastChild())?iIconID?? = ??? 1 ?
          ?????????????
          if ??( this .depth?? == ??iDepth)
          ????????????
          {
          ?????????????????
          if ??( this .isHeadChild())?iIconID?? = ??? 2 ?
          ?????????????????
          if ??( this .isSingleton())?iIconID?? = ??? 3 ?
          ????????????}

          ????????????????sOutput??
          += ??? " ?<a?href='JavaScript:? " ??? + ?? this .name?? + ??? " ?.changeState()'>? " ?
          ?????????????????????
          + ??getImageStr(? 1 ?,? this .isClosed()?? * ??? 4 ??? + ??iIconID)?? + ??? " ?</a>? " ?
          ????????????sOutput??
          += ??getImageStr(? 2 ?,? this .isClosed())
          ????????????????????
          ?????????????
          if ??( this .link?? == ??? "" ?)
          ????????????
          {
          ?????????????????
          // ?sOutput??+=???"?<a?href='JavaScript:?"???+??this.name??+???"?.changeState()'>?"?
          ????????????}

          ?????????????
          else ?
          ????????????
          {
          ????????????????sOutput??
          += ??? " ?<a?href='? " ??? + ?? this .link?? + ??? " ?'?target='? " ??? + ??Target_Text?? + ??? " ?'>? " ?
          ????????????}

          ????????????
          ????????????sOutput??
          += ??? " ?<span?id='? " ??? + ?? this .name?? + ??? " ?'?class='node'? " ?
          ?????????????
          if ??( this .link?? == ??? "" ?)
          ????????????
          {
          ????????????????sOutput??
          += ??? " ?>? " ?
          ????????????}

          ?????????????
          else ?
          ????????????
          {
          ????????????????sOutput??
          += ??? " ??onclick=changeColor('? " ??? + ?? this .name?? + ??? " ?')>? " ?
          ????????????}

          ????????????sOutput??
          += ?? this .text?? + ??? " ?</span></a></td>? " ?
          ????????}

          ????????sOutput??
          = ??sOutput?? + ??? " ?</tr>? " ?
          ?????????
          if ??( ! ? this .isClosed())
          ????????
          {
          ?????????????
          for ??( var ?i?? = ??? 0 ?;?i? < ?? this .childCount;?i?? ++ ?)
          ????????????????sOutput??
          += ?? this .childArray[i].expandChild(iRoot)
          ????????}

          ????}

          ????
          return ?sOutput
          }
          ?


          注意:

          從以上可以看到在WEB頁面中得有相關JS代碼初始化Insert_ID(create space to storage the menu) 如:

          < table? width ="100%" >
          < tr >
          ?
          < td? valign ="top" ?id ="content" ?nowrap >
          ?
          </ td >
          </ tr >
          </ table > ?

          Insert_ID?=?"content"http://JS代碼?

          ?

          在WEB頁面中得初始化Root_Name(此處為"n0")

          JS中沒有提供對父節點的判斷,而是直接(依據當前節點的下標)取得某一節點的父節點(反正菜單項一般是從DB中select的,所以你可以在bean中去處理).我覺得也許可以將此功能也在JS中實現,將DB中的數據直接提到XML(or我們JS是從JS中提取數據的)....

          肚子餓了,先閃人....

          posted on 2006-03-28 18:15 Jkallen 閱讀(17569) 評論(13)  編輯  收藏 所屬分類: AJAX

          評論

          # re: 打造完美的JS樹形菜單 2006-04-20 13:42 qinjm
          產生子節點  回復  更多評論
            

          # re: 打造完美的JS樹形菜單 2006-09-28 08:53 小丫
          能否給出能運行出來您貼圖效果的完整代碼??比較了許多樹形菜單,只覺得您這個是最好的,可惜運行不了。遺憾!!! gsyfj@163.com  回復  更多評論
            

          # re: 打造完美的JS樹形菜單 2006-09-28 08:54 小丫
          能否給出能運行出來您貼圖效果的完整代碼??比較了許多樹形菜單,只覺得您這個是最好的,可惜運行不了。遺憾!!!  回復  更多評論
            

          # re: 打造完美的JS樹形菜單 2006-09-28 17:31 藍天
          近二天找個時間一定全部貼出來! :-)  回復  更多評論
            

          # 好東西,快貼出來啊 2007-03-22 14:00 天下有賊
          我頂  回復  更多評論
            

          # re: 打造完美的JS樹形菜單[未登錄] 2007-07-04 10:08 無名
          能不能搞個能運行的完整的工程給我啊,謝謝了!
            回復  更多評論
            

          # re: 打造完美的JS樹形菜單 2007-07-06 13:32 藍天
          我發現了更好的東東里面有JS的樹
          共享下:

          http://www.scbr.com/  回復  更多評論
            

          # re: 打造完美的JS樹形菜單 2008-06-18 12:45 fan
          我還沒弄明白,接收能力太差!!!
          我也希望有個運行后的完整代碼提供.
          有的話,請發給我,在此謝謝了!

          我的聯系方式:254743383
          郵箱:254743383@qq.com  回復  更多評論
            

          # re: 打造完美的JS樹形菜單 2009-06-08 15:15 000
          這個無限級可刷新Js樹型菜單 dTree
           1、可設置無限級菜單
           2、不必使用框架
           3、可刷新,多頁面內跳轉不會影響菜單
           4、可限級創造子樹
           5、支持目前主流瀏覽器:IE5,6,7
           6、節點圖片可設置切換圖片效果


          下載url:http://www.destroydrop.com/javascripts/tree/
            回復  更多評論
            

          # re: 打造完美的JS樹形菜單 2010-07-11 18:50 jenny lu
          看看@qinjm
            回復  更多評論
            

          # re: 打造完美的JS樹形菜單 2010-07-11 18:51 jenny lu
          看看  回復  更多評論
            

          # re: 打造完美的JS樹形菜單 2010-10-08 21:42 liuzhongshu
          是的,dTree要更好一些, http://www.liuzhongshu.com/code/javascript-tree-dtree.html  回復  更多評論
            

          # re: 打造完美的JS樹形菜單[未登錄] 2016-05-26 18:01 dd
          dsd  回復  更多評論
            

          主站蜘蛛池模板: 德令哈市| 囊谦县| 福贡县| 浮山县| 安多县| 密山市| 阳新县| 安丘市| 若尔盖县| 深水埗区| 安顺市| 海晏县| 宣武区| 军事| 襄汾县| 津南区| 盱眙县| 台南市| 安宁市| 安阳市| 镇平县| 塔河县| 麻阳| 渝中区| 雷州市| 彝良县| 庆元县| 乌恰县| 西畴县| 西丰县| 平罗县| 连城县| 嘉黎县| 栾城县| 胶南市| 湘潭县| 岳阳县| 大方县| 达尔| 武汉市| 沂水县|