// LimitPage 分頁組件 func LimitPage(CurreentPage, AllCount int, FilterArgs, url string) (string, int, int) { // CurreentPage 當前頁 // AllCount 總數據量 // FilterArgs額外的url參數 // url 分頁按鈕的url pageCount, _ := beego.GetConfig("Int", "pagecount", 10) //從配置文件獲取每一個分頁的最大數據量 showPage := 10 //分頁按鈕範圍 // 若是沒有當前分頁就會默認分頁爲第一頁(傳入數值須要處理在沒有分頁的時候會等於0) if AllCount < 1 { AllCount = 1 } // 計算有多少分頁整數 allPage := AllCount / pageCount.(int) // 計算分頁餘數 mod := math.Mod(float64(AllCount), float64(pageCount.(int))) if mod > 0 { 若是有餘再加一頁 allPage++ } 用來存放分頁按鈕的列表 htmlList := []string{} pageHalf := (showPage - 1) / 2 start := 0 stop := 0 var previous string var next string if allPage < showPage { // 若是總分頁數小於展現的分頁數那麼分頁按鈕的結束爲總分頁數 start = 1 stop = allPage } else { // 若是當前頁小於分頁按鈕的一半分頁按鈕的開始等於分頁1結束等於總分頁按鈕 if CurreentPage < pageHalf+1 { start = 1 stop = showPage } else { if CurreentPage >= allPage-pageHalf { start = allPage - showPage stop = allPage } else { start = CurreentPage - pageHalf stop = CurreentPage + pageHalf } } } if CurreentPage <= 1 { // 若是當前頁小於等於1那麼上一頁樣式設置爲不可用 previous = "<li class='page-item disabled'><a href='#' class='page-link'>上一頁</a></li>" } else { as := "<li class='page-item'><a href='%v?page=%v%v' class='page-link' style='cursor:pointer;text-decoration:none;'>上一頁<span aria-hidden='true'>«</span></a></li>" previous = fmt.Sprintf(as, url, CurreentPage-1, FilterArgs) } htmlList = append(htmlList, previous) for i := start; i <= stop; i++ { temp := "" if CurreentPage == i { temp = "<li class='page-item active'><a href='%v?page=%v%v' class='page-link' >%v</a></li>" temp = fmt.Sprintf(temp, url, i, FilterArgs, i) } else { temp = "<li class='page-item'><a href='%v?page=%v%v' class='page-link' >%v</a></li>" temp = fmt.Sprintf(temp, url, i, FilterArgs, i) } htmlList = append(htmlList, temp) } if CurreentPage >= allPage { // 若是當前頁大於等於總頁數那麼下一頁樣式設置爲不可用 next = "<li class='page-item disabled'><a href='#' class='page-link'>下一頁</a></li>" } else { as := "<li class='page-item'><a href='%v?page=%v%v' class='page-link' >下一頁</a></li>" next = fmt.Sprintf(as, url, CurreentPage+1, FilterArgs) } htmlList = append(htmlList, next) data := strings.Join(htmlList, "") dataStart := 0 dataStop := 0 // dataStart 對應的是數據庫的limit, dataStop對應的是數據庫的offset dataStart = (CurreentPage - 1) * pageCount.(int) dataStop = pageCount.(int) return data, dataStart, dataStop }
該模塊我是在寫beego項目的時候寫的,能夠用於其餘的go web框架,只要小改下就行了。html