add possibility to hide column in list

This commit is contained in:
manuel 2023-01-15 14:18:46 +01:00
parent 5b1ec9c7ab
commit 8796f6b7c7
7 changed files with 36 additions and 11 deletions

View File

@ -17,7 +17,8 @@ pub mod derive_attr {
pub textarea: Option<()>,
pub file_upload: Option<()>,
pub not_empty: Option<()>,
pub list_sort_position: Option<syn::LitStr>
pub list_sort_position: Option<syn::LitStr>,
pub list_hide_column: Option<()>
//pub inner_type: Option<syn::Type>,
// Anything that implements `syn::parse::Parse` is supported.

View File

@ -157,6 +157,7 @@ pub fn derive_actix_admin_model(input: proc_macro::TokenStream) -> proc_macro::T
let fields_file_upload = get_actix_admin_fields_file_upload(&fields);
let fields_match_name_to_columns = get_match_name_to_column(&fields);
let fields_list_sort_positions = get_fields_list_sort_positions(&fields);
let fields_list_hide_column = get_fields_list_hide_column(&fields);
let expanded = quote! {
actix_admin::prelude::lazy_static! {
@ -198,7 +199,11 @@ pub fn derive_actix_admin_model(input: proc_macro::TokenStream) -> proc_macro::T
#(#fields_list_sort_positions),*
];
for (field_name, html_input_type, select_list, is_option_list, fields_type_path, is_textarea, is_file_upload, list_sort_position) in actix_admin::prelude::izip!(&field_names, &html_input_types, &field_select_lists, is_option_lists, fields_type_paths, fields_textareas, fields_fileupload, list_sort_positions) {
let list_hide_columns = [
#(#fields_list_hide_column),*
];
for (field_name, html_input_type, select_list, is_option_list, fields_type_path, is_textarea, is_file_upload, list_sort_position, list_hide_column) in actix_admin::prelude::izip!(&field_names, &html_input_types, &field_select_lists, is_option_lists, fields_type_paths, fields_textareas, fields_fileupload, list_sort_positions, list_hide_columns) {
let select_list = select_list.replace('"', "").replace(' ', "").to_string();
let field_name = field_name.replace('"', "").replace(' ', "").to_string();
@ -210,7 +215,8 @@ pub fn derive_actix_admin_model(input: proc_macro::TokenStream) -> proc_macro::T
select_list: select_list.clone(),
is_option: is_option_list,
list_sort_position: list_sort_position,
field_type: ActixAdminViewModelFieldType::get_field_type(fields_type_path, select_list, is_textarea, is_file_upload)
field_type: ActixAdminViewModelFieldType::get_field_type(fields_type_path, select_list, is_textarea, is_file_upload),
list_hide_column: list_hide_column
});
}
vec

View File

@ -15,7 +15,8 @@ pub struct ModelField {
pub textarea: bool,
pub file_upload: bool,
pub not_empty: bool,
pub list_sort_position: usize
pub list_sort_position: usize,
pub list_hide_column: bool
}
impl ModelField {

View File

@ -6,8 +6,6 @@ use syn::{DeriveInput, Fields, LitStr, Ident};
pub fn get_fields_for_tokenstream(input: proc_macro::TokenStream) -> std::vec::Vec<ModelField> {
let ast: DeriveInput = syn::parse(input).unwrap();
//let (_vis, _ty, _generics) = (&ast.vis, &ast.ident, &ast.generics);
//let _names_struct_ident = Ident::new(&(ty.to_string() + "FieldStaticStr"), Span::call_site());
let fields = filter_fields(match ast.data {
syn::Data::Struct(ref s) => &s.fields,
@ -52,6 +50,9 @@ pub fn filter_fields(fields: &Fields) -> Vec<ModelField> {
let is_file_upload = actix_admin_attr
.clone()
.map_or(false, |attr| attr.file_upload.is_some());
let is_list_hide_column = actix_admin_attr
.clone()
.map_or(false, |attr| attr.list_hide_column.is_some());
let is_not_empty = actix_admin_attr
.clone()
.map_or(false, |attr| attr.not_empty.is_some());
@ -88,7 +89,8 @@ pub fn filter_fields(fields: &Fields) -> Vec<ModelField> {
textarea: is_textarea,
file_upload: is_file_upload,
not_empty: is_not_empty,
list_sort_position: list_sort_position
list_sort_position: list_sort_position,
list_hide_column: is_list_hide_column
};
Some(model_field)
} else {
@ -306,6 +308,20 @@ pub fn get_fields_list_sort_positions(fields: &Vec<ModelField>) -> Vec<TokenStre
.collect::<Vec<_>>()
}
pub fn get_fields_list_hide_column(fields: &Vec<ModelField>) -> Vec<TokenStream> {
fields
.iter()
.filter(|model_field| !model_field.primary_key)
.map(|model_field| {
let list_hide_column = model_field.list_hide_column;
quote! {
#list_hide_column
}
})
.collect::<Vec<_>>()
}
pub fn get_fields_for_from_model(fields: &Vec<ModelField>) -> Vec<TokenStream> {
fields
.iter()

View File

@ -15,7 +15,7 @@ pub struct Model {
#[actix_admin(searchable, not_empty)]
pub title: String,
#[sea_orm(column_type = "Text")]
#[actix_admin(searchable, textarea)]
#[actix_admin(searchable, textarea, list_hide_column)]
pub text: String,
#[actix_admin(select_list="Tea")]
pub tea_mandatory: Tea,

View File

@ -83,7 +83,8 @@ pub struct ActixAdminViewModelField {
pub select_list: String,
pub is_option: bool,
pub field_type: ActixAdminViewModelFieldType,
pub list_sort_position: usize
pub list_sort_position: usize,
pub list_hide_column: bool
}
impl ActixAdminViewModelFieldType {

View File

@ -88,7 +88,7 @@
{% endif %}
{% endif %}
</th>
{% for model_field in view_model.fields | sort(attribute="list_sort_position") -%}
{% for model_field in view_model.fields | filter(attribute="list_hide_column", value=false) | sort(attribute="list_sort_position") -%}
<th onclick="sort_by('{{ model_field.field_name }}');" class="is-clickable">{{
model_field.field_name | split(pat="_") | join(sep=" ") | title }}
{% if sort_by == model_field.field_name %}
@ -116,7 +116,7 @@
<i class="fa-solid fa-magnifying-glass"></i> {{ entity.primary_key }}
</a>
</td>
{% for model_field in view_model.fields | sort(attribute="list_sort_position") -%}
{% for model_field in view_model.fields | filter(attribute="list_hide_column", value=false) | sort(attribute="list_sort_position") -%}
{% if model_field.field_type == "Checkbox" %}
<td>{{ entity.values | get(key=model_field.field_name) | get_icon | safe }}</td>
{% elif model_field.field_type == "FileUpload" %}