Jquery EasyUI tree asynchronous loading (traverse the specified folder according to the folder files generated tree)

  • 2021-07-18 07:01:05
  • OfStack

The asynchronous loading of Jquery EasyUI tree (traversing the specified folder and generating tree according to the files in the folder) is as follows:


private void SMT(HttpContext context)
 {
  string SqlConnection82 = System.Configuration.ConfigurationManager.AppSettings["LocalConnectionString"];
  string path = context.Server.MapPath(@"~/CISWeb/SMT_SOP");
  string id = string.Empty;
  List<string> filesNameList = getFiles(path);
  if (filesNameList.Count > 0)
   context.Response.Write(ListToJson(filesNameList,id));
  else
  {
   context.Response.Write("0");
  }  
 }
 /// <summary>
 ///  Object in the directory specified by the scope   Folder / Documents   Quantity 
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 private static List<string> getFiles(string path)
 {
  DirectoryInfo SMT_SOP = new DirectoryInfo(path);
  List<string> allFileList = new List<string>();
  DirectoryInfo[] allDir = SMT_SOP.GetDirectories();// Get the number of folders 
  foreach (DirectoryInfo d in allDir)
  {
   allFileList.Add("0|" + d.Name);
  }
  FileInfo[] allFile = SMT_SOP.GetFiles();// Get the number of files 
  foreach (FileInfo fi in allFile)
  {
   allFileList.Add("1|" + fi.Name);
  }
  return allFileList;
 }
 /// <summary>
 ///  Traverses the specified folder and returns according to the files in the folder JSON
 /// </summary>
 /// <param name="filesNameList"></param>
 /// <param name="id"> No. 1 1 The next time this method is called, id=""</param>
 /// <returns></returns>
 public string ListToJson(List<string> filesNameList,string id)
 { 
  StringBuilder sb2 = new StringBuilder();
  sb2.Append("[");
  for (int i = 0; i < filesNameList.Count; i++)
  {  
   if (filesNameList[i].ToString().Split('|')[0] == "0")
   { 
    sb2.Append("{ \"id\":" +id+ (i + 1).ToString() + ",\"text\":\"" + filesNameList[i].ToString().Split('|')[1] + "\",\"state\":\"closed\",\"children\": [");    
    sb2.Append("]},");    
   }
   else
   {    
    sb2.Append("{\"id\":"+id + (i + 1).ToString() + ",\"text\":\"" + String.Format(filesNameList[i].ToString().Split('|')[1]) + "\"},");    
   }

  }
  sb2.Remove(sb2.Length - 1, 1);
  sb2.Append("]");  
  return sb2.ToString();
 }

Front desk


$.ajax({
   type: "post",
   url: "../../ajax/Handler.ashx?action=SMT",
   data: {},
   success: function (result) {
    $("#menuDiv").dialog("open");//jQuery UI Modal window in 
    var treeData = eval(result);
    $("#tt").tree({
     data: treeData})
    }
   })

Related articles: