一.导航模块界面
导航列表
添加导航
修改导航
修改排序,状态
删除功能
前端展示界面
二.代码展示
模型
package models
//导航管理
type Nav struct {
Id int
Title string //标题
Link string //跳转地址
Position int //位置: 1 顶部, 2 中间, 3 底部, ...
IsOpennew int //是否新窗口打开: 1 否, 2 是
Relation string //关联商品,填写商品id,以逗号隔开,如: 25,26
Sort int //排序
Status int //状态
AddTime int //增加时间
GoodsItems []Goods `gorm:"-"` //忽略该字段,前端使用:获取导航相关商品数据
}
func (Nav) TableName() string {
return "nav"
}
控制器
package admin
import (
"fmt"
"goshop/models"
"math"
"net/http"
"github.com/gin-gonic/gin"
)
type NavController struct {
BaseController
}
func (con NavController) Index(c *gin.Context) {
//当前页
page, _ := models.Int(c.Query("page"))
if page == 0 {
page = 1
}
fmt.Println(page)
//每页显示的数量
pageSize := 8
//获取数据
navList := []models.Nav{}
models.DB.Offset((page - 1) * pageSize).Limit(pageSize).Find(&navList)
//获取总数量
var count int64
models.DB.Table("nav").Count(&count)
c.HTML(http.StatusOK, "admin/nav/index.html", gin.H{
"navList": navList,
//注意float64类型
"totalPages": math.Ceil(float64(count) / float64(pageSize)),
"page": page,
})
}
func (con NavController) Add(c *gin.Context) {
c.HTML(http.StatusOK, "admin/nav/add.html", gin.H{})
}
func (con NavController) DoAdd(c *gin.Context) {
title := c.PostForm("title")
link := c.PostForm("link")
position, _ := models.Int(c.PostForm("position"))
isOpennew, _ := models.Int(c.PostForm("is_opennew"))
relation := c.PostForm("relation")
sort, _ := models.Int(c.PostForm("sort"))
status, _ := models.Int(c.PostForm("status"))
if title == "" {
con.Error(c, "标题不能为空", "/admin/nav/add")
return
}
nav := models.Nav{
Title: title,
Link: link,
Position: position,
IsOpennew: isOpennew,
Relation: relation,
Sort: sort,
Status: status,
AddTime: int(models.GetUnix()),
}
err := models.DB.Create(&nav).Error
if err != nil {
con.Error(c, "增加导航失败 请重试", "/admin/nav/add")
} else {
con.Success(c, "增加导航成功", "/admin/nav")
}
}
func (con NavController) Edit(c *gin.Context) {
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/nav")
} else {
nav := models.Nav{Id: id}
models.DB.Find(&nav)
fmt.Println(nav)
c.HTML(http.StatusOK, "admin/nav/edit.html", gin.H{
"nav": nav,
})
}
}
func (con NavController) DoEdit(c *gin.Context) {
id, err1 := models.Int(c.PostForm("id"))
if err1 != nil {
con.Error(c, "传入数据错误", "/admin/nav")
return
}
title := c.PostForm("title")
link := c.PostForm("link")
position, _ := models.Int(c.PostForm("position"))
isOpennew, _ := models.Int(c.PostForm("is_opennew"))
relation := c.PostForm("relation")
sort, _ := models.Int(c.PostForm("sort"))
status, _ := models.Int(c.PostForm("status"))
if title == "" {
con.Error(c, "标题不能为空", "/admin/nav/add")
return
}
nav := models.Nav{Id: id}
models.DB.Find(&nav)
nav.Title = title
nav.Link = link
nav.Position = position
nav.IsOpennew = isOpennew
nav.Relation = relation
nav.Sort = sort
nav.Status = status
err2 := models.DB.Save(&nav).Error
if err2 != nil {
con.Error(c, "修改数据失败", "/admin/nav/edit?id="+models.String(id))
} else {
con.Success(c, "修改数据成功", "/admin/nav")
}
}
func (con NavController) Delete(c *gin.Context) {
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/nav")
} else {
nav := models.Nav{Id: id}
models.DB.Delete(&nav)
con.Success(c, "删除数据成功", "/admin/nav")
}
}
视图(html)
分页相关插件见: [golang gin框架] 20.Gin 商城项目-商品模块功能
状态修改,排序修改功能见: [golang gin框架] 15.Gin 商城项目-封装上传图片方法,轮播图的增删改查以及异步修改状态,数量
(1).index.html
{{ define "admin/nav/index.html" }}
{{ template "admin/public/page_header.html" .}}
导航名称 |
关联商品 |
跳转地址 |
导航位置 |
排序 |
状态 |
操作 |
{{range $key,$value := .navList}}
{{$value.Title}} |
{{$value.Relation}} |
{{$value.Link}} |
{{if eq $value.Position 1}}
顶部
{{else if eq $value.Position 2}}
中间
{{else if eq $value.Position 3}}
底部
{{end}}
|
{{$value.Sort}}
|
{{if eq $value.Status 1}}
{{else}}
{{end}}
|
修改
删除
|
{{end}}