8000 listing: fix pagination jitter and add PaginationTotalVisible method by molon · Pull Request #470 · qor5/admin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

listing: fix pagination jitter and add PaginationTotalVisible method #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/docsrc/examples/examples_admin/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
}

func ListingExample(b *presets.Builder, db *gorm.DB) http.Handler {
return listingExample(b, db, nil)

Check warning on line 68 in docs/docsrc/examples/examples_admin/listing.go

View check run for this annotation

Codecov / codecov/patch

docs/docsrc/examples/examples_admin/listing.go#L68

Added line #L68 was not covered by tests
}

func listingExample(b *presets.Builder, db *gorm.DB, customize func(mb *presets.ModelBuilder)) http.Handler {
db.AutoMigrate(&Post{}, &Category{})

// Setup the project name, ORM and Homepage
b.DataOperator(gorm2op.DataOperator(db))

Expand Down Expand Up @@ -120,6 +126,10 @@
return h.Td(h.Text("virtual field"))
})

if customize != nil {
customize(postModelBuilder)
}

b.Model(&Category{})
// Use m to customize the model, Or config more models here.
return b
Expand Down
94 changes: 94 additions & 0 deletions docs/docsrc/examples/examples_admin/listing_test.go
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package examples_admin

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/qor5/admin/v3/presets"
"github.com/qor5/web/v3/multipartestutils"
"github.com/theplant/gofixtures"
)

var dataSeedForListing = gofixtures.Data(gofixtures.Sql(`
INSERT INTO "public"."posts" ("id", "title", "body", "updated_at", "created_at", "disabled", "status", "category_id") VALUES ('2', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('3', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('4', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('5', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('6', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('7', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('8', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('9', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('10', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('11', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('12', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('13', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('14', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('15', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('16', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('17', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('18', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('19', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('20', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('21', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('22', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0'),
('23', 'post0title', 'post0body', '0001-12-31 15:54:17+00 BC', '0001-12-31 15:54:17+00 BC', 'f', 'active', '0');
`, []string{"posts"}))

var dataEmptyForListing = gofixtures.Data(gofixtures.Sql(``, []string{"posts"}))

func TestListingExample(t *testing.T) {
dbr, _ := TestDB.DB()
TestDB.AutoMigrate(&Post{}, &Category{})

cases := []multipartestutils.TestCase{
{
Name: "empty List",
Debug: true,
HandlerMaker: func() http.Handler {
return listingExample(presets.New(), TestDB, func(mb *presets.ModelBuilder) {
})
},
ReqFunc: func() *http.Request {
dataEmptyForListing.TruncatePut(dbr)
return httptest.NewRequest("GET", "/posts", nil)
},
ExpectPageBodyContainsInOrder: []string{"No records to show"},
},
{
Name: "not empty List",
Debug: true,
HandlerMaker: func() http.Handler {
return listingExample(presets.New(), TestDB, func(mb *presets.ModelBuilder) {
})
},
ReqFunc: func() *http.Request {
dataSeedForListing.TruncatePut(dbr)
return httptest.NewRequest("GET", "/posts", nil)
},
ExpectPageBodyNotContains: []string{"No records to show"},
ExpectPageBodyContainsInOrder: []string{"v-pagination", ":total-visible='5'"},
},
{
Name: "PaginationTotalVisible 2",
Debug: true,
HandlerMaker: func() http.Handler {
return listingExample(presets.New(), TestDB, func(mb *presets.ModelBuilder) {
mb.Listing().PaginationTotalVisible(2)
})
},
ReqFunc: func() *http.Request {
dataSeedForListing.TruncatePut(dbr)
return httptest.NewRequest("GET", "/posts", nil)
},
ExpectPageBodyNotContains: []string{"No records to show"},
ExpectPageBodyContainsInOrder: []string{"v-pagination", ":total-visible='2'"},
},
}

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
multipartestutils.RunCase(t, c, nil)
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/pquerna/otp v1.4.0
github.com/qor/oss v0.0.0-20240729105053-88484a799a79
github.com/qor5/web/v3 v3.0.6-0.20240808073627-bd2003d12e2c
github.com/qor5/x/v3 v3.0.7-0.20240812140152-73dc2a1abd31
github.com/qor5/x/v3 v3.0.7-0.20240813080103-4016555c8638
github.com/samber/lo v1.46.0
github.com/shurcooL/sanitized_anchor_name v1.0.0
github.com/spf13/cast v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ github.com/qor5/web v1.3.2 h1:zw796YJeDLe8vRwGR1cM+uS1ZuSkPutchBEXv2GgOhI=
github.com/qor5/web v1.3.2/go.mod h1:LszskQJbFQDJwOeZC6j6afOiHxxyjrzz8B3zuBwfgKQ=
github.com/qor5/web/v3 v3.0.6-0.20240808073627-bd2003d12e2c h1:uYoko94cUQN04E2hnZIsp/+yiEEI97V5OKcbS9cTa7o=
github.com/qor5/web/v3 v3.0.6-0.20240808073627-bd2003d12e2c/go.mod h1:32vdHHcZb2JimlcaclW9hLUyimdXjrllZDHTh3rl6d0=
github.com/qor5/x/v3 v3.0.7-0.20240812140152-73dc2a1abd31 h1:NCuNe/EB1AKK7+Eub9IEPlIkEqIvtL91U+MtFFCksI4=
github.com/qor5/x/v3 v3.0.7-0.20240812140152-73dc2a1abd31/go.mod h1:p8LBZGmJ9pozop2jMACV5tN/1gkpAskteOhlwcfFxpw=
github.com/qor5/x/v3 v3.0.7-0.20240813080103-4016555c8638 h1:XyT7HQ+HV7vu5hm2OnDBMdL92iSFyME57VLP2eXw6Ew=
github.com/qor5/x/v3 v3.0.7-0.20240813080103-4016555c8638/go.mod h1:p8LBZGmJ9pozop2jMACV5tN/1gkpAskteOhlwcfFxpw=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
Expand Down
8000 9 changes: 8 additions & 1 deletion presets/listing_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ type ListingBuilder struct {
// 1. the pagination component will not display on listing page.
// 2. the perPage will actually be ignored.
// 3. all data will be returned in one page.
disablePagination bool
disablePagination bool
paginationTotalVisible int64

orderBy string
orderableFields []*OrderableField
Expand All @@ -71,6 +72,7 @@ type ListingBuilder struct {
dialogHeight string
keywordSearchOff bool
columnsProcessor ColumnsProcessor

FieldsBuilder

once sync.Once
Expand Down Expand Up @@ -130,6 +132,11 @@ func (b *ListingBuilder) DisablePagination(v bool) (r *ListingBuilder) {
return b
}

func (b *ListingBuilder) PaginationTotalVisible(v int64) (r *ListingBuilder) {
b.paginationTotalVisible = v
return b
}

func (b *ListingBuilder) SearchFunc(v SearchFunc) (r *ListingBuilder) {
b.Searcher = v
return b
Expand Down
1 change: 1 addition & 0 deletions presets/listing_compo.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ func (c *ListingCompo) dataTable(ctx context.Context) h.HTMLComponent {
CustomPerPages([]int64{c.lb.perPage}).
PerPageText(msgr.PaginationRowsPerPage).
NoOffsetPart(true).
TotalVisible(cmp.Or(c.lb.paginationTotalVisible, int64(5))).
OnSelectPerPage(stateful.ReloadAction(ctx, c,
func(target *ListingCompo) {
target.Page = 0
Expand Down
Loading
0