基于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"`
}
相关推荐
我的golang之路果然有问题1 天前
云服务器部署Gin+gorm 项目 demo
运维·服务器·后端·学习·golang·gin
fashia4 天前
Java转Go日记(六十):gin其他常用知识
开发语言·后端·golang·go·gin
我的golang之路果然有问题4 天前
ElasticSearch+Gin+Gorm简单示例
大数据·开发语言·后端·elasticsearch·搜索引擎·golang·gin
fashia5 天前
Java转Go日记(五十七):gin 中间件
开发语言·后端·golang·go·gin
夏沫mds5 天前
不动产登记区块链系统(Vue3 + Go + Gin + Hyperledger Fabric)
linux·golang·区块链·gin·fabric
比特森林探险记5 天前
Go Gin框架深度解析:高性能Web开发实践
前端·golang·gin
在成都搬砖的鸭鸭5 天前
【Golang】使用gin框架导出excel和csv文件
golang·excel·gin
三金C_C6 天前
gin 框架
go·gin·后端框架
Chandler246 天前
Go 即时通讯系统:日志模块重构,并从main函数开始
后端·重构·golang·gin
三金C_C7 天前
gin 常见中间件配置
中间件·gin