无限级树,ajax+asp.net2.0+Sql实现无限树

之前用过asp.net的TreeView控件,感觉不是很灵活.
现在用ajax来实现,其中数据库为固定格式(id,fatherid...),可以通过存储过程来生成.
第一部分:ajax的简单介绍
   从网上下载ajaxpro.dll(2.0),在visual studio.net2005网站项目中添加引用,配置好config文件,在页面的page_load函数里注册.
注册如下:
   Utility.RegisterTypeForAjax(typeof(JK_see));
web.config文件如下:
   <?xml version="1.0"?>
<configuration>
 <configSections>
  <sectionGroup name="ajaxNet">
   <!--
    If you are using Microsoft .NET 1.1 please remove the two attributes
    requirePermission and restartOnExternalChanges, they are only supported
    with .NET 2.0.
   -->
   <section name="ajaxSettings" type="AjaxPro.AjaxSettingsSectionHandler,AjaxPro.2" requirePermission="false" restartOnExternalChanges="true"/>
  </sectionGroup>
 </configSections>
 <ajaxNet>
  <ajaxSettings>
   <urlNamespaceMappings useAssemblyQualifiedName="false" allowListOnly="false">
    <!--
     Set the attribute useAssemblyQualifiedName to true to enable
     use of assemblies placed in the GAC by using the full assembly
     qualified name.
    
     To hide internal knowledge of assemblies, classes and namespace
     you can override the name of the virtual http endpoints.
     
     <add type="Namespace.Class1,Assembly" path="mypath" />
    -->
   </urlNamespaceMappings>
   <jsonConverters>
    <!--
     This section can be used to add new IJavaScriptConverters to the
     Ajax.NET Professional engine. If you want to disable built-in
     converters you can use the remove tag.
   
     <remove type="Namespace.Class1,Assembly"/>
     <add type="Namespace.Class2,Assembly"/>
     
     <add type="AjaxPro.BitmapConverter,AjaxPro.2" mimeType="image/jpeg" quality="100"/>
    -->
   </jsonConverters>
   <!--
    Set the enabled attribute to true to get Stack, TargetSize and Source
    information if an exception has been thrown.
   -->
   <debug enabled="true"/>
   <!--
    This is the default configuration used with Ajax.NET Professional. You
    can put there your static JavaScript files, or remove the path attribute
    to completly disable the files.
   
    <scriptReplacements>
     <file name="prototype" path="~/ajaxpro/prototype.ashx" />
     <file name="core" path="~/ajaxpro/core.ashx" />
     <file name="converter" path="~/ajaxpro/converter.ashx" />
    </scriptReplacements>
   -->
   <!-- <encryption cryptType="" keyType="" /> -->
   <!--
    Set the enabled attribute to true to enable the use of an Ajax.NET Professional
    token. This will send a token to the client that will be used to identify if the
    requests comes from the same PC.
   -->
   <token enabled="false" sitePassword="password"/>
   <!--
    The oldStyle section can be used to enable old styled JavaScript code or
    functions that are not used any more.
   
    <oldStyle>
     <objectExtendPrototype/>
     <appCodeQualifiedFullName/>
     <allowNumberBooleanAsString/>
     <sessionStateDefaultNone/>
     <includeMsPrototype/>
     <renderDateTimeAsString/>
     <noUtcTime/>
    </oldStyle>
   -->
  </ajaxSettings>
 </ajaxNet>
 <!-- Common ASP.NET configuration -->
 <appSettings>
  <add key="JKConnStr" value="Data Source=211.68.79.52;Initial Catalog=JKXT;User ID=sa;password=jkxt2006;pooling=true;Connect Timeout=100"/>
  <add key="BMConnStr" value="Data Source=211.68.79.52;Initial Catalog=JKXT;User ID=sa;password=jkxt2006;pooling=true;Connect Timeout=100"/>
    <add key="GJConnStr" value="Data Source=211.68.79.52;Initial Catalog=JKXT;User ID=sa;password=jkxt2006;pooling=true;Connect Timeout=100"/>
    <add key="ZFConnStr" value="Data Source=211.68.79.52;Initial Catalog=JGXT;User ID=sa;password=jkxt2006;pooling=true;Connect Timeout=100"/>
  <add key="FTPStr" value="jkxt:jkxt2006@211.68.79.12"/>
  <add key="MAXSXTNumber" value="50"/>
 </appSettings>
 <connectionStrings>
  <add name="jkxtConnectionString" connectionString="Data Source=CHF;Initial Catalog=jkxt;Persist Security Info=True;User ID=sa;Password=jkxt2006"
   providerName="System.Data.SqlClient" />
 </connectionStrings>
 <system.web>
  <compilation debug="true"/>
  <authentication mode="Forms"/>
  <httpModules>
   <!--
   This HttpCompressionModule is only working for requests in "ajaxpro" folder. The module
   is available for ASP.NET 2.0.
   <add name="HttpCompressionModule" type="AjaxPro.HttpCompressionModule,AjaxPro.2"/>
   -->
  </httpModules>
 </system.web>
 <!-- Handler configuration for Ajax.NET Professional -->
 <location path="ajaxpro">
  <system.web>
   <httpHandlers>
    <add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
   </httpHandlers>
   <!--
    If you need to have Ajax.NET Professional methods running on the
    login page you may have to enable your own authorization configuration
    here.
   -->
   <!--
   <authorization>
    <deny users="?"/>
   </authorization>
   -->
  </system.web>
 </location>
 <!--
 If you are using the AjaxPro.BitmapConverter you have to use following location
 configuration to get a JPEG of the Bitmap.
 -->
 <!--
 <location path="ajaximage">
  <system.web>
   <httpHandlers>
    <add verb="*" path="*.ashx" type="AjaxPro.AjaxBitmapHttpHandler,AjaxPro.2"/>
   </httpHandlers>
  </system.web>
 </location>
 -->
</configuration>

第二部分:编写后台AjaxMethod()方法
        //获取节点
        [AjaxMethod()]
        public DataSet GetSubCategory(int iCategoryID)
        {
            DataSet ds = SQL.JK.RunProcedure("up_treebm");
            DataSet dss = ds.Clone();
            DataRow[] copyRows = ds.Tables[0].Select(string.Format("fatherid = '{0}'", iCategoryID));
            dss.Tables[0].Rows.Clear();
            ArrayList arrayYes = new ArrayList();
            ArrayList arrayNo = new ArrayList();
            if (copyRows.Length > 0)
            {
                foreach (DataRow copyRow in copyRows)
                {
                    if (copyRow["ip"].ToString() != "")
                    {
                        bool existresult = false;
                        foreach (object obj in arrayYes)
                        {
                            if (obj.ToString() == copyRow["ip"].ToString())
                            {
                                copyRow["ip"] = "1";
                                existresult = true;
                            }
                        }
                        foreach (object obj in arrayNo)
                        {
                            if (obj.ToString() == copyRow["ip"].ToString())
                            {
                                copyRow["ip"] = "0";
                                existresult = true;
                            }
                        }
                        if (existresult != true)
                        {
                            if (Testping(copyRow["ip"].ToString()))
                            {
                                arrayYes.Add(copyRow["ip"].ToString());
                                copyRow["ip"] = "1";
                            }
                            else
                            {
                                arrayNo.Add(copyRow["ip"].ToString());
                                copyRow["ip"] = "0";
                            }
                        }
                    }
                    dss.Tables[0].ImportRow(copyRow);
                }
                return dss;
            }
            else
                return null;
        }

        /// <summary>
        /// 测试是否关闭或者连通
        /// </summary>
        /// <param name="url">IP地址</param>
        /// <returns>结果</returns>
        public bool Testping(string IP)
        {
            bool br = false;
            string urlstr = @"http://" + IP + @"/";
            HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(urlstr);
            myWebRequest.Method = "Get";
            myWebRequest.Timeout = 1000;
            try
            {
                HttpWebResponse res = (HttpWebResponse)myWebRequest.GetResponse();
                if (res.StatusCode == HttpStatusCode.OK)
                {
                    br = true;
                }
            }
            catch
            {
                return false;
            }

            return br;
        }

        /// <summary>
        /// 生成表(单位)
        /// </summary>
        /// <param name="dwbm"></param>
        /// <param name="ssbm1"></param>
        /// <param name="ssbm2"></param>
        /// <param name="sxtlb">3:,2:或者其他</param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod()]
        public DataSet MakeTable(string dwbm)
        {
            DataSet ds = new DataSet();
            DataTable table1 = new DataTable("first");
            DataRow row1;
            table1.Columns.Add(new DataColumn("SXTMC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("TongDao", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("JSH", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("BM1MC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("BM2MC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("SXTLB", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("SXTLBMC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("SXTBH", System.Type.GetType("System.String")));

            table1.Columns.Add(new DataColumn("IP", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("Admin", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("Password", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("DWBM", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("DWMC", System.Type.GetType("System.String")));

            table1.Columns.Add(new DataColumn("PING", System.Type.GetType("System.String")));

            int lxjcount = 0;
            SqlDataReader lxjreader = Dal.JKXT01_LXJ.GetListbydw(dwbm);
            while (lxjreader.Read())
            {
                string ping;
                if (Dal.JKXT01_SXT.Existswhere("LXJBH='" + lxjreader["LXJBH"].ToString() + "' and SXTLB='1'"))
                {
                    if (Testping(lxjreader["IP"].ToString()))
                    {
                        ping = "1";
                    }
                    else
                    {
                        ping = "0";
                        lxjcount += 1;
                        if (lxjcount > 3)//有四个不通则返回!
                        {
                            lxjreader.Close();
                            ds.Tables.Clear();
                            ds.Tables.Add(table1);
                            return ds;
                        }
                    }
                    SqlDataReader sxtreader = Dal.JKXT01_SXT.GetListbylxj(lxjreader["LXJBH"].ToString());
                    while (sxtreader.Read())
                    {
                        if (sxtreader["SXTLB"].ToString() == "1")
                        {
                            row1 = table1.NewRow();
                            row1["SXTMC"] = sxtreader["SXTMC"].ToString();
                            row1["TongDao"] = sxtreader["TongDao"].ToString();
                            row1["JSH"] = sxtreader["JSH"].ToString();
                            row1["BM1MC"] = sxtreader["BM1MC"].ToString();
                            row1["BM2MC"] = sxtreader["BM2MC"].ToString();
                            row1["SXTLB"] = sxtreader["SXTLB"].ToString();
                            row1["SXTLBMC"] = sxtreader["SXTLBMC"].ToString();
                            row1["SXTBH"] = sxtreader["SXTBH"].ToString();

                            row1["IP"] = lxjreader["IP"].ToString();
                            row1["Admin"] = lxjreader["Admin"].ToString();
                            row1["Password"] = lxjreader["Password"].ToString();
                            row1["DWBM"] = lxjreader["DWBM"].ToString();
                            row1["DWMC"] = lxjreader["DWMC"].ToString();

                            row1["PING"] = ping;

                            table1.Rows.Add(row1);
                        }
                    }
                    sxtreader.Close();
                }
            }
            lxjreader.Close();
            ds.Tables.Clear();
            ds.Tables.Add(table1);
            return ds;
        }

        /// <summary>
        /// 生成表(部门1)
        /// </summary>
        /// <param name="dwbm"></param>
        /// <param name="ssbm1"></param>
        /// <param name="ssbm2"></param>
        /// <param name="sxtlb">3:2或者其他</param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod()]
        public DataSet MakeTable1(string dwbm,string ssbm1)
        {
            DataSet ds = new DataSet();
            DataTable table1 = new DataTable("first");
            DataRow row1;
            table1.Columns.Add(new DataColumn("SXTMC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("TongDao", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("JSH", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("BM1MC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("BM2MC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("SXTLB", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("SXTLBMC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("SXTBH", System.Type.GetType("System.String")));

            table1.Columns.Add(new DataColumn("IP", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("Admin", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("Password", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("DWBM", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("DWMC", System.Type.GetType("System.String")));

            table1.Columns.Add(new DataColumn("PING", System.Type.GetType("System.String")));

            SqlDataReader lxjreader = Dal.JKXT01_LXJ.GetListbydw(dwbm);
            while (lxjreader.Read())
            {
                string ping;
                if(Dal.JKXT01_SXT.Existswhere("LXJBH='"+lxjreader["LXJBH"].ToString()+"' and SSBM1='"+ssbm1+"' and SSBM2=''"))
                {
                    if (Testping(lxjreader["IP"].ToString()))
                    ping = "1";
                    else
                    ping = "0";

                    SqlDataReader sxtreader = Dal.JKXT01_SXT.GetListbylxj(lxjreader["LXJBH"].ToString());
                    while (sxtreader.Read())
                    {
                        if ((sxtreader["SSBM1"].ToString() == ssbm1) && (sxtreader["SSBM2"].ToString() == ""))
                        {
                            row1 = table1.NewRow();
                            row1["SXTMC"] = sxtreader["SXTMC"].ToString();
                            row1["TongDao"] = sxtreader["TongDao"].ToString();
                            row1["JSH"] = sxtreader["JSH"].ToString();
                            row1["BM1MC"] = sxtreader["BM1MC"].ToString();
                            row1["BM2MC"] = sxtreader["BM2MC"].ToString();
                            row1["SXTLB"] = sxtreader["SXTLB"].ToString();
                            row1["SXTLBMC"] = sxtreader["SXTLBMC"].ToString();
                            row1["SXTBH"] = sxtreader["SXTBH"].ToString();

                            row1["IP"] = lxjreader["IP"].ToString();
                            row1["Admin"] = lxjreader["Admin"].ToString();
                            row1["Password"] = lxjreader["Password"].ToString();
                            row1["DWBM"] = lxjreader["DWBM"].ToString();
                            row1["DWMC"] = lxjreader["DWMC"].ToString();

                            row1["PING"] = ping;

                            table1.Rows.Add(row1);
                        }
                    }
                    sxtreader.Close();
                }
            }
            lxjreader.Close();
            ds.Tables.Clear();
            ds.Tables.Add(table1);
            return ds;
        }

        /// <summary>
        /// 生成表(部门2)
        /// </summary>
        /// <param name="dwbm"></param>
        /// <param name="ssbm1"></param>
        /// <param name="ssbm2"></param>
        /// <param name="sxtlb">3:值班室,2:监舍或者其他</param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod()]
        public DataSet MakeTable2(string dwbm, string ssbm1, string ssbm2)
        {
            DataSet ds = new DataSet();
            DataTable table1 = new DataTable("first");
            DataRow row1;
            table1.Columns.Add(new DataColumn("SXTMC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("TongDao", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("JSH", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("BM1MC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("BM2MC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("SXTLB", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("SXTLBMC", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("SXTBH", System.Type.GetType("System.String")));

            table1.Columns.Add(new DataColumn("IP", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("Admin", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("Password", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("DWBM", System.Type.GetType("System.String")));
            table1.Columns.Add(new DataColumn("DWMC", System.Type.GetType("System.String")));

            table1.Columns.Add(new DataColumn("PING", System.Type.GetType("System.String")));

            SqlDataReader lxjreader = Dal.JKXT01_LXJ.GetListbydw(dwbm);
            while (lxjreader.Read())
            {
                string ping;
                if (Dal.JKXT01_SXT.Existswhere("LXJBH='" + lxjreader["LXJBH"].ToString() + "' and SSBM1='" + ssbm1 + "' and SSBM2='" + ssbm2 + "'"))
                {
                    if (Testping(lxjreader["IP"].ToString()))
                        ping = "1";
                    else
                        ping = "0";
                    SqlDataReader sxtreader = Dal.JKXT01_SXT.GetListbylxj(lxjreader["LXJBH"].ToString());
                    while (sxtreader.Read())
                    {
                        if ((sxtreader["SSBM1"].ToString() == ssbm1) && (sxtreader["SSBM2"].ToString() == ssbm2))
                        {
                            row1 = table1.NewRow();
                            row1["SXTMC"] = sxtreader["SXTMC"].ToString();
                            row1["TongDao"] = sxtreader["TongDao"].ToString();
                            row1["JSH"] = sxtreader["JSH"].ToString();
                            row1["BM1MC"] = sxtreader["BM1MC"].ToString();
                            row1["BM2MC"] = sxtreader["BM2MC"].ToString();
                            row1["SXTLB"] = sxtreader["SXTLB"].ToString();
                            row1["SXTLBMC"] = sxtreader["SXTLBMC"].ToString();
                            row1["SXTBH"] = sxtreader["SXTBH"].ToString();

                            row1["IP"] = lxjreader["IP"].ToString();
                            row1["Admin"] = lxjreader["Admin"].ToString();
                            row1["Password"] = lxjreader["Password"].ToString();
                            row1["DWBM"] = lxjreader["DWBM"].ToString();
                            row1["DWMC"] = lxjreader["DWMC"].ToString();

                            row1["PING"] = ping;

                            table1.Rows.Add(row1);
                        }
                    }
                    sxtreader.Close();
                }
            }
            lxjreader.Close();
            ds.Tables.Clear();
            ds.Tables.Add(table1);
            return ds;
        }
第三部分:编写javascript脚本
        //展开动作
        function ExpandSubCategory(iCategoryID)
        {
         var li_father = document.getElementById("li_" + iCategoryID);
         if (li_father.getElementsByTagName("li").length > 0) //分类已下载
         {
          ChangeStatus(iCategoryID);
          return;
         }
      
         li_father.className = "Opened";
      li_father = null;
         switchNote(iCategoryID, true);
         jkxt.JK.JK_see.GetSubCategory(iCategoryID, GetSubCategory_callback);//调用AjaxMethod    
        }
        //展开已下载的节点内容
        function ChangeStatus(iCategoryID)
     {
      var li_father = document.getElementById("li_" + iCategoryID);
      if (li_father.className == "Closed")
      {
       li_father.className = "Opened";
      }
      else
      {
       li_father.className = "Closed";
      }
      li_father = null;    
     }
        //获取到数据的处理函数
        function GetSubCategory_callback(response)
     {
         if(response.value != null)
         {
          var dt = response.value.Tables[0];
          if (dt.Rows.length > 0)
          {
           var iCategoryID = dt.Rows[0]["fatherid"];
          }
          else
          {
                 switchNote(iCategoryID, false);
                 return;
          }
          var li_father = document.getElementById("li_" + iCategoryID);
          var ul = document.createElement("ul");
          for (var i = 0;i < dt.Rows.length;i++)
          {
              if (dt.Rows[i]["isleap"] == 1) //叶子节点
           {
               var li = document.createElement("li");
            li.className = "Opened";
            li.id = "li_" + dt.Rows[i]["nodeid"];
            li.Name = dt.Rows[i]["isleap"];
            var treedeep = dt.Rows[i]["lb"];
            var img = document.createElement("img");
            img.id = dt.Rows[i]["nodeid"];
            img.className = "s";
            img.src = "../css/s.gif";
            img.onclick = function () {
             treeselect('"+dt.Rows[i]["lb"]+"','"+dt.Rows[i]["code"]+"','"+dt.Rows[i]["jybm"]+"','"+dt.Rows[i]["ssbm1"]+"','"+dt.Rows[i]["ssbm2"]+"');
            };
            img.alt = "查看";
            
            var a = document.createElement("a");
            a.href = "javascript:treeselect('"+dt.Rows[i]["lb"]+"','"+dt.Rows[i]["code"]+"','"+dt.Rows[i]["jybm"]+"','"+dt.Rows[i]["ssbm1"]+"','"+dt.Rows[i]["ssbm2"]+"');";
            a.innerHTML = dt.Rows[i]["title"];
            li.appendChild(img);
               li.appendChild(a);
               ul.appendChild(li); 
           }
           else
           {
            var li = document.createElement("li");
            li.className = "Closed";
            li.id = "li_" + dt.Rows[i]["nodeid"];
            li.Name = dt.Rows[i]["isleap"];
            var img = document.createElement("img");
            img.id = dt.Rows[i]["nodeid"];
            img.className = "s";
            img.src = "../css/s.gif";
            if(dt.Rows[i]["lb"] == "4")
            {
                img.onclick = function () {
                    ChangeStatusall();
                 ExpandSubCategory(this.id);
                };
            }
            else
            {
                img.onclick = function () {
                 ExpandSubCategory(this.id);
                };
            }
            img.alt = "展开/折叠";
            
            var a = document.createElement("a");
            if(dt.Rows[i]["lb"] == "0")
            a.href = "javascript:ExpandSubCategory(" + dt.Rows[i]["nodeid"] + ");";
            else if(dt.Rows[i]["lb"] == "4")
            a.href = "javascript:ChangeStatusall();ExpandSubCategory(" + dt.Rows[i]["nodeid"] + ");treeselect('"+dt.Rows[i]["lb"]+"','"+dt.Rows[i]["code"]+"','"+dt.Rows[i]["jybm"]+"','"+dt.Rows[i]["ssbm1"]+"','"+dt.Rows[i]["ssbm2"]+"');";
            else
            a.href = "javascript:ExpandSubCategory(" + dt.Rows[i]["nodeid"] + ");treeselect('"+dt.Rows[i]["lb"]+"','"+dt.Rows[i]["code"]+"','"+dt.Rows[i]["jybm"]+"','"+dt.Rows[i]["ssbm1"]+"','"+dt.Rows[i]["ssbm2"]+"');";
            a.innerHTML = dt.Rows[i]["title"];
            li.appendChild(img);
               li.appendChild(a);
               ul.appendChild(li); 
           }
           if(dt.Rows[i]["lb"] == "4")
                     dwarraylist.push(dt.Rows[i]["nodeid"]);
          }
             li_father.appendChild(ul);
             switchNote(iCategoryID, false);
             dt = null;
          ul = null;
          li_father = null;
      }
      else
      {
          //alert('对不起,单位部门菜单数据获取失败!\n请重新刷新页面!');
          return;
      }
     }
     //改变展开收缩状态
        function switchNote(iCategoryID, show)
     {
      var li_father = document.getElementById("li_" + iCategoryID);
      if (show)
      {
       var ul = document.createElement("ul");
       ul.id = "ul_note_" + iCategoryID;
       
       var note = document.createElement("li");
       note.className = "Child";
       
       var img = document.createElement("img");
       img.className = "s";
       img.src = "../css/s.gif";
       
       var a = document.createElement("a");
       a.href = "javascript:void(0);";
       a.innerHTML = "请稍候...";
       
       note.appendChild(img);
       note.appendChild(a);
       ul.appendChild(note);
       li_father.appendChild(ul);
      }
      else
      {
       var ul = document.getElementById("ul_note_" + iCategoryID);
       if (ul)
       {
        li_father.removeChild(ul);
       }    
      }
     }
     //展开 某个 单位
     function ChangeStatusall()
     {
            if(dwarraylist.length > 0)
            for(var i=0; i < dwarraylist.length; i++)
            {
                var li_father = document.getElementById("li_" + dwarraylist[i]);
          if (li_father)
          {
              switchNote(dwarraylist[i], false);
           if(li_father.Name == 0)
           li_father.className = "Closed";
          }
          li_father = null;  
            }
     }
     //清空树
     function cleartree()
     {
         var tree = document.getElementById("CategoryTree");
         var root = document.getElementById("li_0");
         tree.removeChild(root);
     }
     //初始化树
     function inittree()
     {
         // 加载根节点
         var tree = document.getElementById("CategoryTree");
         var root = document.createElement("li");
         root.id = "li_0";
         tree.appendChild(root);
         // 加载页面时显示第一级分类
         ExpandSubCategory(0);
     }
第四部分:编写存储过程
--exec up_tree
CREATE   procedure up_tree
as
begin
 create table #tree
 ( nodeid int identity, --
  title varchar(200) not null default '',--名称
  fatherid int not null default 0, --父节点
  isleap varchar(1) not null default '0',--1:叶子节点,0:非叶子节点
  code varchar(50) not null default '', --编码,摄像头编号,或所属单位编号,或监狱编号
  ip varchar(50) not null default '',
  jybm varchar(6) not null default '',--监狱编码
  ssbm1 varchar(30) not null default '',
  ssbm2 varchar(30) not null default '',
  sxtlb int not null default 0, --摄像头类别
  tongdao int not null default 0,
  lb varchar(1) not null default ''
 )
 declare @nodeid int
 declare @leapcount int
 -- 所有摄像头,一定是叶子.lxjbh可以区分单位
 insert into #tree (title,isleap,code,ip,jybm,ssbm1,ssbm2,sxtlb,tongdao,lb)
  select a.SXTMC,'1',sxtbh,b.ip,dwbm,ssbm1,ssbm2,sxtlb,tongdao,'1' from jkxt01_sxt a join jkxt01_lxj b on(a.lxjbh = b.lxjbh)  order by cast(right(ip,2) as varchar)+cast(tongdao as varchar)
 --所有分区
 insert into #tree(ssbm1,code,jybm,lb)
  select distinct ssbm1,ssbm2,dwbm,'2' from jkxt01_sxt a join jkxt01_lxj b on(a.lxjbh = b.lxjbh) where ssbm2 <> ''
 update a set a.fatherid = b.nodeid from #tree a join #tree b on(a.jybm = b.jybm and a.ssbm2 = b.code and a.ssbm1 = b.ssbm1)
  where a.lb = '1' and b.lb = '2'
 update #tree set title = mc from sys_bmk where code = bm and sys_bmk.lb = '9' and #tree.lb = '2'
 --区.可能没有分区,若没有分区,则fatherid=0
 insert into #tree(code,jybm,lb)
  select distinct ssbm1,dwbm,'3' from jkxt01_sxt a join jkxt01_lxj b on(a.lxjbh = b.lxjbh) where ssbm1 <> ''
 update a set a.fatherid = b.nodeid from #tree a join #tree b on(a.jybm = b.jybm and a.ssbm1 = b.code)
  where a.lb <= '2' and a.fatherid =0  and b.lb = '3'
 update #tree set title = mc from sys_bmk where code = bm and sys_bmk.lb = '10' and #tree.lb = '3'
 --单位
 insert into #tree (code,lb)
  select distinct dwbm,'4' from jkxt01_lxj
 update a set a.fatherid = b.nodeid from #tree a join #tree b on(a.jybm = b.code)
  where a.lb <='3' and a.fatherid = 0 and b.lb = '4'
 
 update #tree set title = mc from sys_bmk where code = bm and sys_bmk.lb = '11' and #tree.lb = '4'
 
 insert into #tree(code,title) values ('0','单位')
 update #tree set fatherid = @@identity where (len(code) = 4 and code like '110%') or (len(code) = 4 and code like '111%') or (len(code) = 4 and code like '112%')  or (len(code) = 4 and  code like '115%' )
 insert into #tree(code,title) values ('1','分局')
 update #tree set fatherid = @@identity where code like '114%' and len(code) = 4
--查找一遍,所有非叶节点是否真是.可能是多余的.数据不完整时会出现。
 declare cur_leap cursor for select nodeid from #tree where isleap = '0'
 open cur_leap
 fetch cur_leap into @nodeid
 while @@fetch_status = 0
 begin
  select @leapcount = count(*) from #tree where fatherid = @nodeid
  if @leapcount = 0
  begin
   update #tree set isleap = '1' where nodeid = @nodeid
   --select @nodeid
  end
  fetch cur_leap into @nodeid
 end
 close cur_leap
 deallocate cur_leap
 select * from #tree order by lb asc
 drop table #tree
end
GO

总结:
应用了不少东西,呵呵,懒得去整理,不好意思啊!

原文地址:https://www.cnblogs.com/chf/p/590894.html