add possibility to sort columns in list

This commit is contained in:
manuel 2023-01-15 14:11:23 +01:00
parent 4eafe6e40a
commit 5b1ec9c7ab
7 changed files with 42 additions and 8 deletions

View File

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

View File

@ -156,6 +156,7 @@ pub fn derive_actix_admin_model(input: proc_macro::TokenStream) -> proc_macro::T
let fields_textarea = get_actix_admin_fields_textarea(&fields);
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 expanded = quote! {
actix_admin::prelude::lazy_static! {
@ -193,7 +194,11 @@ pub fn derive_actix_admin_model(input: proc_macro::TokenStream) -> proc_macro::T
#(#fields_file_upload),*
];
for (field_name, html_input_type, select_list, is_option_list, fields_type_path, is_textarea, is_file_upload) in actix_admin::prelude::izip!(&field_names, &html_input_types, &field_select_lists, is_option_lists, fields_type_paths, fields_textareas, fields_fileupload) {
let list_sort_positions = [
#(#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 select_list = select_list.replace('"', "").replace(' ', "").to_string();
let field_name = field_name.replace('"', "").replace(' ', "").to_string();
@ -204,6 +209,7 @@ pub fn derive_actix_admin_model(input: proc_macro::TokenStream) -> proc_macro::T
html_input_type: html_input_type,
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)
});
}

View File

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

View File

@ -55,6 +55,15 @@ pub fn filter_fields(fields: &Fields) -> Vec<ModelField> {
let is_not_empty = actix_admin_attr
.clone()
.map_or(false, |attr| attr.not_empty.is_some());
let list_sort_position: usize = actix_admin_attr.clone().map_or(99, |attr| {
attr.list_sort_position.map_or( 99, |attr_field| {
let sort_pos = LitStr::from(attr_field).value().parse::<usize>();
match sort_pos {
Ok(pos) => pos,
_ => 99
}
})
});
let select_list = actix_admin_attr.clone().map_or("".to_string(), |attr| {
attr.select_list.map_or("".to_string(), |attr_field| {
(LitStr::from(attr_field)).value()
@ -78,7 +87,8 @@ pub fn filter_fields(fields: &Fields) -> Vec<ModelField> {
searchable: is_searchable,
textarea: is_textarea,
file_upload: is_file_upload,
not_empty: is_not_empty
not_empty: is_not_empty,
list_sort_position: list_sort_position
};
Some(model_field)
} else {
@ -282,6 +292,20 @@ pub fn get_primary_key_field_name(fields: &Vec<ModelField>) -> String {
primary_key_model_field.ident.to_string()
}
pub fn get_fields_list_sort_positions(fields: &Vec<ModelField>) -> Vec<TokenStream> {
fields
.iter()
.filter(|model_field| !model_field.primary_key)
.map(|model_field| {
let sort_position = model_field.list_sort_position;
quote! {
#sort_position
}
})
.collect::<Vec<_>>()
}
pub fn get_fields_for_from_model(fields: &Vec<ModelField>) -> Vec<TokenStream> {
fields
.iter()

View File

@ -22,6 +22,7 @@ pub struct Model {
#[actix_admin(select_list="Tea")]
pub tea_optional: Option<Tea>,
#[sea_orm(column_type = "Date")]
#[actix_admin(list_sort_position="1")]
pub insert_date: Date,
#[actix_admin(file_upload)]
pub attachment: Option<String>

View File

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

View File

@ -88,7 +88,7 @@
{% endif %}
{% endif %}
</th>
{% for model_field in view_model.fields -%}
{% for model_field in view_model.fields | 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 -%}
{% for model_field in view_model.fields | 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" %}