add empty selectlist to optional field
This commit is contained in:
parent
5194febe9b
commit
3702474b41
@ -11,7 +11,8 @@ use struct_fields::{
|
||||
get_actix_admin_fields,
|
||||
get_field_for_primary_key,
|
||||
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;
|
||||
@ -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_html_input_type = get_actix_admin_fields_html_input(&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 fields_for_create_model = get_fields_for_create_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(",")
|
||||
.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 {
|
||||
field_name: field_name.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
|
||||
|
@ -114,6 +114,20 @@ pub fn get_actix_admin_fields(fields: &Vec<ModelField>) -> Vec<TokenStream> {
|
||||
.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> {
|
||||
fields
|
||||
.iter()
|
||||
|
@ -23,7 +23,7 @@ pub async fn create_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
|
||||
ctx.insert("entity_names", &entity_names);
|
||||
ctx.insert("view_model", &view_model);
|
||||
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
|
||||
.render("create.html", &ctx)
|
||||
|
@ -27,7 +27,7 @@ pub async fn edit_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
|
||||
ctx.insert("view_model", &view_model);
|
||||
ctx.insert("model", &model);
|
||||
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
|
||||
.render("edit.html", &ctx)
|
||||
|
@ -21,8 +21,8 @@ pub trait ActixAdminViewModelTrait {
|
||||
|
||||
fn get_entity_name() -> String;
|
||||
|
||||
fn get_list_link() -> &'static str {
|
||||
"list"
|
||||
fn get_list_link(entity_name: &String) -> String {
|
||||
format!("/admin/{}/list", entity_name)
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,5 +37,6 @@ pub struct ActixAdminViewModel {
|
||||
pub struct ActixAdminViewModelField {
|
||||
pub field_name: String,
|
||||
pub html_input_type: String,
|
||||
pub select_list: String
|
||||
pub select_list: String,
|
||||
pub is_option: bool
|
||||
}
|
@ -5,13 +5,13 @@
|
||||
<meta charset="utf-8">
|
||||
<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>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<main class="container">
|
||||
{% include "header.html" %}
|
||||
<main>
|
||||
<div>
|
||||
{% block content %}
|
||||
{% endblock content %}
|
||||
|
@ -2,21 +2,24 @@
|
||||
|
||||
{% block content %}
|
||||
<form method="post">
|
||||
<div>
|
||||
{% for model_field in view_model.fields -%}
|
||||
{% if model_field.select_list != "" %}
|
||||
<select name="{{ model_field.field_name }}">
|
||||
<option value="" selected>Select</option>
|
||||
{% for select_list_item in select_lists[model_field.field_name] -%}
|
||||
<option value="{{ select_list_item[0] }}">{{ select_list_item[1] }}</option>
|
||||
{%- endfor %}
|
||||
</select>
|
||||
{% for model_field in view_model.fields -%}
|
||||
{% if model_field.select_list != "" %}
|
||||
<select name="{{ model_field.field_name }}">
|
||||
{% if model_field.is_option %}
|
||||
<option value="" selected></option>
|
||||
{% 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="" -->
|
||||
<option value="" selected disabled>Select</option>
|
||||
{% endif %}
|
||||
{% for select_list_item in select_lists[model_field.field_name] -%}
|
||||
<option value="{{ select_list_item[0] }}">{{ select_list_item[1] }}</option>
|
||||
{%- endfor %}
|
||||
<button type="submit">Save</button>
|
||||
<a href="{{ list_link }}" role="button">Cancel</a>
|
||||
</div>
|
||||
</select>
|
||||
{% 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="" -->
|
||||
{% endif %}
|
||||
{%- endfor %}
|
||||
<button type="submit">Save</button>
|
||||
<a href="{{ list_link }}" role="button" class="secondary">Cancel</a>
|
||||
</form>
|
||||
{% endblock content %}
|
@ -6,7 +6,9 @@
|
||||
{% for model_field in view_model.fields -%}
|
||||
{% if model_field.select_list != "" %}
|
||||
<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] -%}
|
||||
<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 %}
|
||||
@ -16,7 +18,7 @@
|
||||
{% endif %}
|
||||
{%- endfor %}
|
||||
<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>
|
||||
{% endblock content %}
|
@ -1,7 +1,7 @@
|
||||
<header>
|
||||
<nav>
|
||||
<ul>
|
||||
<li>Actix Admin</li>
|
||||
<li><a href="/admin/">Actix Admin</a></li>
|
||||
</ul>
|
||||
<ul>
|
||||
{% for entity_name in entity_names -%}
|
||||
|
BIN
database.db-wal
BIN
database.db-wal
Binary file not shown.
Loading…
Reference in New Issue
Block a user