Add pagination links
This commit is contained in:
+3
-1
@@ -34,7 +34,9 @@ fn paginated_details(name: String, conn: DbConn, user: Option<User>, page: Page)
|
||||
"posts": posts.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
||||
"authors": authors.into_iter().map(|u| u.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
||||
"n_authors": authors.len(),
|
||||
"n_articles": articles.len()
|
||||
"n_articles": articles.len(),
|
||||
"page": page.page,
|
||||
"n_pages": Page::total(articles.len() as i32)
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -61,6 +61,15 @@ impl Page {
|
||||
}
|
||||
}
|
||||
|
||||
/// Computes the total number of pages needed to display n_items
|
||||
pub fn total(n_items: i32) -> i32 {
|
||||
if n_items % ITEMS_PER_PAGE == 0 {
|
||||
n_items / ITEMS_PER_PAGE
|
||||
} else {
|
||||
(n_items / ITEMS_PER_PAGE) + 1
|
||||
}
|
||||
}
|
||||
|
||||
pub fn limits(&self) -> (i32, i32) {
|
||||
((self.page - 1) * ITEMS_PER_PAGE, self.page * ITEMS_PER_PAGE)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user