implement htmx multi select for delete
This commit is contained in:
parent
a8ae8eff55
commit
d0cc19d65c
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
use crate::routes::{create_get, create_post, delete_post, edit_get, edit_post, index, list};
|
||||
use crate::routes::{create_get, create_post, delete_post, delete_many_post, edit_get, edit_post, index, list};
|
||||
|
||||
pub struct ActixAdminBuilder {
|
||||
pub scopes: Vec<actix_web::Scope>,
|
||||
@ -43,6 +43,7 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder {
|
||||
.route("/edit/{id}", web::get().to(edit_get::<T, E>))
|
||||
.route("/edit/{id}", web::post().to(edit_post::<T, E>))
|
||||
.route("/delete/{id}", web::post().to(delete_post::<T, E>))
|
||||
.route("/delete_many", web::post().to(delete_many_post::<T, E>))
|
||||
);
|
||||
|
||||
self.actix_admin.entity_names.push(E::get_entity_name());
|
||||
|
21
actix_admin/src/routes/delete_many_post.rs
Normal file
21
actix_admin/src/routes/delete_many_post.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use actix_web::{web, Error, HttpRequest, HttpResponse};
|
||||
use serde::{Deserialize};
|
||||
use actix_web::http::header;
|
||||
use crate::prelude::*;
|
||||
|
||||
pub async fn delete_many_post<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
|
||||
req: HttpRequest,
|
||||
data: web::Data<T>,
|
||||
text: String,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let _db = &data.get_db();
|
||||
let entity_name = E::get_entity_name();
|
||||
println!("{:?}", text);
|
||||
|
||||
Ok(HttpResponse::Found()
|
||||
.append_header((
|
||||
header::LOCATION,
|
||||
format!("/admin/{}/list?render_partial=true", entity_name),
|
||||
))
|
||||
.finish())
|
||||
}
|
@ -9,11 +9,7 @@ pub async fn delete_post<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>
|
||||
id: web::Path<i32>
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let db = &data.get_db();
|
||||
//let entity_name = E::get_entity_name();
|
||||
//let actix_admin = data.get_actix_admin();
|
||||
//let view_model = actix_admin.view_models.get(&entity_name).unwrap();
|
||||
|
||||
// TODO:handle any errors
|
||||
|
||||
let _result = E::delete_entity(db, id.into_inner()).await;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
|
@ -52,6 +52,7 @@ pub async fn list<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
|
||||
ctx.insert("render_partial", &render_partial);
|
||||
ctx.insert("num_pages", &num_pages);
|
||||
ctx.insert("view_model", &view_model);
|
||||
ctx.insert("search", &search);
|
||||
|
||||
let body = TERA
|
||||
.render("list.html", &ctx)
|
||||
|
@ -10,9 +10,12 @@ mod list;
|
||||
pub use list::list;
|
||||
|
||||
mod delete_post;
|
||||
mod delete_many_post;
|
||||
pub use delete_post::delete_post;
|
||||
pub use delete_many_post::delete_many_post;
|
||||
|
||||
mod edit_get;
|
||||
mod edit_post;
|
||||
pub use edit_get::edit_get;
|
||||
pub use edit_post::edit_post;
|
||||
pub use edit_post::edit_post;
|
||||
|
||||
|
@ -5,88 +5,98 @@
|
||||
{% if not render_partial or render_partial == false %}
|
||||
<article>
|
||||
<div class="grid">
|
||||
<div>
|
||||
<div hx-include="#checked-rows" hx-target="#{{ entity_name }}table">
|
||||
<details role="list">
|
||||
<summary aria-haspopup="listbox" role="button">
|
||||
Actions
|
||||
</summary>
|
||||
<ul role="listbox">
|
||||
<li><a href="create">Create</a></li>
|
||||
<li><a>Delete Selected</a></li>
|
||||
<li><a href="#" hx-post="delete_many">Delete Selected</a></li>
|
||||
</ul>
|
||||
</details>
|
||||
</div>
|
||||
<div>
|
||||
<input type="search" id="search" name="search" placeholder="Search"
|
||||
<input type="search" id="search" value="{{ search }}" name="search" placeholder="Search"
|
||||
hx-get="/admin/{{ entity_name }}/list?render_partial=true&entities_per_page={{ entities_per_page }}&page={{ page }}"
|
||||
hx-trigger="keyup changed delay:500ms, search" hx-target="#{{ entity_name }}table"
|
||||
hx-indicator=".htmx-indicator"
|
||||
>
|
||||
hx-indicator=".htmx-indicator">
|
||||
</div>
|
||||
</div>
|
||||
<figure id="{{ entity_name }}table">
|
||||
{% endif %}
|
||||
<table role="grid">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>
|
||||
<div class="htmx-indicator">{% include "spinner.svg" %}</div>
|
||||
</th>
|
||||
<th>{{ view_model.primary_key | title }}</th>
|
||||
{% for model_field in view_model.fields -%}
|
||||
<th>{{ model_field.field_name | split(pat="_") | join(sep=" ") | title }}</th>
|
||||
{%- endfor %}
|
||||
<th>
|
||||
<!-- Edit Action -->
|
||||
<!-- Delete Action -->
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody hx-confirm="Are you sure?" hx-target="closest tr" hx-swap="outerHTML">
|
||||
{% for entity in entities -%}
|
||||
<tr>
|
||||
<td><input type="checkbox" id="entity{{ entity.primary_key }}" name="terms"></td>
|
||||
<td>{{ entity.primary_key }}</td>
|
||||
{% for model_field in view_model.fields -%}
|
||||
<td>{{ entity.values | get(key=model_field.field_name) }}</td>
|
||||
{%- endfor %}
|
||||
<td>
|
||||
<a href="edit/{{ entity.primary_key }}">✎</a>
|
||||
<a href="#" hx-post="delete/{{ entity.primary_key }}">🗑</a>
|
||||
</td>
|
||||
</tr>
|
||||
{%- endfor %}
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="{{ view_model.fields | length + 3 }}">
|
||||
<a href="?page={{ page - 1 }}&entities_per_page={{ entities_per_page }}">«</a>
|
||||
{% for i in range(end=num_pages) %}
|
||||
<a href="?page={{ i + 1 }}&entities_per_page={{ entities_per_page }}">{{ i + 1 }}</a>
|
||||
{% endif %}
|
||||
|
||||
<div id="{{ entity_name }}table">
|
||||
<figure>
|
||||
<form id="checked-rows">
|
||||
<table role="grid">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>
|
||||
<div class="htmx-indicator">{% include "spinner.svg" %}</div>
|
||||
</th>
|
||||
<th>{{ view_model.primary_key | title }}</th>
|
||||
{% for model_field in view_model.fields -%}
|
||||
<th>{{ model_field.field_name | split(pat="_") | join(sep=" ") | title }}</th>
|
||||
{%- endfor %}
|
||||
<a href="?page={{ page + 1 }}&entities_per_page={{ entities_per_page }}">»</a>
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
{% if not render_partial or render_partial == false %}
|
||||
</figure>
|
||||
<div class="grid">
|
||||
<div></div>
|
||||
<div></div>
|
||||
<div></div>
|
||||
<div>
|
||||
<form>
|
||||
<label for="entities_per_page">Entities per Page
|
||||
<select name="entities_per_page" onchange="this.form.submit()">
|
||||
{% for a in [10,20,50,100,] %}
|
||||
<option {% if entities_per_page==a %}selected{% endif %} value="{{ a }}">{{ a }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</label>
|
||||
<th>
|
||||
<!-- Edit Action -->
|
||||
<!-- Delete Action -->
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody hx-confirm="Are you sure?" hx-target="closest tr" hx-swap="outerHTML">
|
||||
{% for entity in entities -%}
|
||||
<tr>
|
||||
<td><input type="checkbox" name="ids" value="{{ entity.primary_key }}" name="terms"></td>
|
||||
<td>{{ entity.primary_key }}</td>
|
||||
{% for model_field in view_model.fields -%}
|
||||
<td>{{ entity.values | get(key=model_field.field_name) }}</td>
|
||||
{%- endfor %}
|
||||
<td>
|
||||
<a href="edit/{{ entity.primary_key }}">✎</a>
|
||||
<a hx-post="delete/{{ entity.primary_key }}">🗑</a>
|
||||
</td>
|
||||
</tr>
|
||||
{%- endfor %}
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="{{ view_model.fields | length + 3 }}">
|
||||
<a
|
||||
href="?page={{ page - 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}">«</a>
|
||||
{% for i in range(end=num_pages) %}
|
||||
<a href="?page={{ i + 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}">{{
|
||||
i + 1 }}</a>
|
||||
{%- endfor %}
|
||||
<a
|
||||
href="?page={{ page + 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}">»</a>
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</form>
|
||||
</figure>
|
||||
<div class="grid">
|
||||
<div></div>
|
||||
<div></div>
|
||||
<div></div>
|
||||
<div>
|
||||
<form>
|
||||
<input type="hidden" value="{{ search }}" name="search">
|
||||
<label for="entities_per_page">Entities per Page
|
||||
<select name="entities_per_page" onchange="this.form.submit()">
|
||||
{% for a in [10,20,50,100,] %}
|
||||
<option {% if entities_per_page==a %}selected{% endif %} value="{{ a }}">{{ a }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</label>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if not render_partial or render_partial == false %}
|
||||
</article>
|
||||
{% endif %}
|
||||
|
||||
{% endblock content %}
|
Loading…
Reference in New Issue
Block a user