简单的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分页的更多相关文章

  1. 记一次SQLServer的分页优化兼谈谈使用Row_Number()分页存在的问题

    最近有项目反应,在服务器CPU使用较高的时候,我们的事件查询页面非常的慢,查询几条记录竟然要4分钟甚至更长,而且在翻第二页的时候也是要这么多的时间,这肯定是不能接受的,也是让现场用SQLServerP ...

  2. js实现前端分页页码管理

    用JS实现前端分页页码管理,可以很美观的区分页码显示(这也是参考大多数网站的分页页码展示),能够有很好的用户体验,这也是有业务需要就写了一下,还是新手,经验不足,欢迎指出批评! 首先先看效果图: 这是 ...

  3. JdbcTemplate+PageImpl实现多表分页查询

    一.基础实体 @MappedSuperclass public abstract class AbsIdEntity implements Serializable { private static ...

  4. MVC如何使用开源分页插件shenniu.pager.js

    最近比较忙,前期忙公司手机端接口项目,各种开发+调试+发布现在几乎上线无问题了:虽然公司项目忙不过在期间抽空做了两件个人觉得有意义的事情,一者使用aspnetcore开发了个人线上项目(要说线上其实只 ...

  5. NET Core-TagHelper实现分页标签

    这里将要和大家分享的是学习总结使用TagHelper实现分页标签,之前分享过一篇使用HtmlHelper扩展了一个分页写法地址可以点击这里http://www.cnblogs.com/wangrudo ...

  6. 套用JQuery EasyUI列表显示数据、分页、查询

    声明,本博客从csdn搬到cnblogs博客园了,以前的csdn不再更新,朋友们可以到这儿来找我的文章,更多的文章会发表,谢谢关注! 有时候闲的无聊,看到extjs那么肥大,真想把自己的项目改了,最近 ...

  7. php实现的分页类

    php分页类文件: <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 pr ...

  8. C#关于分页显示

    ---<PS:本人菜鸟,大手子还请高台贵手> 以下是我今天在做分页时所遇到的一个分页显示问题,使用拼写SQL的方式写的,同类型可参考哦~ ------------------------- ...

  9. JAVA 分页工具类及其使用

    Pager.java package pers.kangxu.datautils.common; import java.io.Serializable; import java.util.List; ...

  10. 分页插件--根据Bootstrap Paginator改写的js插件

    刚刚出来实习,之前实习的公司有一个分页插件,和后端的数据字典约定好了的,基本上是看不到内部是怎么实现的,新公司是做WPF的,好像对于ASP.NET的东西不多,导师扔了一个小系统给我和另一个同事,指了两 ...

随机推荐

  1. The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏

    The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53927   Accepted: 17 ...

  2. 关于List.ToArray()方法的效率测试

    之前一直认为因为List内部是数组,ToArray的实现只是将数组返回出去而已. 今天测了一下发现并不是那样 var a = new List<int>(); ; i < ; i++ ...

  3. view class source code with JAD plugin in Eclipse

    The default class viewer doesn't decompile the class file so you cannot open and check the source co ...

  4. 2016年12月7日 星期三 --出埃及记 Exodus 21:2

    2016年12月7日 星期三 --出埃及记 Exodus 21:2 "If you buy a Hebrew servant, he is to serve you for six year ...

  5. dubbo源码之一——xml schema扩展

    dubbo源码版本:2.5.4 dubbo-parent |----dubbo-config |----dubbo-config-api |----com.alibaba.dubbo.config.* ...

  6. Android编译选项eng、user、userdebug的区别

    eng:debug 版本 user: release 版本 userDebug版本:部分debug版本 LOCAL_MODULE_TAGS := user eng optional test这个样子. ...

  7. 【Javascript】列表查询页面,简单地保存上一次查询的查询参数

    开发中经常做一些查询参数 + 列表参数的功能,这些功能有时候需提供导出Excel,或带超链接到其他明细页面的功能点. 在一些交互性要求严格的系统,需求方会要求: 用户第一个输入某些查询条件进行列表查询 ...

  8. HDU2112 HDU Today 最短路+字符串哈希

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. 【leetcode❤python】102. Binary Tree Level Order Traversal

    #-*- coding: UTF-8 -*-#广度优先遍历# Definition for a binary tree node.# class TreeNode(object):#     def ...

  10. C++,利用链式栈实现括号匹配,界面友好,操作方便,运行流畅

    #include<iostream> #include<string> using namespace std; struct Node { char ch; Node* ne ...