Skip to content

Commit f435911

Browse files
author
cg33
committed
Feature: admin plugin => add custom page size and sort type support
1 parent 9dad923 commit f435911

File tree

12 files changed

+86
-29
lines changed

12 files changed

+86
-29
lines changed

plugins/admin/controller/handler.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ func setFormWithReturnErrMessage(ctx *context.Context, errMsg string, kind strin
9696
}
9797
formData, groupFormData, groupHeaders, title, description, _ = table.List[prefix].GetDataFromDatabaseWithId(id)
9898
} else {
99-
formData, groupFormData, groupHeaders = table.GetNewFormList(panel.GetForm().TabHeaders, panel.GetForm().TabGroups, panel.GetForm().FieldList)
99+
formData, groupFormData, groupHeaders = table.GetNewFormList(panel.GetForm().TabHeaders, panel.GetForm().TabGroups,
100+
panel.GetForm().FieldList)
100101
title = panel.GetForm().Title
101102
description = panel.GetForm().Description
102103
}
103104

104-
queryParam := parameter.GetParam(ctx.Request.URL.Query()).GetRouteParamStr()
105+
queryParam := parameter.GetParam(ctx.Request.URL.Query(), panel.GetInfo().DefaultPageSize,
106+
panel.GetPrimaryKey().Name, panel.GetInfo().GetSort()).GetRouteParamStr()
105107

106108
user := auth.Auth(ctx)
107109

plugins/admin/controller/show.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ func ShowInfo(ctx *context.Context) {
2929
prefix := ctx.Query("__prefix")
3030
panel := table.List[prefix]
3131

32-
params := parameter.GetParam(ctx.Request.URL.Query())
32+
params := parameter.GetParam(ctx.Request.URL.Query(), panel.GetInfo().DefaultPageSize, panel.GetPrimaryKey().Name,
33+
panel.GetInfo().GetSort())
3334

3435
editUrl := modules.AorB(panel.GetEditable() && !panel.GetInfo().IsHideEditButton,
3536
config.Url("/info/"+prefix+"/edit"+params.GetRouteParamStr()), "")
@@ -183,11 +184,14 @@ func Export(ctx *context.Context) {
183184
)
184185

185186
if len(param.Id) == 1 {
186-
params := parameter.GetParam(ctx.Request.URL.Query())
187+
params := parameter.GetParam(ctx.Request.URL.Query(), panel.GetInfo().DefaultPageSize, panel.GetPrimaryKey().Name,
188+
panel.GetInfo().GetSort())
187189
panelInfo, err = panel.GetDataFromDatabase(ctx.Path(), params)
188-
fileName = fmt.Sprintf("%s-%d-page-%s-pageSize-%s.xlsx", panel.GetInfo().Title, time.Now().Unix(), params.Page, params.PageSize)
190+
fileName = fmt.Sprintf("%s-%d-page-%s-pageSize-%s.xlsx", panel.GetInfo().Title, time.Now().Unix(),
191+
params.Page, params.PageSize)
189192
} else {
190-
panelInfo, err = panel.GetDataFromDatabaseWithIds(ctx.Path(), parameter.GetParam(ctx.Request.URL.Query()), param.Id)
193+
panelInfo, err = panel.GetDataFromDatabaseWithIds(ctx.Path(), parameter.GetParam(ctx.Request.URL.Query(),
194+
panel.GetInfo().DefaultPageSize, panel.GetPrimaryKey().Name, panel.GetInfo().GetSort()), param.Id)
191195
fileName = fmt.Sprintf("%s-%d-id-%s.xlsx", panel.GetInfo().Title, time.Now().Unix(), strings.Join(param.Id, "_"))
192196
}
193197

plugins/admin/modules/guard/edit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ func ShowForm(ctx *context.Context) {
5151
Panel: panel,
5252
Id: id,
5353
Prefix: prefix,
54-
Param: parameter.GetParam(ctx.Request.URL.Query()),
54+
Param: parameter.GetParam(ctx.Request.URL.Query(), panel.GetInfo().DefaultPageSize, panel.GetPrimaryKey().Name,
55+
panel.GetInfo().GetSort()),
5556
})
5657
ctx.Next()
5758
}

plugins/admin/modules/guard/new.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ func ShowNewForm(ctx *context.Context) {
3939
ctx.SetUserValue("show_new_form_param", &ShowNewFormParam{
4040
Panel: panel,
4141
Prefix: prefix,
42-
Param: parameter.GetParam(ctx.Request.URL.Query()),
42+
Param: parameter.GetParam(ctx.Request.URL.Query(), panel.GetInfo().DefaultPageSize, panel.GetPrimaryKey().Name,
43+
panel.GetInfo().GetSort()),
4344
})
4445
ctx.Next()
4546
}

plugins/admin/modules/paginator/paginator.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package paginator
22

33
import (
4-
"github.com/GoAdminGroup/go-admin/modules/config"
54
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/parameter"
65
template2 "github.com/GoAdminGroup/go-admin/template"
76
"github.com/GoAdminGroup/go-admin/template/components"
@@ -11,9 +10,9 @@ import (
1110
"strconv"
1211
)
1312

14-
func Get(path string, params parameter.Parameters, size int) types.PaginatorAttribute {
13+
func Get(path string, params parameter.Parameters, size int, pageSizeList []string) types.PaginatorAttribute {
1514

16-
paginator := template2.Get(config.Get().Theme).Paginator().(*components.PaginatorAttribute)
15+
paginator := template2.Default().Paginator().(*components.PaginatorAttribute)
1716

1817
pageInt, _ := strconv.Atoi(params.Page)
1918
pageSizeInt, _ := strconv.Atoi(params.PageSize)
@@ -38,13 +37,12 @@ func Get(path string, params parameter.Parameters, size int) types.PaginatorAttr
3837
paginator.CurPageEndIndex = strconv.Itoa((pageInt) * pageSizeInt)
3938
paginator.CurPageStartIndex = strconv.Itoa((pageInt - 1) * pageSizeInt)
4039
paginator.Total = strconv.Itoa(size)
41-
paginator.Option = map[string]template.HTML{
42-
"10": template.HTML(""),
43-
"20": template.HTML(""),
44-
"30": template.HTML(""),
45-
"50": template.HTML(""),
46-
"100": template.HTML(""),
40+
41+
paginator.Option = make(map[string]template.HTML, len(pageSizeList))
42+
for i := 0; i < len(pageSizeList); i++ {
43+
paginator.Option[pageSizeList[i]] = template.HTML("")
4744
}
45+
4846
paginator.Option[params.PageSize] = template.HTML("selected")
4947

5048
paginator.Pages = []map[string]string{}
@@ -188,5 +186,5 @@ func Get(path string, params parameter.Parameters, size int) types.PaginatorAttr
188186
paginator.Pages = pagesArr
189187
}
190188

191-
return paginator
189+
return paginator.SetPageSizeList(pageSizeList)
192190
}

plugins/admin/modules/parameter/parameter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ type Parameters struct {
1616
Fields map[string]string
1717
}
1818

19-
func GetParam(values url.Values) Parameters {
19+
func GetParam(values url.Values, defaultPageSize int, primaryKey, defaultSort string) Parameters {
2020
page := GetDefault(values, "__page", "1")
21-
pageSize := GetDefault(values, "__pageSize", "10")
22-
sortField := GetDefault(values, "__sort", "id")
23-
sortType := GetDefault(values, "__sort_type", "desc")
21+
pageSize := GetDefault(values, "__pageSize", strconv.Itoa(defaultPageSize))
22+
sortField := GetDefault(values, "__sort", primaryKey)
23+
sortType := GetDefault(values, "__sort_type", defaultSort)
2424
columns := GetDefault(values, "__columns", "")
2525

2626
fields := make(map[string]string)

plugins/admin/modules/table/table.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func (tb DefaultTable) GetDataFromDatabase(path string, params parameter.Paramet
448448
return PanelInfo{
449449
Thead: thead,
450450
InfoList: infoList,
451-
Paginator: paginator.Get(path, params, size),
451+
Paginator: paginator.Get(path, params, size, tb.info.GetPageSizeList()),
452452
Title: tb.info.Title,
453453
Description: tb.info.Description,
454454
}, nil
@@ -612,7 +612,7 @@ func (tb DefaultTable) GetDataFromDatabaseWithIds(path string, params parameter.
612612
return PanelInfo{
613613
Thead: thead,
614614
InfoList: infoList,
615-
Paginator: paginator.Get(path, params, size),
615+
Paginator: paginator.Get(path, params, size, tb.info.GetPageSizeList()),
616616
Title: tb.info.Title,
617617
Description: tb.info.Description,
618618
}, nil

template/chartjs/line.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ func (l *LineChart) GetContent() template.HTML {
494494
tmpl, defineName := l.GetTemplate()
495495

496496
if l.JsContentOptions != nil {
497-
fmt.Println("sfafadffasa")
498497
l.JsContent.Options = l.JsContentOptions
499498
}
500499

template/components/paninator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type PaginatorAttribute struct {
1515
Pages []map[string]string
1616
NextClass string
1717
NextUrl string
18+
PageSizeList []string
1819
Option map[string]template.HTML
1920
Url string
2021
types.Attribute
@@ -50,6 +51,11 @@ func (compo *PaginatorAttribute) SetPages(value []map[string]string) types.Pagin
5051
return compo
5152
}
5253

54+
func (compo *PaginatorAttribute) SetPageSizeList(value []string) types.PaginatorAttribute {
55+
compo.PageSizeList = value
56+
return compo
57+
}
58+
5359
func (compo *PaginatorAttribute) SetNextClass(value string) types.PaginatorAttribute {
5460
compo.NextClass = value
5561
return compo

template/template.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Template interface {
4040
Tree() types.TreeAttribute
4141
Tabs() types.TabsAttribute
4242
Alert() types.AlertAttribute
43+
4344
Paginator() types.PaginatorAttribute
4445
Popup() types.PopupAttribute
4546
Box() types.BoxAttribute

template/types/components.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type PaginatorAttribute interface {
103103
SetPreviousClass(value string) PaginatorAttribute
104104
SetPreviousUrl(value string) PaginatorAttribute
105105
SetPages(value []map[string]string) PaginatorAttribute
106+
SetPageSizeList(value []string) PaginatorAttribute
106107
SetNextClass(value string) PaginatorAttribute
107108
SetNextUrl(value string) PaginatorAttribute
108109
SetOption(value map[string]template.HTML) PaginatorAttribute

template/types/types.go

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/GoAdminGroup/go-admin/template/types/form"
1616
"html"
1717
"html/template"
18+
"strconv"
1819
"strings"
1920
)
2021

@@ -372,6 +373,9 @@ type InfoPanel struct {
372373

373374
Sort Sort
374375

376+
PageSizeList []int
377+
DefaultPageSize int
378+
375379
IsHideNewButton bool
376380
IsHideExportButton bool
377381
IsHideEditButton bool
@@ -385,8 +389,16 @@ type InfoPanel struct {
385389
FooterHtml template.HTML
386390
}
387391

392+
var DefaultPageSizeList = []int{10, 20, 30, 50, 100}
393+
394+
const DefaultPageSize = 10
395+
388396
func NewInfoPanel() *InfoPanel {
389-
return &InfoPanel{curFieldListIndex: -1}
397+
return &InfoPanel{
398+
curFieldListIndex: -1,
399+
PageSizeList: DefaultPageSizeList,
400+
DefaultPageSize: DefaultPageSize,
401+
}
390402
}
391403

392404
func (i *InfoPanel) AddField(head, field string, typeName db.DatabaseType) *InfoPanel {
@@ -490,6 +502,33 @@ func (i *InfoPanel) SetTable(table string) *InfoPanel {
490502
return i
491503
}
492504

505+
func (i *InfoPanel) SetPageSizeList(pageSizeList []int) *InfoPanel {
506+
i.PageSizeList = pageSizeList
507+
return i
508+
}
509+
510+
func (i *InfoPanel) SetDefaultPageSize(defaultPageSize int) *InfoPanel {
511+
i.DefaultPageSize = defaultPageSize
512+
return i
513+
}
514+
515+
func (i *InfoPanel) GetPageSizeList() []string {
516+
var pageSizeList = make([]string, len(i.PageSizeList))
517+
for j := 0; j < len(i.PageSizeList); j++ {
518+
pageSizeList[j] = strconv.Itoa(i.PageSizeList[j])
519+
}
520+
return pageSizeList
521+
}
522+
523+
func (i *InfoPanel) GetSort() string {
524+
switch i.Sort {
525+
case SortAsc:
526+
return "asc"
527+
default:
528+
return "desc"
529+
}
530+
}
531+
493532
func (i *InfoPanel) SetTitle(title string) *InfoPanel {
494533
i.Title = title
495534
return i
@@ -510,8 +549,13 @@ func (i *InfoPanel) SetDescription(desc string) *InfoPanel {
510549
return i
511550
}
512551

513-
func (i *InfoPanel) SetSort(sort Sort) *InfoPanel {
514-
i.Sort = sort
552+
func (i *InfoPanel) SetSortAsc() *InfoPanel {
553+
i.Sort = SortAsc
554+
return i
555+
}
556+
557+
func (i *InfoPanel) SetSortDesc() *InfoPanel {
558+
i.Sort = SortDesc
515559
return i
516560
}
517561

@@ -601,7 +645,7 @@ type FormPanel struct {
601645
FieldList FormFields
602646
curFieldListIndex int
603647

604-
// Warn: may be deprecated future.
648+
// Warn: may be deprecated in the future.
605649
TabGroups TabGroups
606650
TabHeaders TabHeaders
607651

0 commit comments

Comments
 (0)