majidarif majidarif - 1 year ago 47
HTML Question

Procedurally generating pagination

I'm trying to create pagination with Go but I'm a bit confused. It's my first time to creating pagination as I used to use laravel's helper class when I was still using PHP.

I tried doing something like:

var totalPages = int(math.Ceil(float64(totalRecords) / float64(recordsPerPage)))

for i := 0; i < totalPages; i++ {
pages[i] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, i+1, limit, i+1)
}


And that shows all the pages, I want to create something that would look like:

< 1 2 ... 20 24 25 26 27 ... 200 201 >


25 being current page and 201 being the last page.

I also experimented with something like the following but was quirky on some cases like if the page is close to the start or the end:

// pages[0] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, 1, limit, 1)
// pages[1] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, 2, limit, 2)
// pages[2] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, 3, limit, 3)
// pages[3] = `<li><a class="more">&hellip;</a></li>`
// pages[4] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, page, limit, page)
// pages[5] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, page+1, limit, page+1)
// pages[6] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, page+2, limit, page+2)
// pages[7] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, page+3, limit, page+3)
// pages[8] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, page+4, limit, page+4)
// pages[9] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, page+5, limit, page+5)
// pages[10] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, page+6, limit, page+6)
// pages[11] = `<li><a class="more">&hellip;</a></li>`
// pages[12] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, totalPages-1, limit, totalPages-1)
// pages[13] = fmt.Sprintf(`<li><a href="?page=%d&limit=%d">%d</a></li>`, totalPages, limit, totalPages)


So the question is, how do I achieve this? Is there a library? What is the correct logic?

Answer Source

Break down your problem and you will get it.

What you want is to compute the neighbors.

package main

import (
    "fmt"
)

func main() {
    pages(8, 2, 13)
}

func pages(n int, around int, count int) {
    first := n - around
    if first < 1 {
        first = 1
    }
    last := n + around
    if last > count {
        last = count
    }

    if first > 1 {
        for i := 1; i <= 2 && i < first; i++ {
            fmt.Println(i)
        }
        if 3 < first {
            fmt.Println("...")
        }
    }

    for i := first; i <= last; i++ {
        fmt.Println(i)
    }

    if last < count {
        if last <= count-3 {
            fmt.Println("...")
        }
        end := count - 1
        if end <= last {
            end = last + 1
        }
        for i := end; i <= count; i++ {
            fmt.Println(i)
        }

    }
}

Improvement : Make number of "prefix" and "suffix" pages variable ;-)

See: https://play.golang.org/p/lue7ZAVjes

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download