Add selector for number of items per page on search view
This commit is contained in:
parent
d7b207159b
commit
e9dba080a7
2 changed files with 33 additions and 10 deletions
33
search.go
33
search.go
|
@ -26,12 +26,13 @@ func buildQuery(q string) bson.M {
|
|||
}
|
||||
|
||||
type searchData struct {
|
||||
S Status
|
||||
Found int
|
||||
Books []Book
|
||||
Page int
|
||||
Next string
|
||||
Prev string
|
||||
S Status
|
||||
Found int
|
||||
Books []Book
|
||||
ItemsPage int
|
||||
Page int
|
||||
Next string
|
||||
Prev string
|
||||
}
|
||||
|
||||
func searchHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||
|
@ -48,19 +49,21 @@ func searchHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
|||
page = 0
|
||||
}
|
||||
}
|
||||
res, num, _ := db.GetBooks(buildQuery(req), SEARCH_ITEMS_PAGE, page*SEARCH_ITEMS_PAGE)
|
||||
items_page := itemsPage(r)
|
||||
res, num, _ := db.GetBooks(buildQuery(req), items_page, page*items_page)
|
||||
|
||||
var data searchData
|
||||
data.S = GetStatus(w, r)
|
||||
data.S.Search = req
|
||||
data.Books = res
|
||||
data.ItemsPage = items_page
|
||||
data.Found = num
|
||||
data.Page = page + 1
|
||||
if num > (page+1)*SEARCH_ITEMS_PAGE {
|
||||
data.Next = "/search/?q=" + req + "&p=" + strconv.Itoa(page+1)
|
||||
if num > (page+1)*items_page {
|
||||
data.Next = "/search/?q=" + req + "&p=" + strconv.Itoa(page+1) + "&num=" + strconv.Itoa(items_page)
|
||||
}
|
||||
if page > 0 {
|
||||
data.Prev = "/search/?q=" + req + "&p=" + strconv.Itoa(page-1)
|
||||
data.Prev = "/search/?q=" + req + "&p=" + strconv.Itoa(page-1) + "&num=" + strconv.Itoa(items_page)
|
||||
}
|
||||
|
||||
format := r.Form["fmt"]
|
||||
|
@ -70,3 +73,13 @@ func searchHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
|||
loadTemplate(w, "search", data)
|
||||
}
|
||||
}
|
||||
|
||||
func itemsPage(r *http.Request) int {
|
||||
if len(r.Form["num"]) > 0 {
|
||||
items_page, err := strconv.Atoi(r.Form["num"][0])
|
||||
if err == nil {
|
||||
return items_page
|
||||
}
|
||||
}
|
||||
return SEARCH_ITEMS_PAGE
|
||||
}
|
||||
|
|
|
@ -50,7 +50,17 @@
|
|||
<li class="previous">
|
||||
<a href="{{.Prev}}">← Prev</a>
|
||||
</li>
|
||||
{{else}}
|
||||
<div class="span1"></div>
|
||||
{{end}}
|
||||
<form class="form-inline span9 centered">
|
||||
<label>Books per page: </label>
|
||||
<div class="input-append" action="/search/">
|
||||
<input class="span1" name="num" type="text" value="{{.ItemsPage}}" />
|
||||
<button type="submit" class="btn">Ok</button>
|
||||
</div>
|
||||
<input class="hidden span1" name="q" type="text" value="{{.S.Search}}" />
|
||||
</form>
|
||||
{{if .Next}}
|
||||
<li class="next">
|
||||
<a href="{{.Next}}">Next →</a>
|
||||
|
|
Reference in a new issue