zoukankan      html  css  js  c++  java
  • go中的无限极分类的问题

    最近在开发的过程中遇到一个无限极分类的问题,这种问题在php中写过很多,想着很简单,3-5分钟就能解决,结果竟然写了半小时,既然这样,就把这个给总结下。

    思路

    1、循环数据,放到hash中,把标识的数据作为key。

    2、处理数据,循环数据,通过hash的查找拼接数据

    3、go中的难点在于定义数据的格式

    type BusinessRelationOther struct {
        TkBusinessRelation `xorm:"extends"`
        ClassName          string                  `json:"class_name" ` // class的name
        DateName           string                  `json:"date_name" `  // 第三方的name
        List               []BusinessRelationOther `json:"list"`
    }

    展示下代码

    // buildData 数据的资源组装
    func (myL *BusinessRelationLogic) buildData(list []models.BusinessRelationOther) map[int]map[int]models.BusinessRelationOther {
        var data map[int]map[int]models.BusinessRelationOther = make(map[int]map[int]models.BusinessRelationOther)
        for _, v := range list {
            id := v.Id
            fid := v.ParentId
            if _, ok := data[fid]; !ok {
                data[fid] = make(map[int]models.BusinessRelationOther)
            }
            data[fid][int(id)] = v
        }
        return data
    }
    
    // makeTreeCore 图形化
    func (myL *BusinessRelationLogic) makeTreeCore(index int, data map[int]map[int]models.BusinessRelationOther) []models.BusinessRelationOther {
        tmp := make([]models.BusinessRelationOther, 0)
        for id, item := range data[index] {
            if data[id] != nil {
                item.List = myL.makeTreeCore(id, data)
            }
            tmp = append(tmp, item)
        }
        return tmp
    }

    data := myL.buildData(sBusinessRelationFac.BusinessRelationOtherSlicePtr)
    result := myL.makeTreeCore(0, data)
     
  • 相关阅读:
    取得当前目录下所有文件名
    windows用户态和内核态
    MFC CDialog中控件跨线程访问失败
    Effective C++学习笔记(八)
    Effective C++学习笔记(六)
    Effective C++学习笔记(七)
    Effective C++学习笔记(五)
    Effective C++学习笔记(四)
    Effective C++学习笔记(三)
    Effective C++学习笔记(二)
  • 原文地址:https://www.cnblogs.com/ricklz/p/10197172.html
Copyright © 2011-2022 走看看