unlimited_tree.go 829 B

1234567891011121314151617181920212223242526272829
  1. package services
  2. import "hongze/hz_crm_api/models/cygx"
  3. func MakeTree(allNode []*cygx.CygxIndustryMapItems, node *cygx.CygxIndustryMapItems) {
  4. childs, _ := haveChild(allNode, node) //判断节点是否有子节点并返回
  5. if len(childs) > 0 {
  6. node.Children = append(node.Children, childs[0:]...) //添加子节点
  7. for _, v := range childs { //查询子节点的子节点,并添加到子节点
  8. _, has := haveChild(allNode, v)
  9. if has {
  10. MakeTree(allNode, v) //递归添加节点
  11. }
  12. }
  13. }
  14. }
  15. func haveChild(allNode []*cygx.CygxIndustryMapItems, node *cygx.CygxIndustryMapItems) (childs []*cygx.CygxIndustryMapItems, yes bool) {
  16. for _, v := range allNode {
  17. if v.ParentId == node.IndustryMapId {
  18. childs = append(childs, v)
  19. }
  20. }
  21. if len(childs) > 0 {
  22. yes = true
  23. }
  24. return
  25. }