Skip to content

Instantly share code, notes, and snippets.

@devn

devn/shame.clj Secret

Created May 25, 2013 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devn/1688d08c688d6c6ed431 to your computer and use it in GitHub Desktop.
Save devn/1688d08c688d6c6ed431 to your computer and use it in GitHub Desktop.
(defn pagination [q page-num]
(let [num (Integer/parseInt page-num)
total-hits (get-num-hits q page-num)
num-pages (/ total-hits 25)
prev-page-num (dec num)
next-page-num (inc num)]
(when-not (< num-pages 1)
[:div#pagination
;; Make previous page link so long as we're not on the first
;; page.
(when-not (= 0 num)
[:div.prev-links
(link-to {:class "first-page"}
(str "/search?" (generate-query-string {"q" q "num" 0}))
"<<- ")
(link-to {:class "prev"}
(str "/search?" (generate-query-string {"q" q "num" prev-page-num}))
"<- ")])
;; Create all pagination links
;; <<- <- 1 2 3 4 5 6 7 8 9 10 -> ->>
;; <<- <- 23 24 25 26 27 28 29 30 31 [32] -> ->>
(let [page-links (map (fn [p-num] (link-to {:class "page_num"}
(str "/search?"
(generate-query-string {"q" q "num" p-num}))
;; Even though we're
;; indexed by 0 in
;; the query string,
;; show the user a
;; 1-based link.
(inc p-num)))
;; This will only show us the current
;; page we're on up to the total page
;; count.
(range num num-pages))
num-page-links (count page-links)]
;; If there less than 10 pages
(if (< num-page-links 10)
;; Show all of the links
page-links
;; Otherwise only show 10
(take 10 page-links)))
;; When the next page number is less than or equal to the
;; total number of pages
(when (<= next-page-num num-pages)
;; Make a next link
[:div.next-links
(link-to {:class "next"}
(str "/search?" (generate-query-string {"q" q "num" next-page-num}))
" ->")
(link-to {:class "last-page"}
(str "/search?" (generate-query-string {"q" q "num" (-> (Math/floor num-pages)
int
str)}))
" ->>")])])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment