package main import ( "2112aGorm/models" "fmt" "gorm.io/driver/mysql" "gorm.io/gorm" "html/template" "io" "math" "net/http" "os" "strconv" ) var db *gorm.DB func init() { dsn := "root:root@tcp(127.0.0.1:8889)/2112a" db, _ = gorm.Open(mysql.Open(dsn), &gorm.Config{}) } // 添加 func GoodsAdd(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { // to do 商品添加 name := r.FormValue("name") price, _ := strconv.ParseFloat(r.FormValue("price"), 64) number, _ := strconv.Atoi(r.FormValue("number")) cate := r.FormValue("cate") //接收文件 file, header, err := r.FormFile("img") if err != nil { fmt.Println("文件获取失败") return } defer file.Close() //定义文件 path := "static/img/" + header.Filename //创建文件 newFile, err := os.OpenFile(path, os.O_RDONLY|os.O_WRONLY|os.O_CREATE, 0777) //读4 写2 执行1 if err != nil { fmt.Println("文件创建失败") return } // 克拷文件 _, err = io.Copy(newFile, file) if err != nil { fmt.Println("文件上传失败") return } goods := models.Goods{ Name: name, Price: price, Number: number, Cate: cate, Img: path, } err = db.Table("goods").Create(&goods).Error if err != nil { fmt.Println("添加失败") return } //页面跳转 http.Redirect(w, r, "/goods/list", 302) } if r.Method == "GET" { // 显示表单 temp, err := template.ParseFiles("./views/form.html") if err != nil { fmt.Println("模版解析失败") return } temp.Execute(w, nil) } } // 列表 func GoodsList(w http.ResponseWriter, r *http.Request) { keyword := r.FormValue("keyword") cate := r.FormValue("cate") priceMin, _ := strconv.ParseFloat(r.FormValue("min_price"), 64) priceMax, _ := strconv.ParseFloat(r.FormValue("max_price"), 64) // 构建查询条件表达式 query := db.Table("goods") if keyword != "" { query = query.Where("name LIKE ?", "%"+keyword+"%") } if cate != "" { query = query.Where("cate = ?", cate) } if priceMin > 0 && priceMax > 0 { query = query.Where("price BETWEEN ? AND ?", priceMin, priceMax) } //1.总数 var count int64 query.Count(&count) //2.每页显示条数 size := 5 //3.总页数 sum := int(math.Ceil(float64(count) / float64(size))) //4.当前页 now, _ := strconv.Atoi(r.URL.Query().Get("p")) if now == 0 { now = 1 } //5.偏移量 offset := (now - 1) * size var goods []models.Goods query.Table("goods").Limit(size).Offset(offset).Find(&goods) //上一页 up := now - 1 if up < 1 { up = 1 } // 下一页 down := now + 1 if down > sum { down = sum } // 数字页码 var pages []int for i := 1; i <= sum; i++ { pages = append(pages, i) } // 解析模版 temp, err := template.ParseFiles("./views/list.html") if err != nil { fmt.Println("列表解析失败") return } // 定义map list := make(map[string]interface{}) list["goods"] = goods list["sum"] = sum list["up"] = up list["down"] = down list["pages"] = pages list["now"] = now list["keyword"] = keyword list["cate"] = cate list["max_price"] = priceMax list["min_price"] = priceMin //渲染 temp.Execute(w, list) } func main() { http.HandleFunc("/goods/add", GoodsAdd) http.HandleFunc("/goods/list", GoodsList) // 创建静态文件处理器 staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))) // 将处理器注册到路由中 http.Handle("/static/", staticHandler) http.ListenAndServe("localhost:8080", nil) }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!DOCTYPE html>
<html>
<head>
<title>表格分页样式</title>
<link rel="stylesheet" type="text/css" href="/static/css/list.css">
</head>
<body>
<div class="container">
<form>
<input type="search" name="keyword" value="{{.keyword}}">
<select name="cate">
<option value="">请选择分类</option>
<option value="数码">数码</option>
<option value="服装">服装</option>
<option value="儿童">儿童</option>
</select>
<input type="text" name="min_price" style="width: 30px">-<input type="text" name="max_price" style="width: 30px">
<button type="submit">搜索</button>
</form>
<table id="myTable">
<thead>
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>库存</th>
<th>分类</th>
<th>图片</th>
</tr>
</thead>
<tbody>
{{range .goods}}
<tr>
<td>{{.Id}}</td>
<td>{{.Name}}</td>
<td>¥{{.Price}}</td>
<td>{{.Number}}</td>
<td>{{.Cate}}</td>
<td><img src="/{{.Img}}" width="120px"></td>
</tr>
{{end}}
<!-- 其他商品行 -->
</tbody>
</table>
<div class="pagination">
<a href="?p={{.up}}&keyword={{.keyword}}&cate={{.cate}}&min_price={{.min_price}}&max_price={{.max_price}}">« 上一页</a>
{{range .pages}}
{{if eq $.now .}}
<a href="?p={{.}}&keyword={{$.keyword}}&cate={{$.cate}}&min_price={{$.min_price}}&max_price={{$.max_price}}" class="active">{{.}}</a>
{{else }}
<a href="?p={{.}}&keyword={{$.keyword}}&cate={{$.cate}}&min_price={{$.min_price}}&max_price={{$.max_price}}">{{.}}</a>
{{end}}
{{end}}
<a href="?p={{.down}}&keyword={{.keyword}}&cate={{.cate}}&min_price={{.min_price}}&max_price={{.max_price}}">下一页 »</a>
</div>
</div>
</body>
</html>
</body>
</html>
标签:goods,分页,err,列表,FormValue,Template,query,now,cate From: https://www.cnblogs.com/superzwb/p/17921937.html