add paging
This commit is contained in:
parent
b7588163ed
commit
10f891b1cd
@ -96,8 +96,8 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
|
|||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
impl ActixAdminViewModelTrait for Entity {
|
impl ActixAdminViewModelTrait for Entity {
|
||||||
async fn list(db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel> {
|
async fn list(db: &DatabaseConnection, page: usize, entities_per_page: usize) -> (usize, Vec<ActixAdminModel>) {
|
||||||
let entities = Entity::list_model(db, 1, 5).await;
|
let entities = Entity::list_model(db, page, entities_per_page).await;
|
||||||
entities
|
entities
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,11 +146,12 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl ActixAdminModelTrait for Entity {
|
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::* };
|
use sea_orm::{ query::* };
|
||||||
let paginator = Entity::find()
|
let paginator = Entity::find()
|
||||||
.order_by_asc(Column::Id)
|
.order_by_asc(Column::Id)
|
||||||
.paginate(db, posts_per_page);
|
.paginate(db, posts_per_page);
|
||||||
|
let num_pages = paginator.num_pages().await.ok().unwrap();
|
||||||
let entities = paginator
|
let entities = paginator
|
||||||
.fetch_page(page - 1)
|
.fetch_page(page - 1)
|
||||||
.await
|
.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)> {
|
fn get_fields() -> Vec<(String, ActixAdminField)> {
|
||||||
|
@ -11,7 +11,7 @@ pub trait ActixAdminModelTrait {
|
|||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
page: usize,
|
page: usize,
|
||||||
posts_per_page: usize,
|
posts_per_page: usize,
|
||||||
) -> Vec<ActixAdminModel>;
|
) -> (usize, Vec<ActixAdminModel>);
|
||||||
fn get_fields() -> Vec<(String, ActixAdminField)>;
|
fn get_fields() -> Vec<(String, ActixAdminField)>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,14 +34,16 @@ pub async fn list<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
|
|||||||
.unwrap_or(DEFAULT_ENTITIES_PER_PAGE);
|
.unwrap_or(DEFAULT_ENTITIES_PER_PAGE);
|
||||||
|
|
||||||
let db = data.get_db();
|
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();
|
let mut ctx = Context::new();
|
||||||
ctx.insert("entity_names", &entity_names);
|
ctx.insert("entity_names", &entity_names);
|
||||||
ctx.insert("entities", &entities);
|
ctx.insert("entities", &entities);
|
||||||
ctx.insert("page", &page);
|
ctx.insert("page", &page);
|
||||||
ctx.insert("entities_per_page", &entities_per_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);
|
ctx.insert("view_model", &view_model);
|
||||||
|
|
||||||
let body = TERA
|
let body = TERA
|
||||||
|
@ -11,7 +11,7 @@ pub trait ActixAdminViewModelTrait {
|
|||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
page: usize,
|
page: usize,
|
||||||
entities_per_page: usize,
|
entities_per_page: usize,
|
||||||
) -> Vec<ActixAdminModel>;
|
) -> (usize, Vec<ActixAdminModel>);
|
||||||
|
|
||||||
// TODO: Replace return value with proper Result Type containing Ok or Err
|
// TODO: Replace return value with proper Result Type containing Ok or Err
|
||||||
async fn create_entity(db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel;
|
async fn create_entity(db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel;
|
||||||
|
@ -1,27 +1,48 @@
|
|||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<a href="create" role="button">Create</a>
|
<a href="create" role="button">+</a>
|
||||||
<table>
|
<table role="grid">
|
||||||
<tr>
|
<thead>
|
||||||
{% for model_field in view_model.fields -%}
|
<tr>
|
||||||
<th>{{ model_field[0] }}</th>
|
{% 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]) }}">✎</a>
|
||||||
|
<a href="delete/{{ entity.values | get(key=view_model.fields[0][0]) }}">🗑</a>
|
||||||
|
<!--
|
||||||
|
<form method="post" action="delete/{{ entity.values | get(key=view_model.fields[0][0]) }}">
|
||||||
|
<button type="submit">🗑</button>
|
||||||
|
</form>
|
||||||
|
-->
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
{%- endfor %}
|
{%- endfor %}
|
||||||
<th><!-- Edit Action --></th>
|
</tbody>
|
||||||
<th><!-- Delete Action --></th>
|
<tfoot>
|
||||||
</tr>
|
<tr>
|
||||||
{% for entity in entities -%}
|
<td colspan="{{ num_pages + 2 }}">
|
||||||
<tr>
|
<div>
|
||||||
{% for model_field in view_model.fields -%}
|
<a href="?page={{ page - 1 }}&entities_per_page={{ entities_per_page }}">«</a>
|
||||||
<td>{{ entity.values | get(key=model_field[0]) }}</td>
|
{% for i in range(end=num_pages) %}
|
||||||
{%- endfor %}
|
<a href="?page={{ i + 1 }}&entities_per_page={{ entities_per_page }}">{{ i + 1 }}</a>
|
||||||
<td><a href="edit/{{ entity.values | get(key=view_model.fields[0][0]) }}" role="button">Edit</a></td>
|
{%- endfor %}
|
||||||
<td>
|
<a href="?page={{ page + 1 }}&entities_per_page={{ entities_per_page }}">»</a>
|
||||||
<form method="post" action="delete/{{ entity.values | get(key=view_model.fields[0][0]) }}">
|
</td>
|
||||||
<button type="submit">Delete</button>
|
</tr>
|
||||||
</form>
|
</tfoot>
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
{%- endfor %}
|
|
||||||
</table>
|
</table>
|
||||||
{% endblock content %}
|
{% endblock content %}
|
BIN
database.db-wal
BIN
database.db-wal
Binary file not shown.
Loading…
Reference in New Issue
Block a user