基于gin关于多级菜单的处理

多级菜单是很多业务场景需要的。下面是一种处理方式

go 复制代码
// 生成树结构
func tree(menus []*video.XkVideoCategory, parentId uint) []*video.XkVideoCategory {
	//定义子节点目录
	var nodes []*video.XkVideoCategory
	if reflect.ValueOf(menus).IsValid() {
		//循环所有一级菜单
		for _, v := range menus {
			//查询所有该菜单下的所有子菜单
			if v.ParentId == parentId {
				//特别注意压入元素不是单个所有加三个 **...** 告诉切片无论多少元素一并压入
				v.Children = append(v.Children, tree(menus, v.ID)...)
				nodes = append(nodes, v)
			}

		}
	}
	return nodes
}

下面是model

sql 复制代码
type XkVideoCategory struct {
	ID           uint      `gorm:"primarykey;comment:主键ID" json:"id" form:"id"`
	CategoryName string    `json:"categoryName" gorm:"not null;default:'';comment:分类名称"`
	Description  string    `json:"description" gorm:"not null;default:'';comment:分类描述"`
	CreateTime   time.Time `gorm:"type:datetime(0);comment:创建时间" json:"createTime"`
	UpdatedTime  time.Time `gorm:"type:datetime(0);comment:更新时间" json:"updatedTime"`
	ParentId     uint      `json:"parentId" gorm:"not null;default:0;comment:分类的主ID"`
	Status       int8      `json:"status" gorm:"not null;default:1;comment:0 未发布 1 发布"`
	Sorted       int8      `json:"sorted" gorm:"not null;default:1;comment:0 排序"`
	IsDelete     int8      `json:"isDelete" gorm:"not null;default:0;comment:0 未删除 1 删除"`
	// 忽略该字段,- 表示无读写,-:migration 表示无迁移权限,-:all 表示无读写迁移权限
	Children []*XkVideoCategory `gorm:"-" json:"children"`
}
相关推荐
Jerry Lau1 天前
go go go 出发咯 - go web开发入门系列(二) Gin 框架实战指南
前端·golang·gin
Code季风2 天前
微服务分布式配置中心:Gin Web 服务层与 gRPC 服务层集成 Nacos 实战
分布式·微服务·rpc·架构·go·gin·consul
Code季风3 天前
Gin Web 层集成 Viper 配置文件和 Zap 日志文件指南(下)
前端·微服务·架构·go·gin
Code季风3 天前
Gin Web 服务集成 Consul:从服务注册到服务发现实践指南(下)
java·前端·微服务·架构·go·gin·consul
ac.char4 天前
Golang读取ZIP压缩包并显示Gin静态html网站
golang·html·gin
zhuyasen5 天前
定义即代码!这个框架解决了90%的Go开发者还在低效开发项目的问题
架构·go·gin
程序员爱钓鱼5 天前
Go语言项目工程化 — 常见开发工具与 CI/CD 支持
开发语言·后端·golang·gin
Code季风6 天前
深入比较 Gin 与 Beego:Go Web 框架的两大选择
开发语言·golang·go·gin·beego
Code季风6 天前
Gin 中间件详解与实践
学习·中间件·golang·go·gin
小诸葛的博客7 天前
gin如何返回html
前端·html·gin