JAVA

          人生若只如初見(jiàn),何事秋風(fēng)悲畫(huà)扇。

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            50 隨筆 :: 25 文章 :: 157 評(píng)論 :: 0 Trackbacks
          <2009年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          公告

            In life there are very rare chances that you'll meet the person you love and loves you in return. So once you have it don't ever let go, the chance might never come your way.

          常用鏈接

          留言簿(20)

          隨筆分類(lèi)(55)

          隨筆檔案(50)

          文章分類(lèi)(2)

          文章檔案(25)

          相冊(cè)

          JAVA

          友人鏈接

          實(shí)用查詢(xún)

          珍藏鏈接

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          其實(shí)網(wǎng)上現(xiàn)在用JS來(lái)實(shí)現(xiàn)一個(gè)樹(shù)形的代碼多得不成樣子了.不過(guò)偶覺(jué)得這個(gè)還算可以的(至少比我第一次寫(xiě)的那個(gè)強(qiáng)多了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? " ?;

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

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

          看起來(lái)相當(dāng)笨拙.但這僅是為了表現(xiàn)JS罷了,其實(shí)各種node完全可以在bean中實(shí)現(xiàn)才是(node value也正是從DB是取才是啊)!

          先來(lái)看效果啦:



          其中一些主要的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
           ?????..
          ?????}
          ?


          產(chǎn)生子節(jié)點(diǎn):

          ? 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
          }
          ?


          展開(kāi)父節(jié)點(diǎn):

          ? 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頁(yè)面中得有相關(guān)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頁(yè)面中得初始化Root_Name(此處為"n0")

          JS中沒(méi)有提供對(duì)父節(jié)點(diǎn)的判斷,而是直接(依據(jù)當(dāng)前節(jié)點(diǎn)的下標(biāo))取得某一節(jié)點(diǎn)的父節(jié)點(diǎn)(反正菜單項(xiàng)一般是從DB中select的,所以你可以在bean中去處理).我覺(jué)得也許可以將此功能也在JS中實(shí)現(xiàn),將DB中的數(shù)據(jù)直接提到XML(or我們JS是從JS中提取數(shù)據(jù)的)....

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

          posted on 2006-03-28 18:15 Jkallen 閱讀(17571) 評(píng)論(13)  編輯  收藏 所屬分類(lèi): AJAX

          評(píng)論

          # re: 打造完美的JS樹(shù)形菜單 2006-04-20 13:42 qinjm
          產(chǎn)生子節(jié)點(diǎn)  回復(fù)  更多評(píng)論
            

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

          # re: 打造完美的JS樹(shù)形菜單 2006-09-28 08:54 小丫
          能否給出能運(yùn)行出來(lái)您貼圖效果的完整代碼??比較了許多樹(shù)形菜單,只覺(jué)得您這個(gè)是最好的,可惜運(yùn)行不了。遺憾!!!  回復(fù)  更多評(píng)論
            

          # re: 打造完美的JS樹(shù)形菜單 2006-09-28 17:31 藍(lán)天
          近二天找個(gè)時(shí)間一定全部貼出來(lái)! :-)  回復(fù)  更多評(píng)論
            

          # 好東西,快貼出來(lái)啊 2007-03-22 14:00 天下有賊
          我頂  回復(fù)  更多評(píng)論
            

          # re: 打造完美的JS樹(shù)形菜單[未登錄](méi) 2007-07-04 10:08 無(wú)名
          能不能搞個(gè)能運(yùn)行的完整的工程給我啊,謝謝了!
            回復(fù)  更多評(píng)論
            

          # re: 打造完美的JS樹(shù)形菜單 2007-07-06 13:32 藍(lán)天
          我發(fā)現(xiàn)了更好的東東里面有JS的樹(shù)
          共享下:

          http://www.scbr.com/  回復(fù)  更多評(píng)論
            

          # re: 打造完美的JS樹(shù)形菜單 2008-06-18 12:45 fan
          我還沒(méi)弄明白,接收能力太差!!!
          我也希望有個(gè)運(yùn)行后的完整代碼提供.
          有的話,請(qǐng)發(fā)給我,在此謝謝了!

          我的聯(lián)系方式:254743383
          郵箱:254743383@qq.com  回復(fù)  更多評(píng)論
            

          # re: 打造完美的JS樹(shù)形菜單 2009-06-08 15:15 000
          這個(gè)無(wú)限級(jí)可刷新Js樹(shù)型菜單 dTree
           1、可設(shè)置無(wú)限級(jí)菜單
           2、不必使用框架
           3、可刷新,多頁(yè)面內(nèi)跳轉(zhuǎn)不會(huì)影響菜單
           4、可限級(jí)創(chuàng)造子樹(shù)
           5、支持目前主流瀏覽器:IE5,6,7
           6、節(jié)點(diǎn)圖片可設(shè)置切換圖片效果


          下載url:http://www.destroydrop.com/javascripts/tree/
            回復(fù)  更多評(píng)論
            

          # re: 打造完美的JS樹(shù)形菜單 2010-07-11 18:50 jenny lu
          看看@qinjm
            回復(fù)  更多評(píng)論
            

          # re: 打造完美的JS樹(shù)形菜單 2010-07-11 18:51 jenny lu
          看看  回復(fù)  更多評(píng)論
            

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

          # re: 打造完美的JS樹(shù)形菜單[未登錄](méi) 2016-05-26 18:01 dd
          dsd  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 盐源县| 东乡| 青铜峡市| 阳泉市| 广丰县| 平原县| 中阳县| 南岸区| 沛县| 云阳县| 平凉市| 柞水县| 如东县| 东阿县| 梁山县| 潼南县| 申扎县| 卢湾区| 无锡市| 赫章县| 龙里县| 永福县| 汉川市| 新乡县| 安福县| 祁门县| 抚顺县| 廉江市| 年辖:市辖区| 蕲春县| 井陉县| 安义县| 宝鸡市| 扬州市| 贵德县| 太湖县| 石首市| 太白县| 温宿县| 尼勒克县| 绥棱县|