dp815292 2019-04-23 17:05
浏览 63
已采纳

在Go中显示分页小部件

I am using HTML templating in Go to render a pagination widget. I am trying to follow an example of how to do it from here: https://www.solodev.com/blog/web-design/adding-pagination-to-your-website.stml

This is my code so far:

// Item size like call 2 item from database
var pagesize = 2
var PaginationSize int = 6
Pagination := make([]int, PaginationSize)

for i := 0; i < PaginationSize; i++ {
    if RequestPageNumber <= page {
        Pagination[i] = i + 1
    } else {
        Pagination[i] = RequestPageNumber + i
    }

    // Make it active
    if  i == 0 {
        Info.Pagination += template.HTML(fmt.Sprintf(`<li class="page-item active"><a class="page-link" href=?id=%v>%v</a></li>`,RequestPageNumber + i ,RequestPageNumber + i))
    } else {
        Info.Pagination += template.HTML(fmt.Sprintf(`<li class="page-item"><a class="page-link" href=?id=%v>%v</a></li>`,RequestPageNumber + i ,RequestPageNumber + i))
    }
}

I would like to improve this by making the pagination list update only when the active page becomes a multiple of PaginationSize. For example, when the displayed pagination shows

(1), 2, 3, 4, 5, 6

If the user clicks on 2, I would like to see

1, (2), 3, 4, 5, 6

But my current code displays

(2), 3, 4, 5, 6, 7

After the page has advanced past 6, the page list should look like

7, 8, 9, 10, 11, 12 ,13 ,14

The data wrapped in this pagination view may be in a database, so I'd also appreciate tips on how to make this work with a database, if possible.

Thanks.

  • 写回答

1条回答 默认 最新

  • duanpu2272 2019-04-23 17:49
    关注

    Check next solution

    package main
    
    import (
        "fmt"
    )
    
    const PSIZE = 5
    
    func pager(pageNo int) {
        var start int = pageNo/(PSIZE-1)*(PSIZE-1) + 1
        if pageNo%(PSIZE-1) == 0 {
            start -= PSIZE - 1
        }
    
        for i := start; i < start+PSIZE; i++ {
            if i == pageNo {
                fmt.Printf("(%d) ", i)
            } else {
                fmt.Printf("%d ", i)
            }
        }
        fmt.Print("
    ")
    }
    
    func main() {
        pager(4)
        pager(8)
        pager(9)
    }
    

    Output

    1 2 3 (4) 5 
    5 6 7 (8) 9 
    (9) 10 11 12 13 
    

    https://play.golang.org/p/rFslGe2OE0k

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常