I want to do pagination in Golang frontend. I am thinking the following.
First I have to get the current url. Then parse it. Add the page url values to the url and send it to the template.
How do I get the url?
Do I need to build it from *http.Request?
Or is it already available?
More context should be helpful. This is my attempt to understand your question (a shot in the dark):
func endpoint(w http.ResponseWriter, r *http.Request) {
module, mode, val := getpath(r.URL.Path)
}
// split url
func getpath(path string) (module, mode, val string) {
parts := strings.Split(path, "/")
switch len(parts) {
case 4:
val = parts[3]
fallthrough
case 3:
mode = parts[2]
fallthrough
case 2:
module = parts[1]
}
return // Named return values are used, so just return here
}
I display the users list in html table. (using go template and range)
I want to add pagination for that.
I just want to implement next and previous buttons.
The url already has query strings sometimes and plain url sometimes.
So how do I add either <a href="?page=4"<a href="&page=4"
How do I know the url has some query strings already?
I was thinking that, if I can get the current url, I can parse it and add values.add("page", "2")
Am I wrong?
In simple, I just want the next button to have next page and previous button to have previous page
I am about to implement this for my own as soon i got the time. But I am going to use “infinitive scrolling” instead (about the same as pagination but with automatic page loading). I have no I idea how to do this just now. Other than using agGrid or similar Javascript libraries with this built in functions. But I do not like Javascript that much, so I will try the Go approach.
My goal is to do server side infinite scrolling. I do not know if this is possible, but I want to give it a try.
Maybe in a couple of weeks, I can give you a better answer.
For now I will let Go fetch all records and render the page att loading. A few hundred records this will be enough.
I guess you simply add this to the “next” button in some way. But I guess you have to store the “current” page number in sessionStorage and increment it or similar. E g sessionStorage.page+1
Since you can’t scroll on the server, there will have to be a client-side element to this. The client side code isn’t actually that complicated. Back in the day it was because browser compatibility was a nightmare (which is why we used things like jQuery) but these days it is actually relatively simple. Do a search for “pure JavaScript infinite scroll” and you should find many examples you can build on.
Right - that’s the client-side aspect of it I mentioned. You can certainly render on the server and then return a chunk of HTML to the client and append it to an element in the DOM.
Yes. I got the query string using the above code.
But
page := r.URL.Query().Get("page")
I convert the page to int.
//lets assume there is always a page query string with some int value
pageInt, _ := strconv.Atoi(page)
//after conforming pageInt is actually a int.
//for simplicity, lets assume there is a next page always
nextPage := pageInt + 1
in the template page
//which url do I use now?
<a href="?page={{.Next}}">Next</a>
//This is not a good idea. If the url already has some query string, then it will look like this
//http://someurl.com/somepage?somequery=somevalue?page=2
<a href="&page={{.Next}}">Next</a>
//This is also not right,
//If the url has no query string, then it will be like this
//http://someuser.com/somepage&page=2
So I have to pass the entire url for next button from go code. Am I right?
Or is there a way to do this simply?
Something like this (pseudo code not tested). I think you may store the current page number in sessionStorage using Javascript as well. The incremented page should be sent from browser IMO.
Thank you so much. I have an unrelated question. Please guide me.
Golang does not have in built session manager. What is your recommendation? Do I have to create one myself? or use third party library? Which library do you use and recommend?
This is a bit harder. If you ask 10 persons, you may get 10 different answers. I can assure you that others have different opinions. And I look forward to hear them But this is how I interpret session handling:
Use an authentication server that produces an uuid as session_id and store the session_id in a database for a limited time (days).
Retrieve this session_id and store on the web server in some form of cache. Something like cache2go. Set the cache to expire in x hours
Make a session cookie in the browser with same expire time as the server cache.
When the user send an url to the web server, check that this cookie exists in the cache.
Retrieve necessary data based on this session cookie from the authentication server OR already stored in the session cache.
My opinion is that a session_id is harmless to store in the browser. As all other processes are handled by the web server and/or authentication server. And if the session cookie is manipulated, the session will fail, as the new cookie does not match with the cached version in the web server.
By using a cached session_id, there will be less data traffic to the authentication server.