mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
gpage分页改进中
This commit is contained in:
parent
19606b8638
commit
978a1273ad
@ -223,9 +223,7 @@ func (s *Server) setHandler(pattern string, handler *handlerItem, hook ... strin
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//b, _ := gparser.VarToJsonIndent(s.handlerTree)
|
||||
//fmt.Println(string(b))
|
||||
//gutil.Dump(s.handlerTree)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -13,29 +13,32 @@ import (
|
||||
"strings"
|
||||
url2 "net/url"
|
||||
"gitee.com/johng/gf/g/util/gconv"
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
"gitee.com/johng/gf/g/util/gregex"
|
||||
"gitee.com/johng/gf/g/util/gutil"
|
||||
)
|
||||
|
||||
// 分页对象
|
||||
type Page struct {
|
||||
Url *url2.URL // 当前页面的URL对象
|
||||
Route string // 当前页面的路由规则(在静态分页下有效)
|
||||
TotalSize int // 总共数据条数
|
||||
TotalPage int // 总页数
|
||||
CurrentPage int // 当前页码
|
||||
PageName string // 分页参数名称(GET参数)
|
||||
NextPageTag string // 下一页标签
|
||||
PrevPageTag string // 上一页标签
|
||||
FirstPageTag string // 首页标签
|
||||
LastPageTag string // 尾页标签
|
||||
PrevBar string // 上一分页条
|
||||
NextBar string // 下一分页条
|
||||
PageBarNum int // 控制分页条的数量
|
||||
AjaxActionName string // AJAX方法名,当该属性有值时,表示使用AJAX分页
|
||||
Url *url2.URL // 当前页面的URL对象
|
||||
Router *ghttp.Router // 当前页面的路由对象(与gf框架耦合,在静态分页下有效)
|
||||
TotalSize int // 总共数据条数
|
||||
TotalPage int // 总页数
|
||||
CurrentPage int // 当前页码
|
||||
PageName string // 分页参数名称(GET参数)
|
||||
NextPageTag string // 下一页标签
|
||||
PrevPageTag string // 上一页标签
|
||||
FirstPageTag string // 首页标签
|
||||
LastPageTag string // 尾页标签
|
||||
PrevBar string // 上一分页条
|
||||
NextBar string // 下一分页条
|
||||
PageBarNum int // 控制分页条的数量
|
||||
AjaxActionName string // AJAX方法名,当该属性有值时,表示使用AJAX分页
|
||||
}
|
||||
|
||||
// 创建一个分页对象,输入参数分别为:
|
||||
// 总数量、每页数量、当前页码、当前的URL(可以只是URI+QUERY)、(可选)路由规则(例如: /user/list/:page、/order/list/*page)
|
||||
func New(TotalSize, perPage int, CurrentPage interface{}, url string, route...string) *Page {
|
||||
// 总数量、每页数量、当前页码、当前的URL(URI+QUERY)、(可选)路由规则(例如: /user/list/:page、/order/list/*page)
|
||||
func New(TotalSize, perPage int, CurrentPage interface{}, url string, router...*ghttp.Router) *Page {
|
||||
u, _ := url2.Parse(url)
|
||||
page := &Page {
|
||||
PageName : "page",
|
||||
@ -55,8 +58,8 @@ func New(TotalSize, perPage int, CurrentPage interface{}, url string, route...s
|
||||
if curPage > 0 {
|
||||
page.CurrentPage = curPage
|
||||
}
|
||||
if len(route) > 0 {
|
||||
page.Route = route[0]
|
||||
if len(router) > 0 {
|
||||
page.Router = router[0]
|
||||
}
|
||||
return page
|
||||
}
|
||||
@ -239,24 +242,17 @@ func (page *Page) GetContent(mode int) string {
|
||||
// 为指定的页面返回地址值
|
||||
func (page *Page) GetUrl(pageNo int) string {
|
||||
url := *page.Url
|
||||
if len(page.Route) > 0 {
|
||||
// 这里基于路由匹配的URL页码替换比较简单,但能满足绝大多数场景
|
||||
index := -1
|
||||
array := strings.Split(page.Route, "/")
|
||||
for k, v := range array {
|
||||
if strings.EqualFold(v, ":" + page.PageName) || strings.EqualFold(v, "*" + page.PageName) {
|
||||
index = k
|
||||
break
|
||||
}
|
||||
}
|
||||
// 替换url.Path中的分页码
|
||||
if index != -1 {
|
||||
pathArray := strings.Split(page.Url.Path, "/")
|
||||
for i := 0; i <= index - len(pathArray); i++ {
|
||||
pathArray = append(pathArray, "")
|
||||
}
|
||||
pathArray[index] = gconv.String(pageNo)
|
||||
url.Path = strings.TrimRight(strings.Join(pathArray, "/"), "/")
|
||||
if page.Router != nil {
|
||||
if page.Router != nil {
|
||||
gutil.Dump(page.Router)
|
||||
|
||||
url.Path, _ = gregex.ReplaceStringFunc(page.Router.RegRule, url.Path, func(s string) string {
|
||||
gutil.Dump(s)
|
||||
if strings.EqualFold(s, page.PageName) {
|
||||
s = gconv.String(pageNo)
|
||||
}
|
||||
return s
|
||||
})
|
||||
return url.String()
|
||||
}
|
||||
}
|
||||
|
@ -3,12 +3,13 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
"gitee.com/johng/gf/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 基本事件回调使用
|
||||
p := "/:name/info/{uid}"
|
||||
s := ghttp.GetServer()
|
||||
s := g.Server()
|
||||
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
|
||||
"BeforeServe" : func(r *ghttp.Request){ fmt.Println("BeforeServe") },
|
||||
"AfterServe" : func(r *ghttp.Request){ fmt.Println("AfterServe") },
|
||||
|
@ -3,25 +3,26 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
"gitee.com/johng/gf/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s := g.Server()
|
||||
|
||||
// 多事件回调示例,事件1
|
||||
p1 := "/:name/info/{uid}"
|
||||
s.BindHookHandlerByMap(p1, map[string]ghttp.HandlerFunc {
|
||||
pattern1 := "/:name/info/{uid}"
|
||||
s.BindHookHandlerByMap(pattern1, map[string]ghttp.HandlerFunc {
|
||||
"BeforeServe" : func(r *ghttp.Request){
|
||||
fmt.Println("打印到Server端终端")
|
||||
},
|
||||
})
|
||||
s.BindHandler(p1, func(r *ghttp.Request) {
|
||||
s.BindHandler(pattern1, func(r *ghttp.Request) {
|
||||
r.Response.Write("用户:", r.Get("name"), ", uid:", r.Get("uid"))
|
||||
})
|
||||
|
||||
// 多事件回调示例,事件2
|
||||
p2 := "/{object}/list/{page}.java"
|
||||
s.BindHookHandlerByMap(p2, map[string]ghttp.HandlerFunc{
|
||||
pattern2 := "/{object}/list/{page}.java"
|
||||
s.BindHookHandlerByMap(pattern2, map[string]ghttp.HandlerFunc {
|
||||
"BeforeOutput" : func(r *ghttp.Request){
|
||||
r.Response.SetBuffer([]byte(
|
||||
fmt.Sprintf("通过事件修改输出内容, object: %s, page: %s",
|
||||
|
@ -8,9 +8,9 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := ghttp.GetServer()
|
||||
s := g.Server()
|
||||
s.BindHandler("/page/static/*page", func(r *ghttp.Request){
|
||||
page := gpage.New(100, 10, r.Get("page"), r.URL.String(), r.Router.Uri)
|
||||
page := gpage.New(100, 10, r.Get("page"), r.URL.String(), r.Router)
|
||||
buffer, _ := gview.ParseContent(`
|
||||
<html>
|
||||
<head>
|
||||
|
Loading…
Reference in New Issue
Block a user