add example for custom filter to the docs
This commit is contained in:
parent
d95c84c2c6
commit
0ecba48bf6
@ -5,4 +5,50 @@ draft: false
|
||||
weight: 6
|
||||
---
|
||||
|
||||
# Custom Filters
|
||||
# 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<Entity> for Entity {
|
||||
fn get_filter() -> Vec<ActixAdminModelFilter<Entity>> {
|
||||
vec![
|
||||
ActixAdminModelFilter::<Entity> {
|
||||
name: "Insert Date After".to_string(),
|
||||
filter_type: ActixAdminModelFilterType::DateTime,
|
||||
filter: |q: sea_orm::Select<Entity>, v| -> sea_orm::Select<Entity> {
|
||||
q.apply_if(v, | query, val: String| query.filter(Column::InsertDate.gte(val)))
|
||||
},
|
||||
values: None
|
||||
},
|
||||
ActixAdminModelFilter::<Entity> {
|
||||
name: "Is Visible".to_string(),
|
||||
filter_type: ActixAdminModelFilterType::Checkbox,
|
||||
filter: |q: sea_orm::Select<Entity>, v| -> sea_orm::Select<Entity> {
|
||||
q.apply_if(v, | query, val: String| query.filter(Column::IsVisible.eq(val)))
|
||||
},
|
||||
values: None
|
||||
},
|
||||
ActixAdminModelFilter::<Entity> {
|
||||
name: "Post".to_string(),
|
||||
filter_type: ActixAdminModelFilterType::SelectList,
|
||||
filter: |q: sea_orm::Select<Entity>, v| -> sea_orm::Select<Entity> {
|
||||
q.apply_if(v, | query, val: String| query.filter(Column::PostId.eq(val)))
|
||||
},
|
||||
values: None
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
async fn get_filter_values(filter: &ActixAdminModelFilter<Entity>, db: &DatabaseConnection) -> Option<Vec<(String, String)>> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
Loading…
Reference in New Issue
Block a user