Skip to content

Commit

Permalink
fix: use children for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Jun 21, 2024
1 parent 5fa10f4 commit 417f801
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
44 changes: 24 additions & 20 deletions components/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"fmt"
"math"

"github.com/gofiber/fiber/v2"
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/forms"
"github.com/zeiss/fiber-htmx/components/utils"
"gorm.io/gorm"

"github.com/gofiber/fiber/v2"
htmx "github.com/zeiss/fiber-htmx"
)

Expand Down Expand Up @@ -84,6 +84,28 @@ func (p *Results[T]) GetLen() int {
return len(p.Rows)
}

// FromContext returns the results from the context.
func FromContext[T any](c *fiber.Ctx) (Results[T], error) {
var result Results[T]

limit, err := c.ParamsInt("limit", 10)
if err != nil {
return result, err
}
result.Limit = limit

offset, err := c.ParamsInt("offset", 0)
if err != nil {
return result, err
}
result.Offset = offset

result.Search = c.Params("search")
result.Sort = c.Params("sort")

return result, nil
}

// PaginatedResults returns a function that paginates the results.
func PaginatedResults[T any](value interface{}, pagination *Results[T], db *gorm.DB) func(db *gorm.DB) *gorm.DB {
var totalRows int64
Expand Down Expand Up @@ -257,7 +279,6 @@ func TableToolbar(p TableToolbarProps, children ...htmx.Node) htmx.Node {
// TablePaginationProps is a struct that contains the properties of a table pagination
type TablePaginationProps struct {
ClassNames htmx.ClassNames
Pagination htmx.Node
}

// TablePagination is a component that renders a table pagination
Expand All @@ -281,16 +302,7 @@ func TablePagination(p TablePaginationProps, children ...htmx.Node) htmx.Node {
"lg:space-x-8": true,
},
),
htmx.P(
htmx.Merge(
htmx.ClassNames{
"text-sm": true,
"font-medium": true,
},
),
htmx.Text("Rows per page"),
),
p.Pagination,
htmx.Group(children...),
),
)
}
Expand Down Expand Up @@ -368,11 +380,3 @@ func Table[S ~[]R, R Row](p TableProps, columns Columns[R], s S) htmx.Node {
p.Pagination,
)
}

// PaginationFromContext returns a new Pagination object based on the provided context.
func PaginationPropsFromContext(c *fiber.Ctx) (limit int, offset int) {
limit = c.QueryInt("limit", 10)
offset = c.QueryInt("offset", 0)

return
}
26 changes: 8 additions & 18 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,24 +328,14 @@ func (c *exampleController) Get() error {
tables.Table(
tables.TableProps{
Pagination: tables.TablePagination(
tables.TablePaginationProps{
Pagination: tables.Pagination(
tables.PaginationProps{
Total: len(demoRows),
Offset: 0,
Limit: 10,
},
// tables.Select(
// tables.SelectProps{
// Total: len(demoRows),
// Offset: 0,
// Limit: 10,
// Limits: tables.DefaultLimits,
// URL: "/api/data",
// },
// ),
),
},
tables.TablePaginationProps{},
tables.Pagination(
tables.PaginationProps{
Total: len(demoRows),
Offset: 0,
Limit: 10,
},
),
),
},
tables.Columns[DemoRow]{
Expand Down

0 comments on commit 417f801

Please sign in to comment.