From be1dc3ceff6dff44e1f7b2a0afdc529be5cd93b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20P=C3=A9rez?= Date: Thu, 22 Aug 2024 23:46:47 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20a=C3=B1adido=20funci=C3=B3?= =?UTF-8?q?n=20de=20par=C3=A1metros=20de=20paginaci=C3=B3n=20y=20datos=20d?= =?UTF-8?q?e=20sesi=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages.go | 23 +++++++++++++++++++++++ render.go | 2 ++ 2 files changed, 25 insertions(+) diff --git a/pages.go b/pages.go index 3f58218..555a5c6 100644 --- a/pages.go +++ b/pages.go @@ -1,5 +1,10 @@ package gorender +import ( + "net/http" + "strconv" +) + // Pages contiene la información de paginación. type Pages struct { // totalElements son la cantidad de elementos totales a paginar. Pueden ser @@ -128,3 +133,21 @@ func PaginateArray[T any](items []T, currentPage, itemsPerPage int) []T { return items[startIndex:endIndex] } + +func PaginationParams(r *http.Request) (int, int, int) { + limit := r.FormValue("limit") + if limit == "" { + limit = "50" + } + page := r.FormValue("page") + if page == "" || page == "0" { + page = "1" + } + + limitInt, _ := strconv.Atoi(limit) + pageInt, _ := strconv.Atoi(page) + offset := (pageInt - 1) * limitInt + actualPage := offset/limitInt + 1 + + return limitInt, offset, actualPage +} diff --git a/render.go b/render.go index f452270..a43a44b 100644 --- a/render.go +++ b/render.go @@ -32,6 +32,8 @@ type OptionFunc func(*Render) type TemplateData struct { Data map[string]interface{} + // SessionData contiene los datos de la sesión del usuario. + SessionData interface{} // FeedbackData tiene como función mostrar los mensajes habituales de // información, advertencia, éxito y error. No va implícitamente relacionado // con los errores de validación de formularios pero pueden ser usados para