leptos_struct_table/
table_row.rs

1use crate::{ColumnSort, TableClassesProvider, TableHeadEvent};
2use leptos::prelude::*;
3use std::collections::VecDeque;
4
5/// This trait has to 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: 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(row: RwSignal<Self>, index: usize) -> impl IntoView;
21
22    /// Render the head row of the table.
23    fn render_head_row<F>(
24        sorting: Signal<VecDeque<(usize, ColumnSort)>>,
25        on_head_click: F,
26    ) -> impl IntoView
27    where
28        F: Fn(TableHeadEvent) + Clone + 'static;
29
30    /// The name of the column (= struct field name) at the given index. This can be used to implement
31    /// sorting in a database. It takes the `#[table(skip)]` attributes into account. `col_index`
32    /// refers to the index of the field in the struct while ignoring skipped ones.
33    ///
34    /// For example:
35    /// ```
36    /// # use leptos_struct_table::*;
37    /// # use leptos::prelude::*;
38    /// #
39    /// #[derive(TableRow)]
40    /// struct Person {
41    ///     #[table(skip)]
42    ///     id: i64,            // -> ignored
43    ///
44    ///     name: String,       // -> col_index = 0
45    ///
46    ///     #[table(skip)]
47    ///     internal: usize,    // -> ignored
48    ///
49    ///     age: u16,           // -> col_index = 1
50    /// }
51    ///
52    /// assert_eq!(Person::col_name(0), "name");
53    /// assert_eq!(Person::col_name(1), "age");
54    /// ```
55    fn col_name(col_index: usize) -> &'static str;
56
57    /// Converts the given sorting to an SQL statement.
58    /// Return `None` when there is nothing to be sorted otherwise `Some("ORDER BY ...")`.
59    /// Uses [`Self::col_name`] to get the column names for sorting.
60    fn sorting_to_sql(sorting: &VecDeque<(usize, ColumnSort)>) -> Option<String> {
61        let mut sort = vec![];
62
63        for (col, col_sort) in sorting {
64            if let Some(col_sort) = col_sort.as_sql() {
65                sort.push(format!("{} {}", Self::col_name(*col), col_sort))
66            }
67        }
68
69        if sort.is_empty() {
70            return None;
71        }
72
73        Some(format!("ORDER BY {}", sort.join(", ")))
74    }
75}
76
77pub fn get_sorting_for_column(
78    col_index: usize,
79    sorting: Signal<VecDeque<(usize, ColumnSort)>>,
80) -> ColumnSort {
81    sorting
82        .read()
83        .iter()
84        .find(|(col, _)| *col == col_index)
85        .map(|(_, sort)| *sort)
86        .unwrap_or(ColumnSort::None)
87}