自定义分页控件-基于Zhifeiya的分页控件改版
基于Zhifeiya的分页控件改版的分页。
html显示代码:
<div class="pagelist"> {{.pagerHtml}} </div>
controller调用代码:
currentPage, _ := this.GetInt("cp")//当前页 pageSize, _ := this.GetInt("si")//每页记录数 var po pager.PageOptions po = pager.InitPager("/admin/menu/", pageSize, currentPage, "") list, total := rmBll.QueryMenuListPager(nil, po.Start, po.PageSize) po.Total = total var pagerHtml string = pager.GetPagerInfo(po) this.Data["list"] = list this.Data["pagerHtml"] = template.HTML(pagerHtml)
分页代码:前台样式根据页面样式自行调整
package pager /** * 描述:分页控件 * 作者:Tianqi * 时间:2014-09-15 */ import ( "strconv" ) type PageOptions struct { Start int64 //开始索引 CurrentPage int64 //当前页 PageSize int64 //页面大小,默认20 Url string //链接地址(不含参数 ?前的地址) Total int64 //总记录数 TotalPages int64 //总页数 LinkItemCount int64 //生成A标签的个数 默认10个 Params string //参数 FirstPageText string //首页文字 默认"首页" LastPageText string //尾页文字 默认"尾页" PrePageText string //上一页文字 默认"上一页" NextPageText string //下一页文字 默认"下一页" EnableFirstLastLink bool //是否启用首尾连接 默认false 建议开启 EnablePreNexLink bool //是否启用上一页,下一页连接 默认false 建议开启 } /** * 初始化分页控件 */ func InitPager(url string, pagesize int64, currentpage int64, params string) PageOptions { var po PageOptions po.Url = url po.PageSize = pagesize po.CurrentPage = currentpage po.Params = params setDefault(&po) po.Start = getStart(po.PageSize, po.CurrentPage) return po } /** * 获取分页html */ func GetPagerInfo(po PageOptions) string { po.TotalPages = getTotalPages(po.Total, po.PageSize) var html string html += "<div class='l-btns'>" html += "<span>每页显示</span>" html += "<input type='text' value='" + strconv.Itoa(int(po.PageSize)) + "' id='pageSize' class='pagenum'/>" html += "<span>条,共 " html += strconv.Itoa(int(po.Total)) html += " 条记录/ " html += strconv.Itoa(int(po.TotalPages)) html += " 页</span>" // html += "</div>" // html += "<div class='default'>" html += "" if po.CurrentPage == 1 { html += po.FirstPageText html += po.PrePageText } else { html += "<a href='" + getHref(po.Url, 1, po.PageSize, po.Params) + "'>" html += po.FirstPageText html += "</a><a href='" + getHref(po.Url, po.CurrentPage-1, po.PageSize, po.Params) + "'>" html += po.PrePageText html += "</a>" } if po.TotalPages > po.LinkItemCount { for i := po.CurrentPage - po.LinkItemCount/2 + 1; i <= po.CurrentPage+po.LinkItemCount/2-1; i++ { if i < 1 || i > po.TotalPages { continue } if po.CurrentPage != i { html += "<a href='" + getHref(po.Url, i, po.PageSize, po.Params) + "'>" + strconv.Itoa(int(i)) + "</a>" } else { html += "<span class=\"current\">" + strconv.Itoa(int(i)) + "</span>" } } } else { var i int64 = 1 for ; i <= po.TotalPages; i++ { if po.CurrentPage != i { html += "<a href='" + getHref(po.Url, i, po.PageSize, po.Params) + "'>" + strconv.Itoa(int(i)) + "</a>" } else { html += "<span class=\"current\">" + strconv.Itoa(int(i)) + "</span>" } } } if po.CurrentPage >= po.TotalPages { html += po.NextPageText html += po.LastPageText } else { html += "<a href='" + getHref(po.Url, po.CurrentPage+1, po.PageSize, po.Params) + "'>" html += po.NextPageText html += "</a><a href='" + getHref(po.Url, po.TotalPages, po.PageSize, po.Params) + "'>" html += po.LastPageText html += "</a>" } html += "<input type='hidden' id='pagerHref' value='" + getHref1(po.Url, po.CurrentPage, po.Params) + "'/>" html += "" html += "</div>" return html } /** * 描述:获取起始记录索引 */ func getStart(pageSize int64, currentPage int64) int64 { return (currentPage - 1) * pageSize } /** * 描述:获取总页数 */ func getTotalPages(total int64, pageSize int64) int64 { var totalPages int64 if total%pageSize == 0 { totalPages = total / pageSize } else { totalPages = total/pageSize + 1 } return totalPages } /** * 描述:获取链接地址 */ func getHref(url string, currentPage int64, pageSize int64, params string) string { var href string href += url + "?cp=" + strconv.Itoa(int(currentPage)) + "&si=" + strconv.Itoa(int(pageSize)) if len(params) > 0 { href += params } return href } /** * 描述:获取链接地址,不包含pageSize值,页面使用 */ func getHref1(url string, currentPage int64, params string) string { var href string href += url + "?cp=" + strconv.Itoa(int(currentPage)) if len(params) > 0 { href += params } href += "&si=" return href } /** * 设置默认值 */ func setDefault(po *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.LinkItemCount == 0 { po.LinkItemCount = 10 } if po.PageSize <= 0 { po.PageSize = 20 } if po.CurrentPage < 1 { po.CurrentPage = 1 } }
Tianqi.