leptos_struct_table/
table_row.rs1use crate::{ColumnSort, HeadDragHandler, TableClassesProvider, TableHeadEvent};
2use leptos::prelude::*;
3use std::collections::VecDeque;
4
5pub trait TableRow<Column: Copy + Send + Sync + 'static>: Sized {
11 type ClassesProvider: TableClassesProvider + Copy;
12
13 const COLUMN_COUNT: usize;
15
16 fn render_row(
21 row: RwSignal<Self>,
22 index: usize,
23 columns: RwSignal<Vec<Column>>,
24 ) -> impl IntoView;
25
26 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 fn columns() -> &'static [Column];
38
39 fn col_name(col_index: Column) -> &'static str;
42
43 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}