纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
2.4 KiB

3 years ago
  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace NFine.Code
  4. {
  5. public static class TreeView
  6. {
  7. public static string TreeViewJson(this List<TreeViewModel> data, string parentId = "0")
  8. {
  9. StringBuilder strJson = new StringBuilder();
  10. List<TreeViewModel> item = data.FindAll(t => t.parentId == parentId);
  11. strJson.Append("[");
  12. if (item.Count > 0)
  13. {
  14. foreach (TreeViewModel entity in item)
  15. {
  16. strJson.Append("{");
  17. strJson.Append("\"id\":\"" + entity.id + "\",");
  18. strJson.Append("\"text\":\"" + entity.text.Replace("&nbsp;", "") + "\",");
  19. strJson.Append("\"value\":\"" + entity.value + "\",");
  20. if (entity.title != null && !string.IsNullOrEmpty(entity.title.Replace("&nbsp;", "")))
  21. {
  22. strJson.Append("\"title\":\"" + entity.title.Replace("&nbsp;", "") + "\",");
  23. }
  24. if (entity.img != null && !string.IsNullOrEmpty(entity.img.Replace("&nbsp;", "")))
  25. {
  26. strJson.Append("\"img\":\"" + entity.img.Replace("&nbsp;", "") + "\",");
  27. }
  28. if (entity.checkstate != null)
  29. {
  30. strJson.Append("\"checkstate\":" + entity.checkstate + ",");
  31. }
  32. if (entity.parentId != null)
  33. {
  34. strJson.Append("\"parentnodes\":\"" + entity.parentId + "\",");
  35. }
  36. strJson.Append("\"showcheck\":" + entity.showcheck.ToString().ToLower() + ",");
  37. strJson.Append("\"isexpand\":" + entity.isexpand.ToString().ToLower() + ",");
  38. if (entity.complete == true)
  39. {
  40. strJson.Append("\"complete\":" + entity.complete.ToString().ToLower() + ",");
  41. }
  42. strJson.Append("\"hasChildren\":" + entity.hasChildren.ToString().ToLower() + ",");
  43. strJson.Append("\"ChildNodes\":" + TreeViewJson(data, entity.id) + "");
  44. strJson.Append("},");
  45. }
  46. strJson = strJson.Remove(strJson.Length - 1, 1);
  47. }
  48. strJson.Append("]");
  49. return strJson.ToString();
  50. }
  51. }
  52. }