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

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

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?