gofiber: 模板: 分页功能模块

一,代码

1,模块

package page

import "fmt"


type Page struct {
	//定义分页的struct
	Total int `json:"total"`
	TotalPage int `json:"totalpage"`
	CurrentPage int `json:"currentpage"`
	PrevPage int `json:"prevpage"`
	NextPage int `json:"nextpage"`
	ListPage []int `json:"listpage"`
}

func GetPageInfo(total,size,currentPage int) *Page {
    //创建对象
    pageOne := &Page{}
    //items总数
	pageOne.Total = total
    //当前页
	pageOne.CurrentPage = currentPage
    //总页数
	if size == 0 {
		pageOne.TotalPage = 0
	}
	totalPage := total / size
	if total%size > 0 {
		totalPage++
	}
	pageOne.TotalPage = totalPage
	fmt.Println("TotalPage:",totalPage)
    //得到要列出的页码,
    begin:=1
    end:=1
    if totalPage<=10 {
    	begin = 1
    	end = totalPage
	} else {
		if currentPage-5<=0 {
			begin = 1
			end = 10
		} else {
			if totalPage-currentPage<=5 {
				end = totalPage
				begin = totalPage-9
			} else {
				begin = currentPage-5
				end = begin+9
			}
		}
	}

	fmt.Println("end:",end)

	//添加到切片
	listOne := []int{}

	for i := begin; i <= end; i++ {
		fmt.Println(i)
		listOne = append(listOne,i)
	}

	fmt.Println(listOne)
	pageOne.ListPage = listOne

	//得到之前一页和之后一页

	next:=currentPage+1
	if next > totalPage {
		next = totalPage
	}

	prev:=currentPage-1
	if prev < 1 {
		prev = 1
	}

	pageOne.PrevPage = prev
	pageOne.NextPage = next

	return pageOne
}

2,控制器

package controller

import (
	"fmt"
	"github.com/gofiber/fiber/v2"
	"industry/service"
	"strconv"
)

type TestController struct{}

func NewTestController() *TestController {
	return &TestController{}
}

func (dc *TestController) Test(c *fiber.Ctx) error {

	p := c.Query("p", "1")

	bid := 2
	cid := 0
    size:=20

	page, _ := strconv.Atoi(p)

	pageOne := service.GetPageInfo(bid,cid,size,page)

	myMap:=fiber.Map{}
	myMap["Title"] = "书店新上图书列表"
	myMap["page"] = pageOne
	myMap["base"] = "/test/test?"
	fmt.Println(myMap)
	return c.Render("test/test", myMap)
}

二,模板:

定义模板

<div class="page" style="margin-top: 30px;">
  {{$page := .page}}
  {{$base := .base}}
  <ul class="pagination">
    <li>
      <span class="rows">共 {{.page.Total}} 条记录</span></li>
    {{if ne 1 $page.CurrentPage}}
    <li><a class="next" href="{{$base}}&p={{.page.PrevPage}}"> << </a></li>
    {{end}}
    {{range $key, $value := .page.ListPage}}

    {{if eq $value $page.CurrentPage}}
    <li class="active"><span class="current">{{$value}}</span></li>
    {{else}}
    <li><a class="num" href="{{$base}}&p={{$value}}">{{$value}}</a></li>
    {{end}}

    {{end}}
    {{if ne $page.TotalPage $page.CurrentPage}}
    <li><a class="next" href="{{$base}}&p={{.page.NextPage}}">>></a></li>
    {{end}}
    <li><a class="end" href="{{$base}}&p={{.page.TotalPage}}">...{{.page.TotalPage}}</a></li>
  </ul>
</div>

调用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="renderer" content="webkit" />
    <title>新闻资讯 - 官方网站!</title>
    <link rel="shortcut icon" type="image/x-icon" href="/favicon.png" media="screen" />
    <link type="text/css" rel="stylesheet" href="/Font/css/font-awesome.min.css" />
    <link type="text/css" rel="stylesheet" href="/Animate/animate.css" />

</head>
<body>

{{template "partials/page" .}}

</body>
</html>

三,测试效果

posted @ 2024-11-23 09:17  刘宏缔的架构森林  阅读(4)  评论(0编辑  收藏  举报