一個簡單的beego分頁小插件(源代碼在最下面):css
支持條件查詢html
支持參數保留git
支持自定義css樣式github
支持表/視圖函數
支持參數自定義 默認爲pnothis
支持定義生成連接的個數url
使用方式:spa
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 }
---支飛亞