找回密码
 注册
首页 ≡≡网络技术≡≡ WEB前端 Web页面中动态目录树的生成

Web页面中动态目录树的生成

灰儿 2016-9-1 16:08:47
一、为什么需要动态显示的目录树
       由于用目录树显示数据层次性强,在web页面中目录树的使用可以说是随处可见.其实现方法也有好几种.
       方案一.  我们可以直接去msdn去下载webcontrol包,里面包含了一个TreeView控件,它是一个web的服务器端控件,添加引用之后,就可以直接添加到页面中了,开发起来也很简单,数据既可以直接添加,也可以通过数据绑定动态添加,但是它的缺点也很明显:由于每次显示时,它把需要显示的数据全部都加载,如果数据量很大的话,效率可想而知.
     方案二.  我们也可以用javascript编写,然后用css控制它的样式,由于javascript是在客户端运行的,这种方案实现的目录树减轻了服务器的负担,但是显示的数据不能动态绑定.
     试想一个这样的应用环境:我们需要把上万个文件夹和文件用一个目录树加载,文件夹中可以包含多个目录和文件,而这些文件夹和文件的信息都保存在数据库中,我们该怎么做?方案一和方案二显然都行不通,这时我们可以先把第一层目录加载在页面中显示出来(不是全部加载),其它的目录和文件就按需显示,即什么时候用户请求显示,内容什么时候加载.

二、如何实现
 在客户端
JavaScript代码:




  1. function InsertChildTree1(str,o)
  2. {
  3. if(o==null || o.parentNode==null || str==null)
  4.              {
  5. alert("Error.  [3000] object expected!");
  6. return;
  7. }

  8. if(o.parentNode.className=="Closed")return;
  9. if(o.IsLoad)
  10. {
  11.    if(o.IsLoad=="1")return;
  12. }

  13. var response=TreeData.InsertTreeData(str);
  14. var ds=response.value;
  15. if(ds != null && typeof(ds) == "object" && ds.Tables != null)
  16. {

  17.   for(var i=0;i<ds.Tables[0].Rows.length;i++)
  18.    {  
  19.     var oNewList=document.createElement("ul");
  20.       o.parentNode.appendChild(oNewList);
  21.       var oNewNode = document.createElement("li");
  22.       oNewList.appendChild(oNewNode);
  23.       oNewNode.className="Closed";
  24.        var oNodeImg=document.createElement("img");
  25.       
  26.       oNodeImg.setAttribute("IsLoad","0");
  27.       
  28.       var strPath;
  29.       var subCount=ds.Tables[0].Rows[i].NodeCount;
  30.       
  31.      strPath=ds.Tables[0].Rows[i].NodePath;
  32.       oNodeImg.setAttribute("strPath",strPath);
  33.       oNodeImg.className="s";
  34.      oNewNode.appendChild(oNodeImg);
  35.      
  36.     if(subCount>0)
  37.      {
  38.       oNodeImg.onclick=function(){ChangeStatus(this);InsertChildTree1(this.strPath,this);this.IsLoad="1";};
  39.       oNodeImg.src="css/s.gif";
  40.      }
  41.      else
  42.      {
  43.      oNodeImg.src="css/s.gif";
  44.      oNodeImg.parentNode.className="Child";
  45.      }
  46.       
  47.       
  48.      var oNodeHref=document.createElement("a");
  49.       oNodeHref.setAttribute("strPath",strPath);
  50.       
  51.         oNodeHref.setAttribute("href","#");
  52.       oNodeHref.onclick=function(){ViewFolder(this.strPath);};
  53.       
  54.       oNodeHref.innerText=ds.Tables[0].Rows[i].NodeText;
  55.        if(subCount<0)subCount=0;
  56.          oNodeHref.setAttribute("title","此目录下共有"+subCount+"个子文件夹");
  57.    
  58.       oNewNode.appendChild(oNodeHref);
  59.       
  60.     }
  61.       
  62. }
  63. else
  64. {
  65. alert("Error. [3001] " + response.request.responseText);
  66. }

  67. }


  68. function write(o)
  69. {
  70. if(typeof(o.request)!="object")return;

  71. if(o.request.readyState==1   && o.request!=null)
  72. {
  73. alert("ffff");

  74. }
  75. else
  76. {
  77.   return;
  78. }
  79. }

  80. //添加子树
  81. function InsertChildTree(o)
  82. {

  83. if(o.className=="Closed"  )

  84. if(o.IsLoad)
  85. {
  86. if(o.IsLoad=="1")return;
  87. }


  88. var response=TreeData.InitTreeData1();

  89.   write(response);
  90. var ds = response.value;
  91. if(ds != null<%2
复制代码


您需要登录后才可以回帖 登录 | 注册
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。
学习中心
站长自定义文字内容,利用碎片时间,随时随地获取优质内容。
Q设计语言 了解更多
Q Design 提供商家设计所需的指导与资源,帮商家快速完成产品设计、降低生产成本。