love fish大鵬一曰同風起,扶搖直上九萬里

          常用鏈接

          統計

          積分與排名

          friends

          link

          最新評論

          運用ajax技術的樹型菜單(轉)

          ?

          ?? 樹型菜單可以說是項目中應用最為廣泛的運用。以前無論使用微軟控件的樹型,還是比較優秀的阿賴樹型,都是將數據全部讀出,然后再分級顯示。這樣如果數據量大,那么第一次顯示將需要客戶等待很長一段時間,降低了客戶的體驗度。如今使用ajax,事情變得簡單了。

          ?? 此運用參考了《征服web2.0開發技術詳解》的例子。

          ???我使用的平臺是struts+spring+hibernate,但與ajax打交道的也就是struts。我無法將整個代碼貼出來,因此把重要的前臺ajax代碼貼出來,至于后臺的代碼就看你自己所使用的技術了。

          1、jsp頁面

          <% @?page?language = " java " ?contentType = " text/html;?charset=GB2312 " ? import = " java.util.*,com.wehave.oa.cecontract.model.TbJyhtflb " %>
          < html >
          < head >
          < title > Insert?title?here </ title >
          < link?rel = " stylesheet " ?href? = ? " ../css/tree.css " >
          < script?type = " text/javascript " ?src = " ../js/tree_ajax.js " ></ script >
          < script?type = " text/javascript " ?src = " ../js/tree_htfl.js " ></ script >
          < script?languge = " javascript " >
          ????function?ShowDetail(ID,NAME,FLAG)
          {
          ????????window.parent.right.location?
          = " getContract.go?method=doGetContract&folderID= " ? + ID? + ? " &&folderName= " + NAME + " &&flag= " + FLAG;
          ????}

          </ script >
          </ head >
          < body?bgcolor = " #F6F9FF " >
          < tbody >
          < table?cellpadding = " 0 " ?cellspacing = " 0 " ?width = " 300 " ? >
          ?
          < div?id = " load " ?style = " display:none " > ? < img?src = " ../images/tree_loading.gif " > Loading?data.. </ div >
          < ul? class = " tree " >
          <% ??List?treeList? = ?(List)request.getAttribute( " treefolder " );
          ????Iterator?it
          = treeList.iterator();
          ????
          while (it.hasNext()) {
          ????????out.println(it.next().toString());
          ????}

          %>
          </ table >
          </ tbody >
          </ body >
          </ html >

          2、js代碼
          function?showHide(?id?)
          {
          ??var?el
          =?document.getElementById(?id?);
          ??var?bExpand?
          =?true;
          ??var?images?
          =?el.getElementsByTagName("IMG");
          ??
          if?(images[0].src.indexOf("tree_minus.gif")!=-1)
          ??
          {
          ????bExpand?
          =?false;
          ????images[
          0].src="../images/tree_plus.gif";
          ??}
          else{
          ????images[
          0].src="../images/tree_minus.gif";
          ??}

          ??var?subs
          =el.lastChild;
          ??
          if(bExpand)
          ????subs.style.display
          ="block";
          ??
          else
          ????subs.style.display
          ="none";
          }


          function?getSubTree(?id?,submitURL)
          {
          ??var?submitURL
          =submitURL
          ??postXmlHttp(?submitURL,?
          'parseSubTree("'+id+'")'?,'load("'+id+'")');
          }

          function?parseSubTree(id)
          {
          ??var?el
          =?document.getElementById(?id?);
          ??var?ulElmt
          =?document.createElement("UL");
          ??ulElmt.innerHTML
          =_xmlHttpRequestObj.responseText;
          ??el.appendChild(ulElmt);
          ??var?images?
          =?el.getElementsByTagName("IMG");
          ??images[
          0].setAttribute("src",?"../images/tree_minus.gif");
          ??images[
          0].setAttribute("onclick",?new?Function("showHide('"+id+"')"));
          ??var?aTag?
          =?el.getElementsByTagName("A");
          ??aTag[
          0].setAttribute("onclick",?new?Function("showHide('"+id+"')"));
          ??var?loadDiv
          =?document.getElementById(?"load"?);
          ??loadDiv.style.display
          ="none";
          }


          function?load(id)
          {
          ?var?loadDiv
          =?document.getElementById(?"load"?);
          ?loadDiv.style.display
          ="block";
          }


          var?_postXmlHttpProcessPostChangeCallBack;
          var?_xmlHttpRequestObj;
          var?_loadingFunction;

          function?postXmlHttp(?submitUrl,?callbackFunc?,loadFunc)
          {
          ??_postXmlHttpProcessPostChangeCallBack?
          =?callbackFunc;
          ??_loadingFunction?
          =?loadFunc;
          ??
          if(window.createRequest)
          ??
          {
          ????
          try{
          ??????_xmlHttpRequestObj
          =window.createRequest();
          ??????_xmlHttpRequestObj.open(
          'POST',submitUrl,true);
          ??????_xmlHttpRequestObj.onreadystatechange
          =postXmlHttpProcessPostChange;
          ??????_xmlHttpRequestObj.send();
          ????}

          ????
          catch(ee){}
          ??}

          ??
          else?if(window.XMLHttpRequest)
          ??
          {
          ????_xmlHttpRequestObj
          =new?XMLHttpRequest();
          ????_xmlHttpRequestObj.overrideMimeType(
          'text/xml');
          ????_xmlHttpRequestObj.open(
          'POST',submitUrl,true);
          ????_xmlHttpRequestObj.onreadystatechange
          =postXmlHttpProcessPostChange;
          ????_xmlHttpRequestObj.send(
          "");
          ??}

          ??
          else?if(window.ActiveXObject)
          ??
          {
          ????_xmlHttpRequestObj
          =new?ActiveXObject("Microsoft.XMLHTTP");
          ????_xmlHttpRequestObj.open(
          'POST',submitUrl,true);
          ????_xmlHttpRequestObj.onreadystatechange
          =postXmlHttpProcessPostChange;
          ????_xmlHttpRequestObj.send();
          ??}

          }
          ;

          function?postXmlHttpProcessPostChange(?)
          {
          ??
          if(?_xmlHttpRequestObj.readyState==4)
          ??
          {
          ??????
          if(_xmlHttpRequestObj.status==200){
          ????????setTimeout(?_postXmlHttpProcessPostChangeCallBack,?
          2?);
          ????}
          else{
          ????????alert(_xmlHttpRequestObj.status);
          ????}

          ??}

          ??
          if?(?_xmlHttpRequestObj.readyState==1?)
          ??
          {
          ????setTimeout(?_loadingFunction,?
          2?);
          ??}

          }

          3、action代碼

          /**
          ?????*?展開第一層目錄
          ?????
          */

          ????
          public?ActionForward?doGetFolderList(
          ????????????ActionMapping?mapping,
          ????????????ActionForm?form,
          ????????????HttpServletRequest?req,
          ????????????HttpServletResponse?res)
          {
          ????????List?list?
          =?treeCatalogService.getChildren("0");
          ????????List?TreeFolder
          =new?ArrayList();
          ????????Iterator?it
          =list.iterator();
          ????????
          while(it.hasNext()){
          ????????????TbJyhtflb?htfl
          =(TbJyhtflb)it.next();
          ????????????String?s
          =treeCatalogService.renderTreeViewAjax(htfl);
          ????????????TreeFolder.add(s);
          ????????}

          ????????req.setAttribute(
          "treefolder",TreeFolder);
          ????????
          return?mapping.findForward("foldertree");
          ????}

          ????
          ????
          /**
          ?????*?展開下級目錄
          ?????
          */

          ????
          public?ActionForward?doGetSubFolderList(
          ????????????ActionMapping?mapping,
          ????????????ActionForm?form,
          ????????????HttpServletRequest?req,
          ????????????HttpServletResponse?res)
          {
          ????????String?parentID?
          =?req.getParameter("parentID");?//獲得id的值
          ????????if?(parentID!=null&&!parentID.equals("")){?//如果不為null和空
          ????????????res.setContentType("text/html;charset=GB2312");
          ????????????List?list?
          =?treeCatalogService.getChildren(parentID);
          ????????????Iterator?it
          =list.iterator();
          ????????????
          try?{
          ????????????????PrintWriter?out
          =?res.getWriter();
          ????????????????
          while(it.hasNext()){
          ????????????????????TbJyhtflb?htfl
          =(TbJyhtflb)it.next();
          ????????????????????out.println(treeCatalogService.renderTreeViewAjax(htfl));
          ????????????????}

          ????????????????out.close();
          ????????????}
          catch(Exception?e){
          ????????????????e.printStackTrace();
          ????????????}

          ????????}

          ????????
          return?null;
          ????}

          4、service層代碼
          /**
          ?????*?函數說明:展開目錄
          ?????*?參數說明:?目錄對象
          ?????*?返回值:展開目錄的HTML代碼
          ?????
          */

          ????
          public?String?renderTreeViewAjax(TbJyhtflb?htfl)?{
          ????????StringBuffer?content?
          =?new?StringBuffer();
          ????????String?ID
          =htfl.getTbJyhtflbZlId();
          ????????String?NAME
          =htfl.getTbJyhtflbMc();
          ????????String?FLAG
          =htfl.getTbJyhtflbLb();
          ????????content.append(
          "<li?id='"+ID+"'>");
          ????????
          if?(treeCatalogDAO.canExpand(ID))
          ????????????content.append(
          "<img?src=../images/tree_plus.gif?onClick=\"getSubTree('"+ID+"')\">");
          ????????
          else
          ????????????content.append(
          "<img?src=../images/tree_blank.gif>");
          ????????content.append(
          "<img?src=../images/tree_folder.gif><a?href=\"javascript:ShowDetail('"+ID+"','"+NAME+"','"+FLAG+"')\"");
          ????????
          if?(treeCatalogDAO.canExpand(ID)){
          ????????????String?submitURL
          ="getFolderList.go?method=doGetSubFolderList&parentID="+ID;
          ????????????content.append(
          "?onClick=\"getSubTree('"+ID+"',submitURL)\"");
          ????????}

          ????????content.append(
          ">"+NAME+"</a>");
          ????????
          ????????content.append(
          "</li>");
          ????????
          return?content.toString();
          ????}


          代碼基本就是這樣了,希望對大家有用。

          posted on 2006-07-24 15:24 liaojiyong 閱讀(571) 評論(0)  編輯  收藏 所屬分類: Ajax

          主站蜘蛛池模板: 来凤县| 曲松县| 新河县| 宁波市| 元氏县| 西乡县| 定远县| 彭阳县| 海丰县| 凤翔县| 长治市| 鹰潭市| 巴马| 景谷| 凤城市| 郯城县| 西峡县| 九江县| 湛江市| 万源市| 应城市| 九龙县| 田林县| 双牌县| 高邑县| 临潭县| 会东县| 红安县| 鹿泉市| 伽师县| 乐山市| 即墨市| 邢台县| 陆丰市| 刚察县| 禹城市| 镇原县| 灵寿县| 平邑县| 万盛区| 琼结县|