golang

beego 一个幽美的分页功能

2019-09-26  本文已影响0人  顶尖少爷

分页功能

/*
@Time : 2019/8/29 11:10 
@Author : liwei
@File : adminUserController
@Software: GoLand
*/

package controllers

import (
    "fmt"
    "github.com/astaxie/beego"
    "github.com/astaxie/beego/orm"
    "web.demo/models"
    "web.demo/pager"
)

type AdminUserController struct {
    beego.Controller
}

//@router /user_list  [get]
func (this *AdminUserController) Get(){
    pno,_:=this.GetInt("page")//获取当前请求页
    var Items[]models.User
    conditions :=" order by id desc"
    var po pager.PageOptions
    po.ParamName = "page"
    po.TableName = "users"                                                 //指定分页的表名
    po.EnableFirstLastLink = true                                           //是否显示首页尾页 默认false
    po.EnablePreNexLink = true                                              //是否显示上一页下一页 默认为false
    po.Conditions = conditions                                              // 传递分页条件 默认全表
    po.Currentpage = int(pno)                                               //传递当前页数,默认为1
    po.PageSize = 20                                                     //页面大小  默认为20

    //返回分页信息,
    //第一个:为返回的当前页面数据集合,ResultSet类型
    //第二个:生成的分页链接
    //第三个:返回总记录数
    //第四个:返回总页数
    totalItem, totalPage, rs, pagerhtml2 := pager.GetPagerLinks(&po, this.Ctx)
    rs.QueryRows(&Items)      //把当前页面的数据序列化进一个切片内
    this.Data["pagerhtml"] = pagerhtml2
    this.Data["totalItem"] = totalItem
    this.Data["PageSize"] = po.PageSize
    this.Data["totalPage"] =totalPage
    this.Data["Items"] = Items

    this.Layout= "layout.tpl"
    this.TplName = "admin/user/user_list.tpl"
}

func (this *AdminUserController) ShowAPIVersion(){
    o:=orm.NewOrm()
    users :=new(models.User)
    var Items[]models.User
    o.QueryTable(users).All(&Items)

    this.Data["Website"] = "WEB.DEMO"
    this.Data["Email"] = "php.wei.li@gmail.com"
    this.Data["Items"] = Items
    fmt.Println(Items)
    this.TplName = "admin/user/user_list.tpl"
}

css样式

#pull_right {
    text-align: right;
}

.pull-right {
    /*float: left!important;*/
}

.pagination {
    display: inline-block;
    padding-left: 0;
    margin: 20px 0;
    border-radius: 4px;
}

.pagination > li {
    display: inline;
}

.pagination > li > a,
.pagination > li > span {
    position: relative;
    float: left;
    padding: 6px 12px;
    margin-left: -1px;
    line-height: 1.42857143;
    color: #428bca;
    text-decoration: none;
    background-color: #fff;
    border: 1px solid #ddd;
}

.pagination > li:first-child > a,
.pagination > li:first-child > span {
    margin-left: 0;
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
}

.pagination > li:last-child > a,
.pagination > li:last-child > span {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
}

.pagination > li > a:hover,
.pagination > li > span:hover,
.pagination > li > a:focus,
.pagination > li > span:focus {
    color: #2a6496;
    background-color: #eee;
    border-color: #ddd;
}

.pagination > .active > a,
.pagination > .active > span,
.pagination > .active > a:hover,
.pagination > .active > span:hover,
.pagination > .active > a:focus,
.pagination > .active > span:focus {
    z-index: 2;
    color: #fff;
    cursor: default;
    background-color: #428bca;
    border-color: #428bca;
}

.pagination > .disabled > span,
.pagination > .disabled > span:hover,
.pagination > .disabled > span:focus,
.pagination > .disabled > a,
.pagination > .disabled > a:hover,
.pagination > .disabled > a:focus {
    color: #777;
    cursor: not-allowed;
    background-color: #fff;
    border-color: #ddd;
}

.clear {
    clear: both;
}

controller中的使用

pno,_:=this.GetInt("page")//获取当前请求页
    var Items[]models.User
    conditions :=" order by id desc"
    var po pager.PageOptions
    po.ParamName = "page"
    po.TableName = "users"                                                 //指定分页的表名
    po.EnableFirstLastLink = true                                           //是否显示首页尾页 默认false
    po.EnablePreNexLink = true                                              //是否显示上一页下一页 默认为false
    po.Conditions = conditions                                              // 传递分页条件 默认全表
    po.Currentpage = int(pno)                                               //传递当前页数,默认为1
    po.PageSize = 20                                                     //页面大小  默认为20

    //返回分页信息,
    //第一个:为返回的当前页面数据集合,ResultSet类型
    //第二个:生成的分页链接
    //第三个:返回总记录数
    //第四个:返回总页数
    totalItem, totalPage, rs, pagerhtml2 := pager.GetPagerLinks(&po, this.Ctx)
    rs.QueryRows(&Items)      //把当前页面的数据序列化进一个切片内
    this.Data["pagerhtml"] = pagerhtml2
    this.Data["totalItem"] = totalItem
    this.Data["PageSize"] = po.PageSize
    this.Data["totalPage"] =totalPage
    this.Data["Items"] = Items
    this.TplName = "admin/user/user_list.tpl"

tpl中的使用


<div style="float:left;">{{.pagerhtml}}</div>
上一篇 下一篇

猜你喜欢

热点阅读