go分页
简单的beego分页功能代码
一个简单的beego分页小插件(源代码在最下面):
支持条件查询
支持参数保留
支持自定义css样式
支持表/视图
支持参数自定义 默认为pno
支持定义生成链接的个数
使用方式:
1)action中,引入包,然后如下使用:

/**
* 日志列表
*/
func (this *LogController) List() {
pno, _ := this.GetInt("pno") //获取当前请求页
var tlog []m.Tb_log
var conditions string = " order by id desc" //定义日志查询条件,格式为 " and name='zhifeiya' and age=12 "
var po pager.PageOptions //定义一个分页对象
po.TableName = "tb_log" //指定分页的表名
po.EnableFirstLastLink = true //是否显示首页尾页 默认false
po.EnablePreNexLink = true //是否显示上一页下一页 默认为false
po.Conditions = conditions // 传递分页条件 默认全表
po.Currentpage = int(pno) //传递当前页数,默认为1
po.PageSize = 20 //页面大小 默认为20 //返回分页信息,
//第一个:为返回的当前页面数据集合,ResultSet类型
//第二个:生成的分页链接
//第三个:返回总记录数
//第四个:返回总页数
rs, pagerhtml, totalItem, _ := pager.GetPagerLinks(&po, this.Ctx)
rs.QueryRows(&tlog) //把当前页面的数据序列化进一个切片内
this.Data["list"] = tlog //把当前页面的数据传递到前台
this.Data["pagerhtml"] = pagerhtml
this.Data["totalItem"] = totalItem
this.Data["PageSize"] = po.PageSize
this.TplNames = "cms/log/list.html"
}

2)视图代码:
class="default" 是分页样式,可根据实际情况设置
<div class="default"><span>共{{.totalItem}}记录</span>
<div style="float:left;">{{.pagerhtml}}</div>
</div>
效果图片:

<div class="meneame"><span>共{{.totalItem}}记录</span>
<div style="float:left;">{{.pagerhtml}}</div>
</div>
效果图片:

分页源代码:

package pager /**
* 分页功能
* 支飞亚
* 2014-9-1
*/
import (
// "fmt"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/orm"
html "html/template"
con "strconv"
"strings"
"time"
) type PageOptions struct {
TableName string //表名 -----------------[必填]
Conditions string //条件
Currentpage int //当前页 ,默认1 每次分页,必须在前台设置新的页数,不设置始终默认1.在控制器中使用方式:cp, _ := this.GetInt("pno") po.Currentpage = int(cp)
PageSize int //页面大小,默认20
LinkItemCount int //生成A标签的个数 默认10个
Href string //A标签的链接地址 ---------[不需要设置]
ParamName string //参数名称 默认是pno
FirstPageText string //首页文字 默认"首页"
LastPageText string //尾页文字 默认"尾页"
PrePageText string //上一页文字 默认"上一页"
NextPageText string //下一页文字 默认"下一页"
EnableFirstLastLink bool //是否启用首尾连接 默认false 建议开启
EnablePreNexLink bool //是否启用上一页,下一页连接 默认false 建议开启
} /**
* 分页函数,适用任何表
* 返回 总记录条数,总页数,以及当前请求的数据RawSeter,调用中需要"rs.QueryRows(&tblog)"就行了 --tblog是一个Tb_log对象
* 参数:表名,当前页数,页面大小,条件(查询条件,格式为 " and name='zhifeiya' and age=12 ")
*/
func GetPagesInfo(tableName string, currentpage int, pagesize int, conditions string) (int, int, orm.RawSeter) {
if currentpage <= 1 {
currentpage = 1
}
if pagesize == 0 {
pagesize = 20
}
var rs orm.RawSeter
o := orm.NewOrm()
var totalItem, totalpages int = 0, 0 //总条数,总页数
o.Raw("SELECT count(*) FROM " + tableName + " where 1>0 " + conditions).QueryRow(&totalItem) //获取总条数
if totalItem <= pagesize {
totalpages = 1
} else if totalItem > pagesize {
temp := totalItem / pagesize
if (totalItem % pagesize) != 0 {
temp = temp + 1
}
totalpages = temp
}
rs = o.Raw("select * from " + tableName + " where id >0 " + conditions + " LIMIT " + con.Itoa((currentpage-1)*pagesize) + "," + con.Itoa(pagesize))
return totalItem, totalpages, rs
} /**
* 返回总记录条数,总页数,当前页面数据,分页html
* 根据分页选项,生成分页连接 下面是一个实例:
func (this *MainController) Test() {
var po util.PageOptions
po.EnablePreNexLink = true
po.EnableFirstLastLink = true
po.LinkItemCount = 7
po.TableName = "help_topic"
cp, _ := this.GetInt("pno")
po.Currentpage = int(cp)
_,_,_ pager := util.GetPagerLinks(&po, this.Ctx)
this.Data["Email"] = html.HTML(pager)
this.TplNames = "test.html"
}
*/
func GetPagerLinks(po *PageOptions, ctx *context.Context) (int, int, orm.RawSeter, html.HTML) {
var str string = ""
totalItem, totalpages, rs := GetPagesInfo(po.TableName, po.Currentpage, po.PageSize, po.Conditions)
po = setDefault(po, totalpages)
DealUri(po, ctx)
if totalpages <= po.LinkItemCount {
str = fun1(po, totalpages) //显示完全 12345678910
} else if totalpages > po.LinkItemCount {
if po.Currentpage < po.LinkItemCount {
str = fun2(po, totalpages) //123456789...200
} else {
if po.Currentpage+po.LinkItemCount < totalpages {
str = fun3(po, totalpages)
} else {
str = fun4(po, totalpages)
}
}
}
return totalItem, totalpages, rs, html.HTML(str)
} /**
* 处理url,目的是保存参数
*/
func DealUri(po *PageOptions, ctx *context.Context) {
uri := ctx.Request.RequestURI
var rs string
if strings.Contains(uri, "?") {
arr := strings.Split(uri, "?")
rs = arr[0] + "?" + po.ParamName + "time=" + con.Itoa(time.Now().Second())
arr2 := strings.Split(arr[1], "&")
for _, v := range arr2 {
if !strings.Contains(v, po.ParamName) {
rs += "&" + v
}
}
} else {
rs = uri + "?" + po.ParamName + "time=" + con.Itoa(time.Now().Second())
}
po.Href = rs
} /**
* 1...197 198 199 200
*/
func fun4(po *PageOptions, totalpages int) string {
var rs string = ""
rs += getHeader(po, totalpages)
rs += "<a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(1) + "'>" + con.Itoa(1) + "</a>"
rs += "<a href=''>...</a>"
for i := totalpages - po.LinkItemCount; i <= totalpages; i++ {
if po.Currentpage != i {
rs += "<a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(i) + "'>" + con.Itoa(i) + "</a>"
} else {
rs += "<span class=\"current\">" + con.Itoa(i) + "</span>"
}
}
rs += getFooter(po, totalpages)
return rs } /**
* 1...6 7 8 9 10 11 12 13 14 15... 200
*/
func fun3(po *PageOptions, totalpages int) string {
var rs string = ""
rs += getHeader(po, totalpages)
rs += "<a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(1) + "'>" + con.Itoa(1) + "</a>"
rs += "<a href=''>...</a>"
for i := po.Currentpage - po.LinkItemCount/2 + 1; i <= po.Currentpage+po.LinkItemCount/2-1; i++ {
if po.Currentpage != i {
rs += "<a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(i) + "'>" + con.Itoa(i) + "</a>"
} else {
rs += "<span class=\"current\">" + con.Itoa(i) + "</span>"
}
}
rs += "<a href=''>...</a>"
rs += "<a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(totalpages) + "'>" + con.Itoa(totalpages) + "</a>"
rs += getFooter(po, totalpages)
return rs } /**
* totalpages > po.LinkItemCount po.Currentpage < po.LinkItemCount
* 123456789...200
*/
func fun2(po *PageOptions, totalpages int) string {
var rs string = ""
rs += getHeader(po, totalpages)
for i := 1; i <= po.LinkItemCount+1; i++ {
if i == po.LinkItemCount {
rs += "<a href=\"" + po.Href + "&" + po.ParamName + "=" + con.Itoa(i) + "\">...</a>"
} else if i == po.LinkItemCount+1 {
rs += "<a href=\"" + po.Href + "&" + po.ParamName + "=" + con.Itoa(totalpages) + "\">" + con.Itoa(totalpages) + "</a>"
} else {
if po.Currentpage != i {
rs += "<a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(i) + "'>" + con.Itoa(i) + "</a>"
} else {
rs += "<span class=\"current\">" + con.Itoa(i) + "</span>"
}
}
}
rs += getFooter(po, totalpages)
return rs
} /**
* totalpages <= po.LinkItemCount
* 显示完全 12345678910
*/
func fun1(po *PageOptions, totalpages int) string { var rs string = ""
rs += getHeader(po, totalpages)
for i := 1; i <= totalpages; i++ {
if po.Currentpage != i {
rs += "<a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(i) + "'>" + con.Itoa(i) + "</a>"
} else {
rs += "<span class=\"current\">" + con.Itoa(i) + "</span>"
}
}
rs += getFooter(po, totalpages)
return rs
} /**
* 头部
*/
func getHeader(po *PageOptions, totalpages int) string {
var rs string = "<div>"
if po.EnableFirstLastLink { //当首页,尾页都设定的时候,就显示 rs += "<a " + judgeDisable(po, totalpages, 0) + " href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(1) + "'>" + po.FirstPageText + "</a>"
}
if po.EnablePreNexLink { // disabled=\"disabled\"
var a int = po.Currentpage - 1
if po.Currentpage == 1 {
a = 1
}
rs += "<a " + judgeDisable(po, totalpages, 0) + " href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(a) + "'>" + po.PrePageText + "</a>"
}
return rs
} /**
* 尾部
*/
func getFooter(po *PageOptions, totalpages int) string {
var rs string = ""
if po.EnablePreNexLink {
var a int = po.Currentpage + 1
if po.Currentpage == totalpages {
a = totalpages
}
rs += "<a " + judgeDisable(po, totalpages, 1) + " href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(a) + "'>" + po.NextPageText + "</a>"
}
if po.EnableFirstLastLink { //当首页,尾页都设定的时候,就显示
rs += "<a " + judgeDisable(po, totalpages, 1) + " href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(totalpages) + "'>" + po.LastPageText + "</a>"
}
rs += "</div>"
return rs
} /**
* 设置默认值
*/
func setDefault(po *PageOptions, totalpages int) *PageOptions {
if len(po.FirstPageText) <= 0 {
po.FirstPageText = "首页"
}
if len(po.LastPageText) <= 0 {
po.LastPageText = "尾页"
}
if len(po.PrePageText) <= 0 {
po.PrePageText = "上一页"
}
if len(po.NextPageText) <= 0 {
po.NextPageText = "下一页"
}
if po.Currentpage >= totalpages {
po.Currentpage = totalpages
}
if po.Currentpage <= 1 {
po.Currentpage = 1
}
if po.LinkItemCount == 0 {
po.LinkItemCount = 10
}
if po.PageSize == 0 {
po.PageSize = 20
}
if len(po.ParamName) <= 0 {
po.ParamName = "pno"
}
return po
} /**
*判断首页尾页 上一页下一页是否能用
*/
func judgeDisable(po *PageOptions, totalpages int, h_f int) string {
var rs string = ""
//判断头部
if h_f == 0 {
if po.Currentpage == 1 {
rs = "disabled=\"disabled\" style='pointer-events:none;'"
}
} else {
if po.Currentpage == totalpages {
rs = "disabled=\"disabled\" style='pointer-events:none;'"
}
}
return rs
}
go分页的更多相关文章
- 记一次SQLServer的分页优化兼谈谈使用Row_Number()分页存在的问题
最近有项目反应,在服务器CPU使用较高的时候,我们的事件查询页面非常的慢,查询几条记录竟然要4分钟甚至更长,而且在翻第二页的时候也是要这么多的时间,这肯定是不能接受的,也是让现场用SQLServerP ...
- js实现前端分页页码管理
用JS实现前端分页页码管理,可以很美观的区分页码显示(这也是参考大多数网站的分页页码展示),能够有很好的用户体验,这也是有业务需要就写了一下,还是新手,经验不足,欢迎指出批评! 首先先看效果图: 这是 ...
- JdbcTemplate+PageImpl实现多表分页查询
一.基础实体 @MappedSuperclass public abstract class AbsIdEntity implements Serializable { private static ...
- MVC如何使用开源分页插件shenniu.pager.js
最近比较忙,前期忙公司手机端接口项目,各种开发+调试+发布现在几乎上线无问题了:虽然公司项目忙不过在期间抽空做了两件个人觉得有意义的事情,一者使用aspnetcore开发了个人线上项目(要说线上其实只 ...
- NET Core-TagHelper实现分页标签
这里将要和大家分享的是学习总结使用TagHelper实现分页标签,之前分享过一篇使用HtmlHelper扩展了一个分页写法地址可以点击这里http://www.cnblogs.com/wangrudo ...
- 套用JQuery EasyUI列表显示数据、分页、查询
声明,本博客从csdn搬到cnblogs博客园了,以前的csdn不再更新,朋友们可以到这儿来找我的文章,更多的文章会发表,谢谢关注! 有时候闲的无聊,看到extjs那么肥大,真想把自己的项目改了,最近 ...
- php实现的分页类
php分页类文件: <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 pr ...
- C#关于分页显示
---<PS:本人菜鸟,大手子还请高台贵手> 以下是我今天在做分页时所遇到的一个分页显示问题,使用拼写SQL的方式写的,同类型可参考哦~ ------------------------- ...
- JAVA 分页工具类及其使用
Pager.java package pers.kangxu.datautils.common; import java.io.Serializable; import java.util.List; ...
- 分页插件--根据Bootstrap Paginator改写的js插件
刚刚出来实习,之前实习的公司有一个分页插件,和后端的数据字典约定好了的,基本上是看不到内部是怎么实现的,新公司是做WPF的,好像对于ASP.NET的东西不多,导师扔了一个小系统给我和另一个同事,指了两 ...
随机推荐
- 八大排序算法之五--交换排序—冒泡排序(Bubble Sort)
基本思想: 在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒.即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将 ...
- SDUT 2608:Alice and Bob
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Alice and Bob like playing ...
- 最简单的C/S程序——让服务器来做加法
还在写“Hello world!”式的单机程序吗?还在各种拖控件吗?是否自己都觉得有点low呢?来个质的飞跃吧!看看怎么让服务器帮咱做加法! 所谓C/S程序就是Client/Server程序,自然既包 ...
- phpcms常用函数
1../libs/functions/global.func.php --------------------------------------------------字符串安全处理函数--- ...
- Linux 线程与进程,以及通信
http://blog.chinaunix.net/uid-25324849-id-3110075.html 部分转自:http://blog.chinaunix.net/uid-20620288-i ...
- 转载-python学习笔记之常用模块用法分析
内置模块(不用import就可以直接使用) 常用内置函数 help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像函数一样调用 repr(ob ...
- Python网络爬虫Scrapy框架研究 以及 代理设置
地址:https://github.com/yidao620c/core-scrapy 例子:https://github.com/geekan/scrapy-examples 中文翻译文档: htt ...
- WebForm跨页面传值---内置对象
一.Response Response - 响应请求对象 string path = "Default2.aspx": (1)Response.Redirect(path); -- ...
- SqlSever基础 len函数 计算前后都有空格的字符串的长度时
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- 【转载】COM的多线程模型
原文:COM的多线程模型 COM的多线程模型是COM技术里头最难以理解的部分之一,很多书都有涉及但是都没有很好的讲清楚.很多新人都会在这里觉得很迷惑,google大神能搜到一篇vckbase上的文章, ...