[ADD]文章带搜索功能
This commit is contained in:
parent
85d0b5115f
commit
de5b6e207c
|
@ -16,14 +16,15 @@ func (c *IndexController) Get() {
|
|||
page = 1
|
||||
}
|
||||
|
||||
notes, err := models.QueryNoteByPage(page, limit)
|
||||
title := c.GetString("title")
|
||||
notes, err := models.QueryNoteByPage(title, page, limit)
|
||||
if err != nil {
|
||||
c.Abort500(err)
|
||||
}
|
||||
|
||||
c.Data["notes"] = notes
|
||||
// 得到文章的总数
|
||||
count, err := models.QueryNoteCount()
|
||||
count, err := models.QueryNoteCount(title)
|
||||
if err != nil {
|
||||
c.Abort500(err)
|
||||
}
|
||||
|
@ -35,6 +36,7 @@ func (c *IndexController) Get() {
|
|||
|
||||
c.Data["totalPage"] = totalPage
|
||||
c.Data["page"] = page
|
||||
c.Data["title"] = title
|
||||
|
||||
c.TplName = "index.html"
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package models
|
||||
|
||||
import "github.com/jinzhu/gorm"
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
type Note struct {
|
||||
gorm.Model
|
||||
|
@ -19,13 +22,13 @@ func QueryNoteByKeyAndUserId(key string, userId int) (note Note, err error) {
|
|||
return note, err
|
||||
}
|
||||
|
||||
func QueryNoteByPage(page, limit int) (notes []*Note, err error) {
|
||||
err = db.Offset((page - 1) * limit).Limit(limit).Find(¬es).Error
|
||||
func QueryNoteByPage(title string, page int, limit int) (notes []*Note, err error) {
|
||||
err = db.Where("title like ?", fmt.Sprintf("%%%s%%", title)).Offset((page - 1) * limit).Limit(limit).Find(¬es).Error
|
||||
return notes, err
|
||||
}
|
||||
|
||||
func QueryNoteCount() (count int, err error) {
|
||||
err = db.Model(&Note{}).Count(&count).Error
|
||||
func QueryNoteCount(title string) (count int, err error) {
|
||||
err = db.Model(&Note{}).Where("title like ?", fmt.Sprintf("%%%s%%", title)).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
|
|
|
@ -31,11 +31,11 @@
|
|||
</div>
|
||||
<div class="item-btn">
|
||||
{{ 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 }}
|
||||
|
||||
{{ 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 }}
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -6,11 +6,11 @@
|
|||
<img src="/static/images/logo-text.png" alt="" class="logo-text">
|
||||
</a>
|
||||
</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-input-block blog-sebox">
|
||||
<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>
|
||||
</form>
|
||||
|
|
Loading…
Reference in New Issue