

Simple paging with Golang templates and GORM
source link: https://kenanbek.github.io/golang-template-gorm-paging-offset-limit
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Simple paging with Golang templates and GORM
Published: Jul 14, 2020 / Last modified: Jul 15, 2020
Go code:
const PageCount = 10
type Paging struct {
Page int
PrevPage int
NextPage int
HasPrev bool
HasNext bool
}
type HomePage struct {
Title string
Paging Paging
Articles []articles.Article
}
func (ws *Website) homePageHandler(w http.ResponseWriter, r *http.Request) {
var page int
var err error
pageStr := r.URL.Query().Get("page")
if pageStr != "" {
page, err = strconv.Atoi(pageStr)
if err != nil {
log.Println("failed to parse page param", err)
}
}
tmpl, err := template.ParseFiles("resources/fe/website/home.html")
if err != nil {
log.Println("failed to parse template", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
count, err := ws.DB.Articles().CountArticles()
if err != nil {
errStr := fmt.Sprintf("failed to get count of articles from storage: %v", err)
log.Println(errStr)
}
offset := page * PageCount
paging := Paging{}
if page == 0 {
paging.HasPrev = false
} else {
paging.HasPrev = true
}
if page >= count/PageCount {
paging.HasNext = false
} else {
paging.HasNext = true
}
paging.Page = page
paging.NextPage = page + 1
paging.PrevPage = page - 1
arts, err := ws.DB.Articles().ListArticles(offset, PageCount)
if err != nil {
errStr := fmt.Sprintf("failed to load articles from storage: %v", err)
log.Println(errStr)
}
hp := HomePage{
Title: "List of articles",
Articles: arts,
Paging: paging,
}
err = tmpl.Execute(w, hp)
if err != nil {
errStr := fmt.Sprintf("failed to render template: %v", err)
log.Println(errStr)
}
}
Methods ListArticles
and CountArticles
are implemented by using GORM:
func (rp *Articles) CountArticles() (int, error) {
var count int
if err := rp.db.Model(&Article{}).Order("posted DESC").Count(&count).Error; err != nil {
return 0, err
} else {
return count, nil
}
}
func (rp *Articles) ListArticles(offset, limit int) ([]Article, error) {
var articles []Article
if err := rp.db.Order("posted DESC").Offset(offset).Limit(limit).Find(&articles).Error; err != nil {
return nil, err
} else {
return articles, nil
}
}
HTML template:
<nav aria-label="...">
<ul class="pagination justify-content-center">
<li class="page-item">
<a class="page-link" href="?page=">Previous</a>
</li>
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-disabled="true">Previous</a>
</li>
<li class="page-item active" aria-current="page">
<a class="page-link" href="#"> <span class="sr-only">(current)</span></a>
</li>
<li class="page-item">
<a class="page-link" href="?page=">Next</a>
</li>
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-disabled="true">Next</a>
</li>
</ul>
</nav>
[ Tags ]
Recommend
-
234
jinzhu/gorm: GORM V1, V2 moved to https://github.com/go-gorm/gorm...
-
45
golang 官方的那个操作mysql的有点麻烦所以就使用了gorm,下面就gorm的使用做下简单介绍 下载gorm: go get -u github.com/jinzhu/gorm 在项目中引入gorm: import ( "github.com/jinzhu/...
-
41
前言 对于golang中使用gorm操作mysql,可能其他的操作都是固定的,唯一麻烦的就是字段的映射。 gorm允许自己定义一个表的结构映射,但是,golang中,首字母大写来表示public或者private,而gorm在做映射的时候,大写...
-
29
gorm是go语言的一个orm框架,框架的原理和思想在这里就不做详细介绍了,我主要演示一下gorm的实际使用。 开启mysql连接 主要用到 gorm.open()这个方法 //参数含义:数据库用户名、密码、主...
-
8
The fantastic ORM library for Golang, aims to be developer friendly. Overview Full-Featured ORM Associations (Has One, Has Many, Belongs To, Many To Many, Polymorphism, Single-table inheritance) Hooks (B...
-
7
golang gorm mysql 读写时间戳 eflight · 2天之前 · 100 次点击 · 预计阅读时间 2 分钟 · 大...
-
19
Suppose we have a paging system with 4 frames and 12 pages, where the number of frames denotes the number of pages that can be held in RAM at any given time. Assume the pages are accessed by some program in the order shown below...
-
5
<?xml encoding="utf-8" ??>Introduction This guide explains how to build an example web API in Go to create, update, delete book records from a database. At the end of this article, you should be...
-
9
V2EX › 程序员 Golang gorm 怎么跨库查询 MySQL? gejigeji · 1 天前 · 1096 次...
-
7
golang gorm框架的sql注入漏洞 keluda · 2020-06-27 18:32:50 · 6153 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK