add paging

This commit is contained in:
Manuel Gugger 2022-06-02 18:46:24 +02:00
parent b7588163ed
commit 10f891b1cd
6 changed files with 53 additions and 29 deletions

View File

@ -96,8 +96,8 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
#[async_trait(?Send)]
impl ActixAdminViewModelTrait for Entity {
async fn list(db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel> {
let entities = Entity::list_model(db, 1, 5).await;
async fn list(db: &DatabaseConnection, page: usize, entities_per_page: usize) -> (usize, Vec<ActixAdminModel>) {
let entities = Entity::list_model(db, page, entities_per_page).await;
entities
}
@ -146,11 +146,12 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
#[async_trait]
impl ActixAdminModelTrait for Entity {
async fn list_model(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<ActixAdminModel> {
async fn list_model(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> (usize, Vec<ActixAdminModel>) {
use sea_orm::{ query::* };
let paginator = Entity::find()
.order_by_asc(Column::Id)
.paginate(db, posts_per_page);
let num_pages = paginator.num_pages().await.ok().unwrap();
let entities = paginator
.fetch_page(page - 1)
.await
@ -162,7 +163,7 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
);
}
model_entities
(num_pages, model_entities)
}
fn get_fields() -> Vec<(String, ActixAdminField)> {

View File

@ -11,7 +11,7 @@ pub trait ActixAdminModelTrait {
db: &DatabaseConnection,
page: usize,
posts_per_page: usize,
) -> Vec<ActixAdminModel>;
) -> (usize, Vec<ActixAdminModel>);
fn get_fields() -> Vec<(String, ActixAdminField)>;
}

View File

@ -34,14 +34,16 @@ pub async fn list<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
.unwrap_or(DEFAULT_ENTITIES_PER_PAGE);
let db = data.get_db();
let entities: Vec<ActixAdminModel> = E::list(db, page, entities_per_page).await;
let result: (usize, Vec<ActixAdminModel>) = E::list(db, page, entities_per_page).await;
let entities = result.1;
let num_pages = result.0;
let mut ctx = Context::new();
ctx.insert("entity_names", &entity_names);
ctx.insert("entities", &entities);
ctx.insert("page", &page);
ctx.insert("entities_per_page", &entities_per_page);
ctx.insert("num_pages", "5" /*&num_pages*/);
ctx.insert("num_pages", &num_pages);
ctx.insert("view_model", &view_model);
let body = TERA

View File

@ -11,7 +11,7 @@ pub trait ActixAdminViewModelTrait {
db: &DatabaseConnection,
page: usize,
entities_per_page: usize,
) -> Vec<ActixAdminModel>;
) -> (usize, Vec<ActixAdminModel>);
// TODO: Replace return value with proper Result Type containing Ok or Err
async fn create_entity(db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel;

View File

@ -1,27 +1,48 @@
{% extends "base.html" %}
{% block content %}
<a href="create" role="button">Create</a>
<table>
<tr>
{% for model_field in view_model.fields -%}
<th>{{ model_field[0] }}</th>
<a href="create" role="button">&#43;</a>
<table role="grid">
<thead>
<tr>
{% for model_field in view_model.fields -%}
<th>{{ model_field[0] }}</th>
{%- endfor %}
<th>
<!-- Edit Action -->
<!-- Delete Action -->
</th>
</tr>
</thead>
<tbody>
{% for entity in entities -%}
<tr>
{% for model_field in view_model.fields -%}
<td>{{ entity.values | get(key=model_field[0]) }}</td>
{%- endfor %}
<td>
<a href="edit/{{ entity.values | get(key=view_model.fields[0][0]) }}">&#9998;</a>
<a href="delete/{{ entity.values | get(key=view_model.fields[0][0]) }}">&#128465;</a>
<!--
<form method="post" action="delete/{{ entity.values | get(key=view_model.fields[0][0]) }}">
<button type="submit">&#128465;</button>
</form>
-->
</td>
</tr>
{%- endfor %}
<th><!-- Edit Action --></th>
<th><!-- Delete Action --></th>
</tr>
{% for entity in entities -%}
<tr>
{% for model_field in view_model.fields -%}
<td>{{ entity.values | get(key=model_field[0]) }}</td>
{%- endfor %}
<td><a href="edit/{{ entity.values | get(key=view_model.fields[0][0]) }}" role="button">Edit</a></td>
<td>
<form method="post" action="delete/{{ entity.values | get(key=view_model.fields[0][0]) }}">
<button type="submit">Delete</button>
</form>
</td>
</tr>
{%- endfor %}
</tbody>
<tfoot>
<tr>
<td colspan="{{ num_pages + 2 }}">
<div>
<a href="?page={{ page - 1 }}&entities_per_page={{ entities_per_page }}">&laquo;</a>
{% for i in range(end=num_pages) %}
<a href="?page={{ i + 1 }}&entities_per_page={{ entities_per_page }}">{{ i + 1 }}</a>
{%- endfor %}
<a href="?page={{ page + 1 }}&entities_per_page={{ entities_per_page }}">&raquo;</a>
</td>
</tr>
</tfoot>
</table>
{% endblock content %}

Binary file not shown.