1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::{ColumnSort, HeadDragHandler, TableClassesProvider, TableHeadEvent};
use leptos::prelude::*;
use std::collections::VecDeque;
/// This trait has to be implemented in order for [`TableContent`] to be able to render rows and the head row of the table.
/// Usually this is done by `#[derive(TableRow)]`.
///
/// Please see the [simple example](https://github.com/Synphonyte/leptos-struct-table/blob/master/examples/simple/src/main.rs)
/// for how to use.
pub trait TableRow<Column: Copy + Send + Sync + 'static>: Sized {
type ClassesProvider: TableClassesProvider + Copy;
/// How many columns this row has (i.e. the number of fields in the struct)
const COLUMN_COUNT: usize;
/// Renders the inner of one row of the table using the cell renderers.
/// This produces the children that go into the `row_renderer` given to [`TableContent`].
///
/// This render function has to render exactly one root element.
fn render_row(
row: RwSignal<Self>,
index: usize,
columns: RwSignal<Vec<Column>>,
) -> impl IntoView;
/// Render the head row of the table.
fn render_head_row<F>(
sorting: Signal<VecDeque<(Column, ColumnSort)>>,
on_head_click: F,
drag_handlers: HeadDragHandler<Column>,
columns: RwSignal<Vec<Column>>,
) -> impl IntoView
where
F: Fn(TableHeadEvent<Column>) + Send + Clone + 'static;
/// Returns the cell renderer for a **column**
/// Allows to create custom row renders while still using the annotation configured cell renderers.
fn cell_renderer_for_column(
row: RwSignal<Self>,
column: Column,
class: String,
) -> impl IntoView;
/// All columns this row can show in their default order.
fn columns() -> &'static [Column];
/// The name of the column (= struct field name) at the given index. This can be used to implement
/// sorting in a database. Information on column indexes is available at: the [Column index type](crate#column-index-type) section.
fn col_name(col_index: Column) -> &'static str;
/// Converts the given sorting to an SQL statement.
/// Return `None` when there is nothing to be sorted otherwise `Some("ORDER BY ...")`.
/// Uses [`Self::col_name`] to get the column names for sorting.
fn sorting_to_sql(sorting: &VecDeque<(Column, ColumnSort)>) -> Option<String>
where
Column: Send + Sync + 'static,
{
let mut sort = vec![];
for (col, col_sort) in sorting {
if let Some(col_sort) = col_sort.as_sql() {
sort.push(format!("{} {}", Self::col_name(*col), col_sort))
}
}
if sort.is_empty() {
return None;
}
Some(format!("ORDER BY {}", sort.join(", ")))
}
}
pub fn get_sorting_for_column<Column>(
col_index: Column,
sorting: Signal<VecDeque<(Column, ColumnSort)>>,
) -> ColumnSort
where
Column: Eq + Send + Sync + 'static,
{
sorting
.read()
.iter()
.find(|(col, _)| *col == col_index)
.map(|(_, sort)| *sort)
.unwrap_or(ColumnSort::None)
}