forked from goffee/cup
add sample element paginator
This commit is contained in:
parent
368d6c8e6b
commit
51d76c2a94
2 changed files with 74 additions and 7 deletions
|
|
@ -643,11 +643,13 @@ func Themecontent(c *core.Context) *core.Response {
|
|||
|
||||
// first, include all compoments
|
||||
type templateData struct {
|
||||
ContentTable components.ContentTable
|
||||
ContentTabledetail components.ContentTabledetail
|
||||
ContentGraph components.ContentGraph
|
||||
FieldText components.FormInput
|
||||
FormSelectCityM components.FormSelect
|
||||
ContentTable components.ContentTable
|
||||
ContentTabledetail components.ContentTabledetail
|
||||
ContentGraph components.ContentGraph
|
||||
FieldText components.FormInput
|
||||
FormSelectCityM components.FormSelect
|
||||
Pagination components.ContentPagination
|
||||
ShouldShowPagination bool
|
||||
}
|
||||
|
||||
// for select options
|
||||
|
|
@ -699,7 +701,7 @@ func Themecontent(c *core.Context) *core.Response {
|
|||
var allTd [][]components.ContentTableTD
|
||||
//var vals []components.ContentTableTD
|
||||
// rows
|
||||
for i := 1; i <= 10; i++ {
|
||||
for i := 1; i <= 28; i++ {
|
||||
vals := make([]components.ContentTableTD, len(allTh))
|
||||
for b := 0; b < len(allTh)-2; b++ {
|
||||
vals[b].Value = fmt.Sprintf("%s%d%d", "TD data: ", i, b)
|
||||
|
|
@ -718,6 +720,46 @@ func Themecontent(c *core.Context) *core.Response {
|
|||
allTd = append(allTd, vals)
|
||||
}
|
||||
|
||||
// Pagination demo
|
||||
// start config
|
||||
limit := 10
|
||||
shouldShowPagination := false
|
||||
pageViewTableOffset := 0
|
||||
// Get the length of AllOptions
|
||||
totalrecords := len(allTd)
|
||||
// get current table offset
|
||||
tpage := c.RequestParamExists("tpage")
|
||||
if tpage {
|
||||
pageViewTableOffset, _ = strconv.Atoi(c.GetRequestParam("tpage").(string))
|
||||
}
|
||||
|
||||
// start default option paginator
|
||||
var pagination components.ContentPagination
|
||||
pagination.PageStartRecord = pageViewTableOffset + 1
|
||||
pagination.PageEndRecord = 0
|
||||
pagination.TotalRecords = 0
|
||||
pagination.PrevLink = ""
|
||||
pagination.NextLink = ""
|
||||
|
||||
// check current page
|
||||
// fake function to emulate a query offset
|
||||
newTd := getPaginatedPageViews(allTd, limit, pageViewTableOffset)
|
||||
|
||||
if len(newTd) > 0 {
|
||||
pagination.TotalRecords = totalrecords
|
||||
pagination.PageStartRecord = pageViewTableOffset + 1
|
||||
pagination.PageEndRecord = pageViewTableOffset + len(newTd)
|
||||
shouldShowPagination = totalrecords > limit
|
||||
}
|
||||
|
||||
if shouldShowPagination && pageViewTableOffset != 0 {
|
||||
pagination.PrevLink = fmt.Sprintf("/themecontent?tpage=%d", pageViewTableOffset-limit)
|
||||
}
|
||||
|
||||
if shouldShowPagination && pageViewTableOffset+limit < totalrecords {
|
||||
pagination.NextLink = fmt.Sprintf("/themecontent?tpage=%d", pageViewTableOffset+limit)
|
||||
}
|
||||
|
||||
// for td items in table detail
|
||||
var allTdetail []components.ContentTabledetailTD
|
||||
// table detail
|
||||
|
|
@ -772,7 +814,7 @@ func Themecontent(c *core.Context) *core.Response {
|
|||
ContentTable: components.ContentTable{
|
||||
ID: "table_demo",
|
||||
AllTH: allTh,
|
||||
AllTD: allTd,
|
||||
AllTD: newTd,
|
||||
},
|
||||
ContentTabledetail: components.ContentTabledetail{
|
||||
ID: "table_demodetail",
|
||||
|
|
@ -785,6 +827,8 @@ func Themecontent(c *core.Context) *core.Response {
|
|||
Labels: "Berlin|Paris|Venecia",
|
||||
Values: valuesgraph,
|
||||
},
|
||||
Pagination: pagination,
|
||||
ShouldShowPagination: shouldShowPagination,
|
||||
}
|
||||
|
||||
return c.Response.Template("custom_theme_contentpage.html", tmplData)
|
||||
|
|
@ -797,3 +841,23 @@ func Themecontent(c *core.Context) *core.Response {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
func getPaginatedPageViews(values [][]components.ContentTableTD, limit int, offset int) [][]components.ContentTableTD {
|
||||
|
||||
// Validate the offset and adjust if necessary
|
||||
if offset < 0 {
|
||||
offset = 0 // Ensure offset is not negative
|
||||
} else if offset >= len(values) {
|
||||
var emptytd [][]components.ContentTableTD
|
||||
return emptytd
|
||||
}
|
||||
|
||||
// Calculate the end index (limit the slice to the size of the array)
|
||||
end := offset + limit
|
||||
if end > len(values) {
|
||||
end = len(values) // Ensure end doesn't exceed the length of the array
|
||||
}
|
||||
|
||||
return values[offset:end] // Slice the array
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@
|
|||
<legend>Content demos</legend>
|
||||
<div class="row">
|
||||
{{template "content_table" .ContentTable}}
|
||||
{{if .ShouldShowPagination}}
|
||||
{{template "content_pagination" .Pagination}}
|
||||
{{end}}
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-lg-6">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue