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
|
// first, include all compoments
|
||||||
type templateData struct {
|
type templateData struct {
|
||||||
ContentTable components.ContentTable
|
ContentTable components.ContentTable
|
||||||
ContentTabledetail components.ContentTabledetail
|
ContentTabledetail components.ContentTabledetail
|
||||||
ContentGraph components.ContentGraph
|
ContentGraph components.ContentGraph
|
||||||
FieldText components.FormInput
|
FieldText components.FormInput
|
||||||
FormSelectCityM components.FormSelect
|
FormSelectCityM components.FormSelect
|
||||||
|
Pagination components.ContentPagination
|
||||||
|
ShouldShowPagination bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// for select options
|
// for select options
|
||||||
|
|
@ -699,7 +701,7 @@ func Themecontent(c *core.Context) *core.Response {
|
||||||
var allTd [][]components.ContentTableTD
|
var allTd [][]components.ContentTableTD
|
||||||
//var vals []components.ContentTableTD
|
//var vals []components.ContentTableTD
|
||||||
// rows
|
// rows
|
||||||
for i := 1; i <= 10; i++ {
|
for i := 1; i <= 28; i++ {
|
||||||
vals := make([]components.ContentTableTD, len(allTh))
|
vals := make([]components.ContentTableTD, len(allTh))
|
||||||
for b := 0; b < len(allTh)-2; b++ {
|
for b := 0; b < len(allTh)-2; b++ {
|
||||||
vals[b].Value = fmt.Sprintf("%s%d%d", "TD data: ", i, 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)
|
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
|
// for td items in table detail
|
||||||
var allTdetail []components.ContentTabledetailTD
|
var allTdetail []components.ContentTabledetailTD
|
||||||
// table detail
|
// table detail
|
||||||
|
|
@ -772,7 +814,7 @@ func Themecontent(c *core.Context) *core.Response {
|
||||||
ContentTable: components.ContentTable{
|
ContentTable: components.ContentTable{
|
||||||
ID: "table_demo",
|
ID: "table_demo",
|
||||||
AllTH: allTh,
|
AllTH: allTh,
|
||||||
AllTD: allTd,
|
AllTD: newTd,
|
||||||
},
|
},
|
||||||
ContentTabledetail: components.ContentTabledetail{
|
ContentTabledetail: components.ContentTabledetail{
|
||||||
ID: "table_demodetail",
|
ID: "table_demodetail",
|
||||||
|
|
@ -785,6 +827,8 @@ func Themecontent(c *core.Context) *core.Response {
|
||||||
Labels: "Berlin|Paris|Venecia",
|
Labels: "Berlin|Paris|Venecia",
|
||||||
Values: valuesgraph,
|
Values: valuesgraph,
|
||||||
},
|
},
|
||||||
|
Pagination: pagination,
|
||||||
|
ShouldShowPagination: shouldShowPagination,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Response.Template("custom_theme_contentpage.html", tmplData)
|
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>
|
<legend>Content demos</legend>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
{{template "content_table" .ContentTable}}
|
{{template "content_table" .ContentTable}}
|
||||||
|
{{if .ShouldShowPagination}}
|
||||||
|
{{template "content_pagination" .Pagination}}
|
||||||
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-6">
|
<div class="col-lg-6">
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue