纽威
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.

38 lines
1.2 KiB

3 years ago
  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace NFine.Code
  4. {
  5. public static class TreeSelect
  6. {
  7. public static string TreeSelectJson(this List<TreeSelectModel> data)
  8. {
  9. StringBuilder sb = new StringBuilder();
  10. sb.Append("[");
  11. sb.Append(TreeSelectJson(data, "0", ""));
  12. sb.Append("]");
  13. return sb.ToString();
  14. }
  15. private static string TreeSelectJson(List<TreeSelectModel> data, string parentId, string blank)
  16. {
  17. StringBuilder sb = new StringBuilder();
  18. var ChildNodeList = data.FindAll(t => t.parentId == parentId);
  19. var tabline = "";
  20. if (parentId != "0")
  21. {
  22. tabline = "  ";
  23. }
  24. if (ChildNodeList.Count > 0)
  25. {
  26. tabline = tabline + blank;
  27. }
  28. foreach (TreeSelectModel entity in ChildNodeList)
  29. {
  30. entity.text = tabline + entity.text;
  31. string strJson = entity.ToJson();
  32. sb.Append(strJson);
  33. sb.Append(TreeSelectJson(data, entity.id, tabline));
  34. }
  35. return sb.ToString().Replace("}{", "},{");
  36. }
  37. }
  38. }