leptos_struct_table/
table_row.rs

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