From e5bdbf5d8d1668eb9acf2a32c0cddacd75a52c07 Mon Sep 17 00:00:00 2001 From: manuel Date: Sun, 8 Jan 2023 15:45:28 +0100 Subject: [PATCH] fix pagination --- actix_admin_macros/src/lib.rs | 9 +- examples/basic/entity/mod.rs | 11 +-- examples/basic/entity/post.rs | 5 +- src/routes/list.rs | 14 ++- templates/base.html | 3 - templates/head.html | 28 +++++- templates/list.html | 156 ++++++++++++++++++---------------- templates/loader.html | 5 ++ 8 files changed, 140 insertions(+), 91 deletions(-) create mode 100644 templates/loader.html diff --git a/actix_admin_macros/src/lib.rs b/actix_admin_macros/src/lib.rs index d024da7..9058116 100644 --- a/actix_admin_macros/src/lib.rs +++ b/actix_admin_macros/src/lib.rs @@ -246,16 +246,17 @@ pub fn derive_actix_admin_model(input: proc_macro::TokenStream) -> proc_macro::T .order_by_asc(Column::Id) .paginate(db, posts_per_page); let num_pages = paginator.num_pages().await?; - let entities = paginator - .fetch_page(page - 1) - .await?; let mut model_entities = Vec::new(); + if (num_pages == 0) { return Ok((num_pages, model_entities)) }; + let entities = paginator + .fetch_page(std::cmp::min(num_pages - 1, page - 1)) + .await?; for entity in entities { model_entities.push( ActixAdminModel::from(entity) ); } - + Ok((num_pages, model_entities)) } diff --git a/examples/basic/entity/mod.rs b/examples/basic/entity/mod.rs index d3825f1..c7851e4 100644 --- a/examples/basic/entity/mod.rs +++ b/examples/basic/entity/mod.rs @@ -5,7 +5,7 @@ pub mod comment; pub mod post; pub use comment::Entity as Comment; pub use post::Entity as Post; -use sea_orm::prelude::DateTime; +use chrono::{Local}; use sea_orm::prelude::Decimal; // setup @@ -29,7 +29,7 @@ pub async fn create_post_table(db: &DbConn) -> Result { .col(ColumnDef::new(post::Column::Text).string().not_null()) .col(ColumnDef::new(post::Column::TeaMandatory).string().not_null()) .col(ColumnDef::new(post::Column::TeaOptional).string()) - .col(ColumnDef::new(post::Column::InsertDate).date()) + .col(ColumnDef::new(post::Column::InsertDate).date().not_null()) .col(ColumnDef::new(post::Column::Attachment).string()) .to_owned(); @@ -63,23 +63,24 @@ pub async fn create_post_table(db: &DbConn) -> Result { let res = create_table(db, &stmt).await; - for i in 1..101 { + for i in 1..1000 { let row = post::ActiveModel { title: Set(format!("Test {}", i)), text: Set("some content".to_string()), tea_mandatory: Set(post::Tea::EverydayTea), tea_optional: Set(None), + insert_date: Set(Local::now().date_naive()), ..Default::default() }; let _res = Post::insert(row).exec(db).await; } - for i in 1..101 { + for i in 1..1000 { let row = comment::ActiveModel { comment: Set(format!("Test {}", i)), user: Set("me@home.com".to_string()), my_decimal: Set(Decimal::new(105, 0)), - insert_date: Set(DateTime::default()), + insert_date: Set(Local::now().naive_utc()), is_visible: Set(i%2 == 0), ..Default::default() }; diff --git a/examples/basic/entity/post.rs b/examples/basic/entity/post.rs index 18fbe2f..a49468e 100644 --- a/examples/basic/entity/post.rs +++ b/examples/basic/entity/post.rs @@ -21,15 +21,16 @@ pub struct Model { pub tea_mandatory: Tea, #[actix_admin(select_list="Tea")] pub tea_optional: Option, + #[sea_orm(column_type = "Date")] pub insert_date: Date, #[actix_admin(file_upload)] - pub attachment: String + pub attachment: Option } impl Display for Model { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { match &*self { - _ => write!(formatter, "{} {}", &self.title, &self.insert_date), + _ => write!(formatter, "{} {}", &self.title, ""/* &self.insert_date*/), } } } diff --git a/src/routes/list.rs b/src/routes/list.rs index 0dd7507..4e32980 100644 --- a/src/routes/list.rs +++ b/src/routes/list.rs @@ -44,7 +44,7 @@ pub async fn list( let params = web::Query::::from_query(req.query_string()).unwrap(); - let page = params.page.unwrap_or(1); + let mut page = params.page.unwrap_or(1); let entities_per_page = params .entities_per_page .unwrap_or(DEFAULT_ENTITIES_PER_PAGE); @@ -58,13 +58,22 @@ pub async fn list( match result { Ok(res) => { let entities = res.1; - let num_pages = res.0; + let num_pages = std::cmp::max(res.0, 1); ctx.insert("entities", &entities); ctx.insert("num_pages", &num_pages); + ctx.insert("page", &std::cmp::min(num_pages, page)); + page = std::cmp::min(page, num_pages); + let min_show_page = if &page < &5 { 1 } else { let max_page = &page - &5; max_page }; + let max_show_page = if &page >= &num_pages { std::cmp::max(1, num_pages - 1) } else { let max_page = &page + &5; std::cmp::min(num_pages - 1, max_page) }; + ctx.insert("min_show_page", &min_show_page); + ctx.insert("max_show_page", &max_show_page); }, Err(e) => { ctx.insert("entities", &Vec::::new()); ctx.insert("num_pages", &0); + ctx.insert("min_show_page", &1); + ctx.insert("max_show_page", &1); + ctx.insert("page", &1); errors.push(e); } } @@ -79,7 +88,6 @@ pub async fn list( ctx.insert("entity_name", &entity_name); ctx.insert("notifications", ¬ifications); - ctx.insert("page", &page); ctx.insert("entities_per_page", &entities_per_page); ctx.insert("render_partial", &render_partial); ctx.insert("view_model", &ActixAdminViewModelSerializable::from(view_model.clone())); diff --git a/templates/base.html b/templates/base.html index b1328ea..7295f52 100644 --- a/templates/base.html +++ b/templates/base.html @@ -12,9 +12,6 @@ -
-
-
{% include "navbar.html" %}
diff --git a/templates/head.html b/templates/head.html index 17a369b..afcd8aa 100644 --- a/templates/head.html +++ b/templates/head.html @@ -7,6 +7,29 @@