Skip to main content

yew_datatable_core/column/
column_meta.rs

1//! Metadata for a table column that affects its behavior and display.
2//!
3//! Contains configuration such as header text, footer text, sortable/filterable
4//! flags, resize constraints, and group column settings.
5
6use crate::column::column_id::ColumnId;
7
8/// Metadata for a column that affects its behavior.
9///
10/// Controls column display properties, interaction capabilities,
11/// and sizing constraints.
12#[derive(Debug, Clone)]
13pub struct ColumnMeta {
14    /// The column ID.
15    pub id: ColumnId,
16
17    /// Display header text.
18    pub header: String,
19
20    /// Footer text.
21    pub footer: Option<String>,
22
23    /// Whether the column is sortable.
24    pub sortable: bool,
25
26    /// Whether the column is filterable.
27    pub filterable: bool,
28
29    /// Whether the column is resizable.
30    pub resizable: bool,
31
32    /// Whether the column is visible by default.
33    pub visible: bool,
34
35    /// Minimum column width in pixels.
36    pub min_width: Option<f64>,
37
38    /// Maximum column width in pixels.
39    pub max_width: Option<f64>,
40
41    /// Default column width in pixels.
42    pub default_width: Option<f64>,
43
44    /// Whether this is a group column (contains sub-columns).
45    pub is_group: bool,
46
47    /// Parent column ID for nested columns.
48    pub parent_id: Option<ColumnId>,
49
50    /// Placeholder text for column filter input.
51    pub filter_placeholder: Option<String>,
52}
53
54impl ColumnMeta {
55    /// Creates new column metadata with the given ID and header.
56    ///
57    /// # Parameters
58    ///
59    /// - `id`: The column identifier.
60    /// - `header`: The display header text.
61    ///
62    /// # Returns
63    ///
64    /// - `ColumnMeta`: A new column metadata with default settings.
65    pub fn new(id: impl Into<ColumnId>, header: impl Into<String>) -> Self {
66        Self {
67            id: id.into(),
68            header: header.into(),
69            footer: None,
70            sortable: true,
71            filterable: true,
72            resizable: true,
73            visible: true,
74            min_width: None,
75            max_width: None,
76            default_width: None,
77            is_group: false,
78            parent_id: None,
79            filter_placeholder: None,
80        }
81    }
82
83    /// Sets the footer text.
84    ///
85    /// # Parameters
86    ///
87    /// - `footer`: The footer text to display.
88    ///
89    /// # Returns
90    ///
91    /// - `Self`: The modified column metadata.
92    pub fn with_footer(mut self, footer: impl Into<String>) -> Self {
93        // Set the footer text.
94        self.footer = Some(footer.into());
95        self
96    }
97
98    /// Sets whether the column is sortable.
99    ///
100    /// # Parameters
101    ///
102    /// - `sortable`: Whether sorting is enabled.
103    ///
104    /// # Returns
105    ///
106    /// - `Self`: The modified column metadata.
107    pub fn with_sortable(mut self, sortable: bool) -> Self {
108        // Update the sortable flag.
109        self.sortable = sortable;
110        self
111    }
112
113    /// Sets whether the column is filterable.
114    ///
115    /// # Parameters
116    ///
117    /// - `filterable`: Whether filtering is enabled.
118    ///
119    /// # Returns
120    ///
121    /// - `Self`: The modified column metadata.
122    pub fn with_filterable(mut self, filterable: bool) -> Self {
123        // Update the filterable flag.
124        self.filterable = filterable;
125        self
126    }
127
128    /// Sets whether the column is resizable.
129    ///
130    /// # Parameters
131    ///
132    /// - `resizable`: Whether resizing is enabled.
133    ///
134    /// # Returns
135    ///
136    /// - `Self`: The modified column metadata.
137    pub fn with_resizable(mut self, resizable: bool) -> Self {
138        // Update the resizable flag.
139        self.resizable = resizable;
140        self
141    }
142
143    /// Sets whether the column is visible by default.
144    ///
145    /// # Parameters
146    ///
147    /// - `visible`: Whether the column is visible.
148    ///
149    /// # Returns
150    ///
151    /// - `Self`: The modified column metadata.
152    pub fn with_visible(mut self, visible: bool) -> Self {
153        // Update the visible flag.
154        self.visible = visible;
155        self
156    }
157
158    /// Sets the minimum column width.
159    ///
160    /// # Parameters
161    ///
162    /// - `width`: The minimum width in pixels.
163    ///
164    /// # Returns
165    ///
166    /// - `Self`: The modified column metadata.
167    pub fn with_min_width(mut self, width: f64) -> Self {
168        // Set the minimum width constraint.
169        self.min_width = Some(width);
170        self
171    }
172
173    /// Sets the maximum column width.
174    ///
175    /// # Parameters
176    ///
177    /// - `width`: The maximum width in pixels.
178    ///
179    /// # Returns
180    ///
181    /// - `Self`: The modified column metadata.
182    pub fn with_max_width(mut self, width: f64) -> Self {
183        // Set the maximum width constraint.
184        self.max_width = Some(width);
185        self
186    }
187
188    /// Sets the default column width.
189    ///
190    /// # Parameters
191    ///
192    /// - `width`: The default width in pixels.
193    ///
194    /// # Returns
195    ///
196    /// - `Self`: The modified column metadata.
197    pub fn with_default_width(mut self, width: f64) -> Self {
198        // Set the default width.
199        self.default_width = Some(width);
200        self
201    }
202
203    /// Sets the filter placeholder text.
204    ///
205    /// # Parameters
206    ///
207    /// - `placeholder`: The placeholder text for the filter input.
208    ///
209    /// # Returns
210    ///
211    /// - `Self`: The modified column metadata.
212    pub fn with_filter_placeholder(mut self, placeholder: impl Into<String>) -> Self {
213        // Set the filter placeholder text.
214        self.filter_placeholder = Some(placeholder.into());
215        self
216    }
217
218    /// Marks this column as a group column.
219    ///
220    /// Group columns contain sub-columns and are not sortable or filterable.
221    ///
222    /// # Returns
223    ///
224    /// - `Self`: The modified column metadata.
225    pub fn as_group(mut self) -> Self {
226        // Mark as group and disable sorting and filtering.
227        self.is_group = true;
228        self.sortable = false;
229        self.filterable = false;
230        self
231    }
232
233    /// Sets the parent column ID.
234    ///
235    /// # Parameters
236    ///
237    /// - `parent_id`: The parent column identifier.
238    ///
239    /// # Returns
240    ///
241    /// - `Self`: The modified column metadata.
242    pub fn with_parent(mut self, parent_id: impl Into<ColumnId>) -> Self {
243        // Set the parent column reference.
244        self.parent_id = Some(parent_id.into());
245        self
246    }
247}
248
249/// Provides a default column metadata with empty ID and header.
250impl Default for ColumnMeta {
251    fn default() -> Self {
252        Self::new("", "")
253    }
254}