From b691a2333fd2d29251211cac6c007aa5825b6867 Mon Sep 17 00:00:00 2001 From: Minho Date: Fri, 26 Jan 2018 18:07:55 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/book.go | 11 +- controllers/document.go | 5 +- controllers/home.go | 7 +- controllers/label.go | 12 +- controllers/manager.go | 17 +- controllers/search.go | 7 +- models/attachment.go | 8 +- models/member.go | 4 +- utils/pager.go | 346 --------------------------------- utils/pagination/pagination.go | 112 +++++++++++ views/widgets/header.tpl | 2 +- 11 files changed, 148 insertions(+), 383 deletions(-) delete mode 100644 utils/pager.go create mode 100644 utils/pagination/pagination.go diff --git a/controllers/book.go b/controllers/book.go index 35904831..ce361ff2 100644 --- a/controllers/book.go +++ b/controllers/book.go @@ -19,6 +19,7 @@ import ( "github.com/lifei6671/mindoc/graphics" "github.com/lifei6671/mindoc/models" "github.com/lifei6671/mindoc/utils" + "github.com/lifei6671/mindoc/utils/pagination" ) type BookController struct { @@ -39,9 +40,8 @@ func (c *BookController) Index() { } if totalCount > 0 { - html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, totalCount) - - c.Data["PageHtml"] = html + pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize) + c.Data["PageHtml"] = pager.HtmlPages() } else { c.Data["PageHtml"] = "" } @@ -364,9 +364,8 @@ func (c *BookController) Users() { members, totalCount, err := models.NewMemberRelationshipResult().FindForUsersByBookId(book.BookId, pageIndex, 15) if totalCount > 0 { - html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, 10, totalCount) - - c.Data["PageHtml"] = html + pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize) + c.Data["PageHtml"] = pager.HtmlPages() } else { c.Data["PageHtml"] = "" } diff --git a/controllers/document.go b/controllers/document.go index 4fa5fc6c..3dff1cf8 100644 --- a/controllers/document.go +++ b/controllers/document.go @@ -24,6 +24,7 @@ import ( "github.com/lifei6671/mindoc/conf" "github.com/lifei6671/mindoc/models" "github.com/lifei6671/mindoc/utils" + "github.com/lifei6671/mindoc/utils/pagination" "github.com/russross/blackfriday" ) @@ -1056,8 +1057,8 @@ func (c *DocumentController) History() { c.Data["Document"] = doc if totalCount > 0 { - html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, totalCount) - c.Data["PageHtml"] = html + pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize) + c.Data["PageHtml"] = pager.HtmlPages() } } diff --git a/controllers/home.go b/controllers/home.go index b3fcfa70..c96a5a33 100644 --- a/controllers/home.go +++ b/controllers/home.go @@ -4,7 +4,7 @@ import ( "net/url" "github.com/astaxie/beego" "github.com/lifei6671/mindoc/models" - "github.com/lifei6671/mindoc/utils" + "github.com/lifei6671/mindoc/utils/pagination" "math" "github.com/lifei6671/mindoc/conf" ) @@ -35,9 +35,8 @@ func (c *HomeController) Index() { c.Abort("500") } if totalCount > 0 { - html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, pageSize, totalCount) - - c.Data["PageHtml"] = html + pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize) + c.Data["PageHtml"] = pager.HtmlPages() } else { c.Data["PageHtml"] = "" } diff --git a/controllers/label.go b/controllers/label.go index 98b7386c..17cbba1d 100644 --- a/controllers/label.go +++ b/controllers/label.go @@ -5,7 +5,7 @@ import ( "github.com/astaxie/beego/orm" "github.com/lifei6671/mindoc/conf" "github.com/lifei6671/mindoc/models" - "github.com/lifei6671/mindoc/utils" + "github.com/lifei6671/mindoc/utils/pagination" "math" ) @@ -54,9 +54,8 @@ func (c *LabelController) Index() { return } if totalCount > 0 { - html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, totalCount) - - c.Data["PageHtml"] = html + pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize) + c.Data["PageHtml"] = pager.HtmlPages() } else { c.Data["PageHtml"] = "" } @@ -78,9 +77,8 @@ func (c *LabelController) List() { c.ShowErrorPage(50001, err.Error()) } if totalCount > 0 { - html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, pageSize, totalCount) - - c.Data["PageHtml"] = html + pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize) + c.Data["PageHtml"] = pager.HtmlPages() } else { c.Data["PageHtml"] = "" } diff --git a/controllers/manager.go b/controllers/manager.go index 6b1e7f8f..8d40932c 100644 --- a/controllers/manager.go +++ b/controllers/manager.go @@ -14,6 +14,7 @@ import ( "github.com/lifei6671/mindoc/utils" "path/filepath" "strconv" + "github.com/lifei6671/mindoc/utils/pagination" ) type ManagerController struct { @@ -49,9 +50,8 @@ func (c *ManagerController) Users() { } if totalCount > 0 { - html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, int(totalCount)) - - c.Data["PageHtml"] = html + pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize) + c.Data["PageHtml"] = pager.HtmlPages() } else { c.Data["PageHtml"] = "" } @@ -286,9 +286,11 @@ func (c *ManagerController) Books() { } if totalCount > 0 { - html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, totalCount) + //html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, 8, totalCount) - c.Data["PageHtml"] = html + pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize) + + c.Data["PageHtml"] = pager.HtmlPages() } else { c.Data["PageHtml"] = "" } @@ -564,9 +566,8 @@ func (c *ManagerController) AttachList() { } if totalCount > 0 { - html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, int(totalCount)) - - c.Data["PageHtml"] = html + pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize) + c.Data["PageHtml"] = pager.HtmlPages() } else { c.Data["PageHtml"] = "" } diff --git a/controllers/search.go b/controllers/search.go index 9387aaf1..d5483535 100644 --- a/controllers/search.go +++ b/controllers/search.go @@ -4,7 +4,7 @@ import ( "github.com/astaxie/beego" "github.com/lifei6671/mindoc/conf" "github.com/lifei6671/mindoc/models" - "github.com/lifei6671/mindoc/utils" + "github.com/lifei6671/mindoc/utils/pagination" "regexp" "strconv" "strings" @@ -42,9 +42,8 @@ func (c *SearchController) Index() { return } if totalCount > 0 { - html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, totalCount) - - c.Data["PageHtml"] = html + pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize) + c.Data["PageHtml"] = pager.HtmlPages() } else { c.Data["PageHtml"] = "" } diff --git a/models/attachment.go b/models/attachment.go index cbe0b456..bdf08740 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -90,14 +90,16 @@ func (m *Attachment) FindListByDocumentId(doc_id int) (attaches []*Attachment, e } //分页查询附件 -func (m *Attachment) FindToPager(pageIndex, pageSize int) (attachList []*AttachmentResult, totalCount int64, err error) { +func (m *Attachment) FindToPager(pageIndex, pageSize int) (attachList []*AttachmentResult, totalCount int, err error) { o := orm.NewOrm() - totalCount, err = o.QueryTable(m.TableNameWithPrefix()).Count() + total, err := o.QueryTable(m.TableNameWithPrefix()).Count() if err != nil { - return + + return nil,0,err } + totalCount = int(total) offset := (pageIndex - 1) * pageSize var list []*Attachment diff --git a/models/member.go b/models/member.go index de8400db..3a069e65 100644 --- a/models/member.go +++ b/models/member.go @@ -231,7 +231,7 @@ func (m *Member) FindByAccount(account string) (*Member, error) { } //分页查找用户. -func (m *Member) FindToPager(pageIndex, pageSize int) ([]*Member, int64, error) { +func (m *Member) FindToPager(pageIndex, pageSize int) ([]*Member, int, error) { o := orm.NewOrm() var members []*Member @@ -253,7 +253,7 @@ func (m *Member) FindToPager(pageIndex, pageSize int) ([]*Member, int64, error) for _, m := range members { m.ResolveRoleName() } - return members, totalCount, nil + return members, int(totalCount), nil } func (c *Member) IsAdministrator() bool { diff --git a/utils/pager.go b/utils/pager.go deleted file mode 100644 index 2f8adf7a..00000000 --- a/utils/pager.go +++ /dev/null @@ -1,346 +0,0 @@ -package utils - -import ( - // "fmt" - html "html/template" - con "strconv" - "strings" - - "fmt" - "github.com/astaxie/beego/orm" - "math" -) - -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 建议开启 - TotalPages int -} - -/** - * 分页函数,适用任何表 - * 返回 总记录条数,总页数,以及当前请求的数据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 1 > 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.TplName = "test.html" - } -*/ -func GetPagerLinks(po *PageOptions, requestURI string) (int, int, orm.RawSeter, html.HTML) { - str := "" - totalItem, totalpages, rs := GetPagesInfo(po.TableName, po.CurrentPage, po.PageSize, po.Conditions) - po = setDefault(po, totalpages) - DealUri(po, requestURI) - 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) -} - -func GetPagerHtml(requestURI string, pageIndex, pageSize, totalCount int) html.HTML { - po := &PageOptions{ - CurrentPage: pageIndex, - PageSize: pageSize, - EnableFirstLastLink: true, - ParamName: "page", - TotalPages: int(math.Ceil(float64(totalCount) / float64(pageSize))), - LinkItemCount: pageSize, - } - totalPages := int(math.Ceil(float64(totalCount) / float64(pageSize))) - - setDefault(po, totalPages) - DealUri(po, requestURI) - str := "" - 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) - } - } - } - str = strings.Replace(str, "?&", "?", -1) - //str = strings.Replace(str,"&&","&",-1) - return html.HTML(str) -} - -/** - * 处理url,目的是保存参数 - */ -func DealUri(po *PageOptions, requestURI string) { - var rs string - if strings.Contains(requestURI, "?") { - arr := strings.Split(requestURI, "?") - rs = "" - arr2 := strings.Split(arr[1], "&") - for _, v := range arr2 { - if !strings.Contains(v, po.ParamName) { - if strings.HasSuffix(rs, "&") { - rs += v - } else { - rs += v + "&" - } - //rs += "&" + v - } - } - if strings.HasPrefix(rs, "&") { - rs = string(rs[1:]) - } - if strings.HasSuffix(rs, "&") { - rs = string(rs[0 : strings.Count(rs, "")-1]) - } - rs = arr[0] + "?" + rs - fmt.Println(rs) - } else { - //rs = requestURI + "?" //+ po.ParamName + "time=" + con.Itoa(time.Now().Second()) - rs = requestURI + "?" - } - - po.Href = rs -} - -/** - * 1...197 198 199 200 - */ -func fun4(po *PageOptions, totalPages int) string { - rs := "" - rs += getHeader(po, totalPages) - rs += "
  • " + con.Itoa(1) + "
  • " - - rs += "
  • ...
  • " - - - for i := totalPages - po.LinkItemCount-1; i <= totalPages; i++ { - if po.CurrentPage != i { - rs += "
  • " + con.Itoa(i) + "
  • " - } else { - rs += "
  • " + con.Itoa(i) + " (current)
  • " - } - } - 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 += "
  • " + con.Itoa(1) + "
  • " - rs += "
  • ...
  • " - for i := po.CurrentPage - po.LinkItemCount/2 + 1; i <= po.CurrentPage+po.LinkItemCount/2-1; i++ { - if po.CurrentPage != i { - rs += "
  • " + con.Itoa(i) + "
  • " - } else { - rs += "
  • " + con.Itoa(i) + " (current)
  • " - } - } - rs += "
  • ...
  • " - rs += "
  • " + con.Itoa(totalpages) + "
  • " - rs += getFooter(po, totalpages) - return rs - -} - -/** - * totalpages > po.LinkItemCount po.CurrentPage < po.LinkItemCount - * 123456789...200 - */ -func fun2(po *PageOptions, totalpages int) string { - rs := "" - rs += getHeader(po, totalpages) - for i := 1; i <= po.LinkItemCount+2; i++ { - if i == po.LinkItemCount+2 { - rs += "
  • ...
  • " - } else if i == po.LinkItemCount+1 { - rs += "
  • " + con.Itoa(totalpages) + "
  • " - } else { - if po.CurrentPage != i { - rs += "
  • " + con.Itoa(i) + "
  • " - } else { - rs += "
  • " + con.Itoa(i) + " (current)
  • " - } - } - } - 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 += "
  • " + con.Itoa(i) + "
  • " - } else { - rs += "
  • " + con.Itoa(i) + " (current)
  • " - } - } - rs += getFooter(po, totalpages) - return rs -} - -/** - * 头部 - */ -func getHeader(po *PageOptions, totalpages int) string { - var rs string = "" - 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 = " " - } - } else { - if po.CurrentPage == totalpages { - rs = " " - } - } - return rs -} diff --git a/utils/pagination/pagination.go b/utils/pagination/pagination.go new file mode 100644 index 00000000..0baa0bb3 --- /dev/null +++ b/utils/pagination/pagination.go @@ -0,0 +1,112 @@ +package pagination + +import ( + "fmt" + "math" + "net/http" + "net/url" + "strconv" + "strings" + "html/template" +) + +//Pagination 分页器 +type Pagination struct { + Request *http.Request + Total int + Pernum int +} + +//NewPagination 新建分页器 +func NewPagination(req *http.Request, total int, pernum int) *Pagination { + return &Pagination{ + Request: req, + Total: total, + Pernum: pernum, + } +} + +func (p *Pagination) HtmlPages() template.HTML { + return template.HTML(p.Pages()) +} + +//Pages 渲染生成html分页标签 +func (p *Pagination) Pages() string { + queryParams := p.Request.URL.Query() + //从当前请求中获取page + page := queryParams.Get("page") + if page == "" { + page = "1" + } + //将页码转换成整型,以便计算 + pagenum, _ := strconv.Atoi(page) + if pagenum == 0 { + return "" + } + + //计算总页数 + var totalPageNum = int(math.Ceil(float64(p.Total) / float64(p.Pernum))) + + //首页链接 + var firstLink string + //上一页链接 + var prevLink string + //下一页链接 + var nextLink string + //末页链接 + var lastLink string + //中间页码链接 + var pageLinks []string + + //首页和上一页链接 + if pagenum > 1 { + firstLink = fmt.Sprintf(`
  • 首页
  • `, p.pageURL("1")) + prevLink = fmt.Sprintf(`
  • 上一页
  • `, p.pageURL(strconv.Itoa(pagenum-1))) + } else { + firstLink = `
  • 首页
  • ` + prevLink = `
  • 上一页
  • ` + } + + //末页和下一页 + if pagenum < totalPageNum { + lastLink = fmt.Sprintf(`
  • 末页
  • `, p.pageURL(strconv.Itoa(totalPageNum))) + nextLink = fmt.Sprintf(`
  • 下一页
  • `, p.pageURL(strconv.Itoa(pagenum+1))) + } else { + lastLink = `
  • 末页
  • ` + nextLink = `
  • 下一页
  • ` + } + + //生成中间页码链接 + pageLinks = make([]string, 0, 10) + startPos := pagenum - 3 + endPos := pagenum + 3 + if startPos < 1 { + endPos = endPos + int(math.Abs(float64(startPos))) + 1 + startPos = 1 + } + if endPos > totalPageNum { + endPos = totalPageNum + } + for i := startPos; i <= endPos; i++ { + var s string + if i == pagenum { + s = fmt.Sprintf(`
  • %d
  • `, p.pageURL(strconv.Itoa(i)), i) + } else { + s = fmt.Sprintf(`
  • %d
  • `, p.pageURL(strconv.Itoa(i)), i) + } + pageLinks = append(pageLinks, s) + } + + return fmt.Sprintf(``, firstLink, prevLink, strings.Join(pageLinks, ""), nextLink, lastLink) +} + +//pageURL 生成分页url +func (p *Pagination) pageURL(page string) string { + //基于当前url新建一个url对象 + u, _ := url.Parse(p.Request.URL.String()) + q := u.Query() + q.Set("page", page) + u.RawQuery = q.Encode() + return u.String() +} + diff --git a/views/widgets/header.tpl b/views/widgets/header.tpl index d57f0744..43c98af3 100644 --- a/views/widgets/header.tpl +++ b/views/widgets/header.tpl @@ -60,7 +60,7 @@ {{if gt .Member.MemberId 0}}