Skip to main content

yew_datatable_core/column/
cell_context.rs

1//! Context provided when rendering a table cell.
2//!
3//! Contains all information needed to render a cell value,
4//! including the row data, column information, and table state.
5
6use crate::column::column_id::ColumnId;
7use crate::row::data_table_row_id::DataTableRowId;
8
9/// Context provided when rendering a cell.
10///
11/// Contains all information needed to render a cell value,
12/// including the row data, column information, and table state.
13#[derive(Debug, Clone)]
14pub struct DataTableCellContext<'a, T> {
15    /// The row data.
16    pub row: &'a T,
17
18    /// The row ID.
19    pub row_id: DataTableRowId,
20
21    /// The row index in the current view.
22    pub row_index: usize,
23
24    /// The column ID.
25    pub column_id: ColumnId,
26
27    /// The column index.
28    pub column_index: usize,
29
30    /// Whether the row is selected.
31    pub is_selected: bool,
32
33    /// Whether the row is expanded.
34    pub is_expanded: bool,
35
36    /// The depth level for nested rows.
37    pub depth: usize,
38}
39
40impl<'a, T> DataTableCellContext<'a, T> {
41    /// Creates a new cell context.
42    ///
43    /// # Parameters
44    ///
45    /// - `row`: The row data reference.
46    /// - `row_id`: The unique identifier of the row.
47    /// - `row_index`: The row index in the current view.
48    /// - `column_id`: The column identifier.
49    /// - `column_index`: The column index in the current view.
50    ///
51    /// # Returns
52    ///
53    /// - `DataTableCellContext<'a, T>`: A new cell context with default selection, expansion, and depth state.
54    pub fn new(row: &'a T, row_id: DataTableRowId, row_index: usize, column_id: ColumnId, column_index: usize) -> Self {
55        Self {
56            row,
57            row_id,
58            row_index,
59            column_id,
60            column_index,
61            is_selected: false,
62            is_expanded: false,
63            depth: 0,
64        }
65    }
66
67    /// Sets the selection state.
68    ///
69    /// # Parameters
70    ///
71    /// - `selected`: Whether the row is selected.
72    ///
73    /// # Returns
74    ///
75    /// - `Self`: The modified cell context.
76    pub fn with_selected(mut self, selected: bool) -> Self {
77        // Update the selection state.
78        self.is_selected = selected;
79        self
80    }
81
82    /// Sets the expanded state.
83    ///
84    /// # Parameters
85    ///
86    /// - `expanded`: Whether the row is expanded.
87    ///
88    /// # Returns
89    ///
90    /// - `Self`: The modified cell context.
91    pub fn with_expanded(mut self, expanded: bool) -> Self {
92        // Update the expansion state.
93        self.is_expanded = expanded;
94        self
95    }
96
97    /// Sets the depth level.
98    ///
99    /// # Parameters
100    ///
101    /// - `depth`: The nesting depth level.
102    ///
103    /// # Returns
104    ///
105    /// - `Self`: The modified cell context.
106    pub fn with_depth(mut self, depth: usize) -> Self {
107        // Update the depth level.
108        self.depth = depth;
109        self
110    }
111}