add empty selectlist to optional field

This commit is contained in:
Manuel Gugger 2022-07-26 11:47:49 +02:00
parent 5194febe9b
commit 3702474b41
10 changed files with 53 additions and 26 deletions

View File

@ -11,7 +11,8 @@ use struct_fields::{
get_actix_admin_fields, get_actix_admin_fields,
get_field_for_primary_key, get_field_for_primary_key,
get_primary_key_field_name, get_primary_key_field_name,
get_actix_admin_fields_select_list get_actix_admin_fields_select_list,
get_actix_admin_fields_is_option_list
}; };
mod selectlist_fields; mod selectlist_fields;
@ -35,6 +36,7 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
let field_names = get_actix_admin_fields(&fields); let field_names = get_actix_admin_fields(&fields);
let field_html_input_type = get_actix_admin_fields_html_input(&fields); let field_html_input_type = get_actix_admin_fields_html_input(&fields);
let field_select_list = get_actix_admin_fields_select_list(&fields); let field_select_list = get_actix_admin_fields_select_list(&fields);
let is_option_list = get_actix_admin_fields_is_option_list(&fields);
let name_primary_field_str = get_primary_key_field_name(&fields); let name_primary_field_str = get_primary_key_field_name(&fields);
let fields_for_create_model = get_fields_for_create_model(&fields); let fields_for_create_model = get_fields_for_create_model(&fields);
let fields_for_edit_model = get_fields_for_edit_model(&fields); let fields_for_edit_model = get_fields_for_edit_model(&fields);
@ -185,11 +187,16 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
).split(",") ).split(",")
.collect::<Vec<_>>(); .collect::<Vec<_>>();
for (field_name, html_input_type, select_list) in izip!(&field_names, &html_input_types, &field_select_lists) { let is_option_lists = [
#(#is_option_list),*
];
for (field_name, html_input_type, select_list, is_option_list) in izip!(&field_names, &html_input_types, &field_select_lists, is_option_lists) {
vec.push(ActixAdminViewModelField { vec.push(ActixAdminViewModelField {
field_name: field_name.replace('"', "").replace(' ', "").to_string(), field_name: field_name.replace('"', "").replace(' ', "").to_string(),
html_input_type: html_input_type.replace('"', "").replace(' ', "").to_string(), html_input_type: html_input_type.replace('"', "").replace(' ', "").to_string(),
select_list: select_list.replace('"', "").replace(' ', "").to_string() select_list: select_list.replace('"', "").replace(' ', "").to_string(),
is_option: is_option_list
}); });
} }
vec vec

View File

@ -114,6 +114,20 @@ pub fn get_actix_admin_fields(fields: &Vec<ModelField>) -> Vec<TokenStream> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }
pub fn get_actix_admin_fields_is_option_list(fields: &Vec<ModelField>) -> Vec<TokenStream> {
fields
.iter()
.filter(|model_field| !model_field.primary_key)
.map(|model_field| {
let is_option = model_field.is_option();
quote! {
#is_option
}
})
.collect::<Vec<_>>()
}
pub fn get_actix_admin_fields_html_input(fields: &Vec<ModelField>) -> Vec<TokenStream> { pub fn get_actix_admin_fields_html_input(fields: &Vec<ModelField>) -> Vec<TokenStream> {
fields fields
.iter() .iter()

View File

@ -23,7 +23,7 @@ pub async fn create_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
ctx.insert("entity_names", &entity_names); ctx.insert("entity_names", &entity_names);
ctx.insert("view_model", &view_model); ctx.insert("view_model", &view_model);
ctx.insert("select_lists", &E::get_select_lists(_db).await); ctx.insert("select_lists", &E::get_select_lists(_db).await);
ctx.insert("list_link", E::get_list_link()); ctx.insert("list_link", &E::get_list_link(&entity_name));
let body = TERA let body = TERA
.render("create.html", &ctx) .render("create.html", &ctx)

View File

@ -27,7 +27,7 @@ pub async fn edit_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
ctx.insert("view_model", &view_model); ctx.insert("view_model", &view_model);
ctx.insert("model", &model); ctx.insert("model", &model);
ctx.insert("select_lists", &E::get_select_lists(db).await); ctx.insert("select_lists", &E::get_select_lists(db).await);
ctx.insert("list_link", E::get_list_link()); ctx.insert("list_link", &E::get_list_link(&entity_name));
let body = TERA let body = TERA
.render("edit.html", &ctx) .render("edit.html", &ctx)

View File

@ -21,8 +21,8 @@ pub trait ActixAdminViewModelTrait {
fn get_entity_name() -> String; fn get_entity_name() -> String;
fn get_list_link() -> &'static str { fn get_list_link(entity_name: &String) -> String {
"list" format!("/admin/{}/list", entity_name)
} }
} }
@ -37,5 +37,6 @@ pub struct ActixAdminViewModel {
pub struct ActixAdminViewModelField { pub struct ActixAdminViewModelField {
pub field_name: String, pub field_name: String,
pub html_input_type: String, pub html_input_type: String,
pub select_list: String pub select_list: String,
pub is_option: bool
} }

View File

@ -5,13 +5,13 @@
<meta charset="utf-8"> <meta charset="utf-8">
<title>Actix Admin</title> <title>Actix Admin</title>
<link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.classless.min.css"> <link rel="stylesheet" href="https://unpkg.com/@picocss/pico@latest/css/pico.min.css">
<script src="https://unpkg.com/htmx.org@1.7.0"></script> <script src="https://unpkg.com/htmx.org@1.7.0"></script>
</head> </head>
<body> <body>
<main class="container">
{% include "header.html" %} {% include "header.html" %}
<main>
<div> <div>
{% block content %} {% block content %}
{% endblock content %} {% endblock content %}

View File

@ -2,21 +2,24 @@
{% block content %} {% block content %}
<form method="post"> <form method="post">
<div>
{% for model_field in view_model.fields -%} {% for model_field in view_model.fields -%}
{% if model_field.select_list != "" %} {% if model_field.select_list != "" %}
<select name="{{ model_field.field_name }}"> <select name="{{ model_field.field_name }}">
<option value="" selected>Select</option> {% if model_field.is_option %}
<option value="" selected></option>
{% else %}
<option value="" selected disabled>Select</option>
{% endif %}
{% for select_list_item in select_lists[model_field.field_name] -%} {% for select_list_item in select_lists[model_field.field_name] -%}
<option value="{{ select_list_item[0] }}">{{ select_list_item[1] }}</option> <option value="{{ select_list_item[0] }}">{{ select_list_item[1] }}</option>
{%- endfor %} {%- endfor %}
</select> </select>
{% else %} {% else %}
<input type="{{ model_field.html_input_type }}" name="{{ model_field.field_name }}" placeholder="{{ model_field.field_name }}" aria-label="{{ model_field.field_name }}"><!-- required="" --> <input type="{{ model_field.html_input_type }}" name="{{ model_field.field_name }}"
placeholder="{{ model_field.field_name }}" aria-label="{{ model_field.field_name }}"><!-- required="" -->
{% endif %} {% endif %}
{%- endfor %} {%- endfor %}
<button type="submit">Save</button> <button type="submit">Save</button>
<a href="{{ list_link }}" role="button">Cancel</a> <a href="{{ list_link }}" role="button" class="secondary">Cancel</a>
</div>
</form> </form>
{% endblock content %} {% endblock content %}

View File

@ -6,7 +6,9 @@
{% for model_field in view_model.fields -%} {% for model_field in view_model.fields -%}
{% if model_field.select_list != "" %} {% if model_field.select_list != "" %}
<select name="{{ model_field.field_name }}"> <select name="{{ model_field.field_name }}">
<option value="" selected>Select</option> {% if model_field.is_option %}
<option value=""></option>
{% endif %}
{% for select_list_item in select_lists[model_field.field_name] -%} {% for select_list_item in select_lists[model_field.field_name] -%}
<option {% if select_list_item[0] == model.values | get(key=model_field.field_name) %} selected {% endif %} value="{{ select_list_item[0] }}">{{ select_list_item[1] }}</option> <option {% if select_list_item[0] == model.values | get(key=model_field.field_name) %} selected {% endif %} value="{{ select_list_item[0] }}">{{ select_list_item[1] }}</option>
{%- endfor %} {%- endfor %}
@ -16,7 +18,7 @@
{% endif %} {% endif %}
{%- endfor %} {%- endfor %}
<button type="submit">Save</button> <button type="submit">Save</button>
<a href="{{ list_link }}" role="button">Cancel</a> <a href="{{ list_link }}" role="button" class="secondary">Cancel</a>
</div> </div>
</form> </form>
{% endblock content %} {% endblock content %}

View File

@ -1,7 +1,7 @@
<header> <header>
<nav> <nav>
<ul> <ul>
<li>Actix Admin</li> <li><a href="/admin/">Actix Admin</a></li>
</ul> </ul>
<ul> <ul>
{% for entity_name in entity_names -%} {% for entity_name in entity_names -%}

Binary file not shown.