[ADD]文章带搜索功能
This commit is contained in:
parent
85d0b5115f
commit
de5b6e207c
|
@ -16,14 +16,15 @@ func (c *IndexController) Get() {
|
||||||
page = 1
|
page = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
notes, err := models.QueryNoteByPage(page, limit)
|
title := c.GetString("title")
|
||||||
|
notes, err := models.QueryNoteByPage(title, page, limit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Abort500(err)
|
c.Abort500(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["notes"] = notes
|
c.Data["notes"] = notes
|
||||||
// 得到文章的总数
|
// 得到文章的总数
|
||||||
count, err := models.QueryNoteCount()
|
count, err := models.QueryNoteCount(title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Abort500(err)
|
c.Abort500(err)
|
||||||
}
|
}
|
||||||
|
@ -35,6 +36,7 @@ func (c *IndexController) Get() {
|
||||||
|
|
||||||
c.Data["totalPage"] = totalPage
|
c.Data["totalPage"] = totalPage
|
||||||
c.Data["page"] = page
|
c.Data["page"] = page
|
||||||
|
c.Data["title"] = title
|
||||||
|
|
||||||
c.TplName = "index.html"
|
c.TplName = "index.html"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import "github.com/jinzhu/gorm"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
type Note struct {
|
type Note struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
|
@ -19,13 +22,13 @@ func QueryNoteByKeyAndUserId(key string, userId int) (note Note, err error) {
|
||||||
return note, err
|
return note, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func QueryNoteByPage(page, limit int) (notes []*Note, err error) {
|
func QueryNoteByPage(title string, page int, limit int) (notes []*Note, err error) {
|
||||||
err = db.Offset((page - 1) * limit).Limit(limit).Find(¬es).Error
|
err = db.Where("title like ?", fmt.Sprintf("%%%s%%", title)).Offset((page - 1) * limit).Limit(limit).Find(¬es).Error
|
||||||
return notes, err
|
return notes, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func QueryNoteCount() (count int, err error) {
|
func QueryNoteCount(title string) (count int, err error) {
|
||||||
err = db.Model(&Note{}).Count(&count).Error
|
err = db.Model(&Note{}).Where("title like ?", fmt.Sprintf("%%%s%%", title)).Count(&count).Error
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,11 +31,11 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="item-btn">
|
<div class="item-btn">
|
||||||
{{ if gt .page 1 }}
|
{{ if gt .page 1 }}
|
||||||
<button class="layui-btn layui-btn-normal" onclick="window.location.href='/?page={{ add .page -1 }}'">上一页</button>
|
<button class="layui-btn layui-btn-normal" onclick="window.location.href='/?page={{ add .page -1 }}&title={{ .title }}'">上一页</button>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ if lt .page .totalPage }}
|
{{ if lt .page .totalPage }}
|
||||||
<button class="layui-btn layui-btn-normal" onclick="window.location.href='/?page={{ add .page 1}}'">下一页</button>
|
<button class="layui-btn layui-btn-normal" onclick="window.location.href='/?page={{ add .page 1}}&title={{ .title }}'">下一页</button>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -6,11 +6,11 @@
|
||||||
<img src="/static/images/logo-text.png" alt="" class="logo-text">
|
<img src="/static/images/logo-text.png" alt="" class="logo-text">
|
||||||
</a>
|
</a>
|
||||||
</h1>
|
</h1>
|
||||||
<form class="layui-form blog-seach pull-left" action="">
|
<form class="layui-form blog-seach pull-left" action="/">
|
||||||
<div class="layui-form-item blog-sewrap">
|
<div class="layui-form-item blog-sewrap">
|
||||||
<div class="layui-input-block blog-sebox">
|
<div class="layui-input-block blog-sebox">
|
||||||
<i class="layui-icon layui-icon-search"></i>
|
<i class="layui-icon layui-icon-search"></i>
|
||||||
<input type="text" name="title" lay-verify="title" autocomplete="off" class="layui-input">
|
<input type="text" name="title" lay-verify="title" value="{{ .title }}" autocomplete="off" class="layui-input">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
Loading…
Reference in New Issue