Skip to main content

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    /// Returns the cell renderer for a **column**
37    /// Allows to create custom row renders while still using the annotation configured cell renderers.
38    fn cell_renderer_for_column(
39        row: RwSignal<Self>,
40        column: Column,
41        class: String,
42    ) -> impl IntoView;
43
44    /// All columns this row can show in their default order.
45    fn columns() -> &'static [Column];
46
47    /// The name of the column (= struct field name) at the given index. This can be used to implement
48    /// sorting in a database. Information on column indexes is available at: the [Column index type](crate#column-index-type) section.
49    fn col_name(col_index: Column) -> &'static str;
50
51    /// Converts the given sorting to an SQL statement.
52    /// Return `None` when there is nothing to be sorted otherwise `Some("ORDER BY ...")`.
53    /// Uses [`Self::col_name`] to get the column names for sorting.
54    fn sorting_to_sql(sorting: &VecDeque<(Column, ColumnSort)>) -> Option<String>
55    where
56        Column: Send + Sync + 'static,
57    {
58        let mut sort = vec![];
59
60        for (col, col_sort) in sorting {
61            if let Some(col_sort) = col_sort.as_sql() {
62                sort.push(format!("{} {}", Self::col_name(*col), col_sort))
63            }
64        }
65
66        if sort.is_empty() {
67            return None;
68        }
69
70        Some(format!("ORDER BY {}", sort.join(", ")))
71    }
72}
73
74pub fn get_sorting_for_column<Column>(
75    col_index: Column,
76    sorting: Signal<VecDeque<(Column, ColumnSort)>>,
77) -> ColumnSort
78where
79    Column: Eq + Send + Sync + 'static,
80{
81    sorting
82        .read()
83        .iter()
84        .find(|(col, _)| *col == col_index)
85        .map(|(_, sort)| *sort)
86        .unwrap_or(ColumnSort::None)
87}