Add pagination links

This commit is contained in:
Bat
2018-07-25 14:29:34 +02:00
parent 5549e4d0e5
commit 04dac6f87f
8 changed files with 47 additions and 1 deletions
+3 -1
View File
@@ -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)
}))
})
}
+9
View File
@@ -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)
}