From 0ecba48bf67dc5c5679ca415f462320e15f64bfd Mon Sep 17 00:00:00 2001 From: Manuel Gugger Date: Tue, 4 Jul 2023 19:54:37 +0200 Subject: [PATCH] add example for custom filter to the docs --- docs/content/docs/custom-filters.md | 48 ++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/docs/content/docs/custom-filters.md b/docs/content/docs/custom-filters.md index d6f3940..8ff3fcd 100644 --- a/docs/content/docs/custom-filters.md +++ b/docs/content/docs/custom-filters.md @@ -5,4 +5,50 @@ draft: false weight: 6 --- -# Custom Filters \ No newline at end of file +# Custom Filters + +You may add custom filters by implementing the ActixAdminModelFilterTrait for the Entity. The filters are separated from the actual values which might need to be loaded from the Db. For any filter requiring values for a dropdown, add a match for the filter name in the get_filter_values() method. + +```rust +#[async_trait] +impl ActixAdminModelFilterTrait for Entity { + fn get_filter() -> Vec> { + vec![ + ActixAdminModelFilter:: { + name: "Insert Date After".to_string(), + filter_type: ActixAdminModelFilterType::DateTime, + filter: |q: sea_orm::Select, v| -> sea_orm::Select { + q.apply_if(v, | query, val: String| query.filter(Column::InsertDate.gte(val))) + }, + values: None + }, + ActixAdminModelFilter:: { + name: "Is Visible".to_string(), + filter_type: ActixAdminModelFilterType::Checkbox, + filter: |q: sea_orm::Select, v| -> sea_orm::Select { + q.apply_if(v, | query, val: String| query.filter(Column::IsVisible.eq(val))) + }, + values: None + }, + ActixAdminModelFilter:: { + name: "Post".to_string(), + filter_type: ActixAdminModelFilterType::SelectList, + filter: |q: sea_orm::Select, v| -> sea_orm::Select { + q.apply_if(v, | query, val: String| query.filter(Column::PostId.eq(val))) + }, + values: None + } + ] + } + + async fn get_filter_values(filter: &ActixAdminModelFilter, db: &DatabaseConnection) -> Option> { + match filter.name.as_str() { + "Post" => Some({ + Post::find().order_by_asc(post::Column::Id).all(db).await.unwrap() + .iter().map(|p| (p.id.to_string(), p.title.to_string())).collect() + }), + _ => None + } + } +} +``` \ No newline at end of file