基于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"`
}
相关推荐
用户9230777112247 天前
Gin教程 Golang+Gin框架入门实战教程 大地老师
gin
驰羽11 天前
[GO]gin框架:ShouldBindJSON与其他常见绑定方法
开发语言·golang·gin
千码君201616 天前
Go语言:记录一下Go语言系统学习的第一天
java·开发语言·学习·golang·gin·并发编程·编译语言
会跑的葫芦怪1 个月前
Gin 框架令牌桶限流实战指南
驱动开发·gin
Stars20241 个月前
【gin框架读取参数的方式】
iphone·xcode·gin
会跑的葫芦怪1 个月前
Go tool pprof 与 Gin 框架性能分析完整指南
开发语言·golang·gin
n8n1 个月前
Gin 框架令牌桶限流实战指南
go·gin
n8n1 个月前
Go tool pprof 与 Gin 框架性能分析完整指南
go·gin
kite01211 个月前
Gin + JWT 认证机制详解:构建安全的Go Web应用
golang·gin·jwt
T0uken1 个月前
【Golang】Gin:静态服务与模板
开发语言·golang·gin