pager.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package utils
  2. import (
  3. // "fmt"
  4. html "html/template"
  5. con "strconv"
  6. "strings"
  7. "github.com/astaxie/beego/orm"
  8. "fmt"
  9. "math"
  10. )
  11. type PageOptions struct {
  12. TableName string //表名 -----------------[必填]
  13. Conditions string //条件
  14. CurrentPage int //当前页 ,默认1 每次分页,必须在前台设置新的页数,不设置始终默认1.在控制器中使用方式:cp, _ := this.GetInt("pno") po.CurrentPage = int(cp)
  15. PageSize int //页面大小,默认20
  16. LinkItemCount int //生成A标签的个数 默认10个
  17. Href string //A标签的链接地址 ---------[不需要设置]
  18. ParamName string //参数名称 默认是pno
  19. FirstPageText string //首页文字 默认"首页"
  20. LastPageText string //尾页文字 默认"尾页"
  21. PrePageText string //上一页文字 默认"上一页"
  22. NextPageText string //下一页文字 默认"下一页"
  23. EnableFirstLastLink bool //是否启用首尾连接 默认false 建议开启
  24. EnablePreNexLink bool //是否启用上一页,下一页连接 默认false 建议开启
  25. }
  26. /**
  27. * 分页函数,适用任何表
  28. * 返回 总记录条数,总页数,以及当前请求的数据RawSeter,调用中需要"rs.QueryRows(&tblog)"就行了 --tblog是一个Tb_log对象
  29. * 参数:表名,当前页数,页面大小,条件(查询条件,格式为 " and name='zhifeiya' and age=12 ")
  30. */
  31. func GetPagesInfo(tableName string, currentpage int, pagesize int, conditions string) (int, int, orm.RawSeter) {
  32. if currentpage <= 1 {
  33. currentpage = 1
  34. }
  35. if pagesize == 0 {
  36. pagesize = 20
  37. }
  38. var rs orm.RawSeter
  39. o := orm.NewOrm()
  40. var totalItem, totalpages int = 0, 0 //总条数,总页数
  41. o.Raw("SELECT count(*) FROM " + tableName + " where 1 > 0 " + conditions).QueryRow(&totalItem) //获取总条数
  42. if totalItem <= pagesize {
  43. totalpages = 1
  44. } else if totalItem > pagesize {
  45. temp := totalItem / pagesize
  46. if (totalItem % pagesize) != 0 {
  47. temp = temp + 1
  48. }
  49. totalpages = temp
  50. }
  51. rs = o.Raw("select * from " + tableName + " where 1 > 0 " + conditions + " LIMIT " + con.Itoa((currentpage-1)*pagesize) + "," + con.Itoa(pagesize))
  52. return totalItem, totalpages, rs
  53. }
  54. /**
  55. * 返回总记录条数,总页数,当前页面数据,分页html
  56. * 根据分页选项,生成分页连接 下面是一个实例:
  57. func (this *MainController) Test() {
  58. var po util.PageOptions
  59. po.EnablePreNexLink = true
  60. po.EnableFirstLastLink = true
  61. po.LinkItemCount = 7
  62. po.TableName = "help_topic"
  63. cp, _ := this.GetInt("pno")
  64. po.CurrentPage = int(cp)
  65. _,_,_ pager := util.GetPagerLinks(&po, this.Ctx)
  66. this.Data["Email"] = html.HTML(pager)
  67. this.TplName = "test.html"
  68. }
  69. */
  70. func GetPagerLinks(po *PageOptions,requestURI string) (int, int, orm.RawSeter, html.HTML) {
  71. str := ""
  72. totalItem, totalpages, rs := GetPagesInfo(po.TableName, po.CurrentPage, po.PageSize, po.Conditions)
  73. po = setDefault(po, totalpages)
  74. DealUri(po, requestURI)
  75. if totalpages <= po.LinkItemCount {
  76. str = fun1(po, totalpages) //显示完全 12345678910
  77. } else if totalpages > po.LinkItemCount {
  78. if po.CurrentPage < po.LinkItemCount {
  79. str = fun2(po, totalpages) //123456789...200
  80. } else {
  81. if po.CurrentPage + po.LinkItemCount < totalpages {
  82. str = fun3(po, totalpages)
  83. } else {
  84. str = fun4(po, totalpages)
  85. }
  86. }
  87. }
  88. return totalItem, totalpages, rs, html.HTML(str)
  89. }
  90. func GetPagerHtml(requestURI string,pageIndex, pageSize,totalCount int) (html.HTML){
  91. po := &PageOptions{
  92. CurrentPage: pageIndex,
  93. PageSize: pageSize,
  94. EnableFirstLastLink : true,
  95. ParamName : "page",
  96. }
  97. totalPages := int(math.Ceil(float64(totalCount) / float64(pageSize)))
  98. setDefault(po,totalPages)
  99. DealUri(po,requestURI)
  100. str := ""
  101. if totalPages <= po.LinkItemCount {
  102. str = fun1(po, totalPages) //显示完全 12345678910
  103. } else if totalPages > po.LinkItemCount {
  104. if po.CurrentPage < po.LinkItemCount {
  105. str = fun2(po, totalPages) //123456789...200
  106. } else {
  107. if po.CurrentPage + po.LinkItemCount < totalPages {
  108. str = fun3(po, totalPages)
  109. } else {
  110. str = fun4(po, totalPages)
  111. }
  112. }
  113. }
  114. str = strings.Replace(str,"?&","?",-1)
  115. //str = strings.Replace(str,"&&","&",-1)
  116. return html.HTML(str)
  117. }
  118. /**
  119. * 处理url,目的是保存参数
  120. */
  121. func DealUri(po *PageOptions, requestURI string) {
  122. var rs string
  123. if strings.Contains(requestURI, "?") {
  124. arr := strings.Split(requestURI, "?")
  125. rs = ""
  126. arr2 := strings.Split(arr[1], "&")
  127. for _, v := range arr2 {
  128. if !strings.Contains(v, po.ParamName) {
  129. if strings.HasSuffix(rs,"&") {
  130. rs += v
  131. }else{
  132. rs += v + "&"
  133. }
  134. //rs += "&" + v
  135. }
  136. }
  137. if strings.HasPrefix(rs,"&") {
  138. rs = string(rs[1:])
  139. }
  140. if strings.HasSuffix(rs,"&"){
  141. rs = string(rs[0:strings.Count(rs,"")-1])
  142. }
  143. rs = arr[0] + "?" + rs
  144. fmt.Println(rs)
  145. } else {
  146. //rs = requestURI + "?" //+ po.ParamName + "time=" + con.Itoa(time.Now().Second())
  147. rs = requestURI + "?"
  148. }
  149. po.Href = rs
  150. }
  151. /**
  152. * 1...197 198 199 200
  153. */
  154. func fun4(po *PageOptions, totalPages int) string {
  155. rs := ""
  156. rs += getHeader(po, totalPages)
  157. rs += "<li><a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(1) + "'>" + con.Itoa(1) + "</a><li>"
  158. rs += "<li><a href=''>...</a></li>"
  159. for i := totalPages - po.LinkItemCount; i <= totalPages; i++ {
  160. if po.CurrentPage != i {
  161. rs += "<li><a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(i) + "'>" + con.Itoa(i) + "</a></li>"
  162. } else {
  163. rs += "<li class=\"active\"><a href=\"###\">" + con.Itoa(i) + " <span class=\"sr-only\">(current)</span></a></li>"
  164. }
  165. }
  166. rs += getFooter(po, totalPages)
  167. return rs
  168. }
  169. /**
  170. * 1...6 7 8 9 10 11 12 13 14 15... 200
  171. */
  172. func fun3(po *PageOptions, totalpages int) string {
  173. var rs string = ""
  174. rs += getHeader(po, totalpages)
  175. rs += "<li><a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(1) + "'>" + con.Itoa(1) + "</a></li>"
  176. rs += "<a href=''>...</a>"
  177. for i := po.CurrentPage - po.LinkItemCount/2 + 1; i <= po.CurrentPage+po.LinkItemCount/2-1; i++ {
  178. if po.CurrentPage != i {
  179. rs += "<a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(i) + "'>" + con.Itoa(i) + "</a>"
  180. } else {
  181. rs += "<li class=\"active\"><a href=\"###\">" + con.Itoa(i) + " <span class=\"sr-only\">(current)</span></a></li>"
  182. }
  183. }
  184. rs += "<a href=''>...</a>"
  185. rs += "<li><a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(totalpages) + "'>" + con.Itoa(totalpages) + "</a></li>"
  186. rs += getFooter(po, totalpages)
  187. return rs
  188. }
  189. /**
  190. * totalpages > po.LinkItemCount po.CurrentPage < po.LinkItemCount
  191. * 123456789...200
  192. */
  193. func fun2(po *PageOptions, totalpages int) string {
  194. var rs string = ""
  195. rs += getHeader(po, totalpages)
  196. for i := 1; i <= po.LinkItemCount+1; i++ {
  197. if i == po.LinkItemCount {
  198. rs += "<li><a href=\"" + po.Href + "&" + po.ParamName + "=" + con.Itoa(i) + "\">...</a></li>"
  199. } else if i == po.LinkItemCount+1 {
  200. rs += "<li><a href=\"" + po.Href + "&" + po.ParamName + "=" + con.Itoa(totalpages) + "\">" + con.Itoa(totalpages) + "</a></li>"
  201. } else {
  202. if po.CurrentPage != i {
  203. rs += "<li><a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(i) + "'>" + con.Itoa(i) + "</a></li>"
  204. } else {
  205. rs += "<li class=\"active\"><a href=\"###\">" + con.Itoa(i) + " <span class=\"sr-only\">(current)</span></a></li>"
  206. }
  207. }
  208. }
  209. rs += getFooter(po, totalpages)
  210. return rs
  211. }
  212. /**
  213. * totalpages <= po.LinkItemCount
  214. * 显示完全 12345678910
  215. */
  216. func fun1(po *PageOptions, totalpages int) string {
  217. var rs string = ""
  218. rs += getHeader(po, totalpages)
  219. for i := 1; i <= totalpages; i++ {
  220. if po.CurrentPage != i {
  221. rs += "<li><a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(i) + "'>" + con.Itoa(i) + "</a></li>"
  222. } else {
  223. rs += "<li class=\"active\"><a href=\"###\">" + con.Itoa(i) + " <span class=\"sr-only\">(current)</span></a></li>"
  224. }
  225. }
  226. rs += getFooter(po, totalpages)
  227. return rs
  228. }
  229. /**
  230. * 头部
  231. */
  232. func getHeader(po *PageOptions, totalpages int) string {
  233. var rs string = "<ul class=\"pagination\">"
  234. if po.EnableFirstLastLink { //当首页,尾页都设定的时候,就显示
  235. rs += "<li" + judgeDisable(po, totalpages, 0) + " ><a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(1) + "'>" + po.FirstPageText + "</a></li>"
  236. }
  237. if po.EnablePreNexLink { // disabled=\"disabled\"
  238. var a int = po.CurrentPage - 1
  239. if po.CurrentPage == 1 {
  240. a = 1
  241. }
  242. rs += "<li" + judgeDisable(po, totalpages, 0) + " ><a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(a) + "'>" + po.PrePageText + "</a></li>"
  243. }
  244. return rs
  245. }
  246. /**
  247. * 尾部
  248. */
  249. func getFooter(po *PageOptions, totalpages int) string {
  250. var rs string = ""
  251. if po.EnablePreNexLink {
  252. var a int = po.CurrentPage + 1
  253. if po.CurrentPage == totalpages {
  254. a = totalpages
  255. }
  256. rs += "<li " + judgeDisable(po, totalpages, 1) + " ><a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(a) + "'>" + po.NextPageText + "</a></li>"
  257. }
  258. if po.EnableFirstLastLink { //当首页,尾页都设定的时候,就显示
  259. rs += "<li " + judgeDisable(po, totalpages, 1) + " ><a href='" + po.Href + "&" + po.ParamName + "=" + con.Itoa(totalpages) + "'>" + po.LastPageText + "</a></li>"
  260. }
  261. rs += "</ul>"
  262. return rs
  263. }
  264. /**
  265. * 设置默认值
  266. */
  267. func setDefault(po *PageOptions, totalpages int) *PageOptions {
  268. if len(po.FirstPageText) <= 0 {
  269. po.FirstPageText = "首页"
  270. }
  271. if len(po.LastPageText) <= 0 {
  272. po.LastPageText = "尾页"
  273. }
  274. if len(po.PrePageText) <= 0 {
  275. po.PrePageText = "上一页"
  276. }
  277. if len(po.NextPageText) <= 0 {
  278. po.NextPageText = "下一页"
  279. }
  280. if po.CurrentPage >= totalpages {
  281. po.CurrentPage = totalpages
  282. }
  283. if po.CurrentPage <= 1 {
  284. po.CurrentPage = 1
  285. }
  286. if po.LinkItemCount == 0 {
  287. po.LinkItemCount = 10
  288. }
  289. if po.PageSize == 0 {
  290. po.PageSize = 20
  291. }
  292. if len(po.ParamName) <= 0 {
  293. po.ParamName = "pno"
  294. }
  295. return po
  296. }
  297. /**
  298. *判断首页尾页 上一页下一页是否能用
  299. */
  300. func judgeDisable(po *PageOptions, totalpages int, h_f int) string {
  301. var rs string = ""
  302. //判断头部
  303. if h_f == 0 {
  304. if po.CurrentPage == 1 {
  305. rs = " "
  306. }
  307. } else {
  308. if po.CurrentPage == totalpages {
  309. rs = " "
  310. }
  311. }
  312. return rs
  313. }