ag_grid_rs/
column.rs

1//! Types pertaining to the grid columns.
2
3use ag_grid_derive::{FieldSetter, ToJsValue};
4use wasm_bindgen::prelude::*;
5
6pub use crate::shared::SortMethod;
7use crate::{callbacks::IHeaderValueGetterParams, types::OneOrMany};
8
9#[wasm_bindgen]
10extern "C" {
11    /// A handle for the AG Grid [`Column API`].
12    ///
13    /// [`Column API`]: https://www.ag-grid.com/javascript-data-grid/column-api/
14    pub type ColumnApi;
15}
16
17/// A customisable struct for defining a column.
18#[derive(FieldSetter, ToJsValue)]
19#[js_value(skip_serializing_none)]
20pub struct ColumnDef {
21    // Base
22    #[field_setter(skip)]
23    field: Option<String>,
24
25    /// The unique ID to give the column. This is optional. If missing, the ID
26    /// will default to the field. If both field and colId are missing, a
27    /// unique ID will be generated. This ID is used to identify the column in
28    /// the API for sorting, filtering etc.
29    col_id: Option<String>,
30
31    /// A comma separated string or if using the [`ColumnDef::type_array`]
32    /// method, a vector of strings containing ColumnType keys which can be
33    /// used as a template for a column. This helps to reduce duplication of
34    /// properties when you have a lot of common column properties.
35    type_: Option<OneOrMany<String>>,
36
37    // TODO: support callback function
38    /// A function or expression that gets the value to be displayed from your
39    /// data.
40    value_getter: Option<String>,
41
42    // TODO: support callback function
43    /// A function or an expression to format a value. Not used for CSV export
44    /// or copy to clipboard; only for UI cell rendering.
45    value_formatter: Option<String>,
46
47    /// Provide a reference data map to be used to map column values to their
48    /// respective value from the map.
49    //ref_data: Option<HashMap<String, String>>,
50
51    /// Set to `true` to display a disabled checkbox when row is not selectable
52    /// and checkboxes are enabled.
53    show_disabled_checkboxes: Option<bool>,
54    // TODO
55
56    // Display
57    /// Set to `true` for this column to be hidden.
58    hide: Option<bool>,
59
60    /// Same as [`ColumnDef::hide`], except only applied when creating a new
61    /// column. Not applied when updating column definitions.
62    initial_hide: Option<bool>,
63
64    /// Set to `true` to block making column visible/hidden via the UI (API will
65    /// still work).
66    lock_visible: Option<bool>,
67
68    /// Lock a column to position to `Left` or `Right` to always have this
69    /// column displayed in that position. `True` is treated as `Left`.
70    lock_position: Option<LockPosition>,
71
72    /// Set to `true` if you do not want this column to be movable via dragging.
73    suppress_movable: Option<bool>,
74
75    // Editing
76    /// Set to `true` if this column is editable.
77    editable: Option<bool>, // TODO: add support for a callback function
78
79    /// Set to `true` to have the cell editor appear in a popup.
80    cell_editor_popup: Option<bool>,
81
82    /// Set the position for the popup cell editor. Possible values are `Over`,
83    /// whereby the popup will be positioned over the cell, or `Under`, whereby
84    /// the popup will be positioned below the cell leaving the cell value
85    /// visible.
86    cell_editor_popup_position: Option<PopupPosition>,
87
88    /// Set to `true` to have cells under this column enter edit mode after
89    /// single click.
90    single_click_edit: Option<bool>,
91    // TODO
92
93    // Events
94    // TODO
95
96    // Filter
97    /// Set whether the column is filterable, or use one of the provided
98    /// filters.
99    filter: Option<Filter>,
100
101    /// Whether to display a floating filter for this column.
102    floating_filter: Option<bool>,
103    // TODO
104
105    // Header
106    /// The name to render in the column header. If not specified and field is
107    /// specified, the field name will be used as the header name.
108    header_name: Option<String>,
109
110    /// Get the value for display in the header.
111    header_value_getter: Option<Closure<dyn FnMut(IHeaderValueGetterParams) -> String>>,
112
113    /// Tooltip for the column header.
114    header_tooltip: Option<String>,
115
116    /// CSS class to use for the header cell. Can be a string or, if using the
117    /// `header_class_array` method, a vector of strings.
118    header_class: Option<OneOrMany<String>>,
119
120    /// Set to `true` to wrap long header names onto the next line.
121    wrap_header_text: Option<bool>,
122
123    /// Set to `true` to enable the header row to automatically adjust its
124    /// height to accommodate the size of the header cell.
125    auto_header_height: Option<bool>,
126
127    /// Select which menu tabs are present, and in what order they are shown.
128    menu_tabs: Option<Vec<MenuTab>>,
129
130    /// Set to `true` to disable showing the menu for this column header.
131    suppress_menu: Option<bool>,
132
133    /// If `true`, a 'select all' checkbox will be put into the header.
134    header_checkbox_selection: Option<bool>, // TODO: add support for a callback function
135
136    /// If `true`, the header checkbox selection will only select filtered
137    /// items.
138    header_checkbox_selection_filtered_only: Option<bool>,
139    // TODO
140
141    // Integrated Charts
142    // All options are enterprise-only
143
144    // Pinned
145    /// Pin a column to one side: right or left. A value of `True` is converted
146    /// to `Left`.
147    pinned: Option<PinnedPosition>,
148
149    /// Same as [`ColumnDef::pinned`], except only applied when creating a new
150    /// column. Not applied when updating column definitions.
151    initial_pinned: Option<PinnedPosition>,
152
153    /// Set to `true` to block the user pinning the column, the column can only
154    /// be pinned via definitions or API.
155    lock_pinned: Option<bool>,
156
157    // Pivoting
158    // All options are enterprise-only
159
160    // Rendering and Styling
161    /// Set to `true` to have the grid calculate the height of a row based on
162    /// contents of this column.
163    auto_height: Option<bool>,
164
165    /// Set to `true` to have the text wrap inside the cell - typically used
166    /// with [`ColumnDef::auto_height`].
167    wrap_text: Option<bool>,
168
169    /// Set to `true` to flash a cell when it's refreshed.
170    enable_cell_change_flash: Option<bool>,
171
172    /// Set to `true` to prevent this column from flashing on changes. Only
173    /// applicable if cell flashing is turned on for the grid.
174    suppress_cell_flash: Option<bool>,
175    // TODO
176
177    // Row Dragging
178    /// Set to `true` to allow row dragging.
179    row_drag: Option<bool>, // TODO: add support for callback function
180
181    /// Set to `true` to allow dragging for native drag and drop.
182    dnd_source: Option<bool>, // TODO: add support for callback function
183    // TODO
184
185    // Row Grouping
186    // All options and enterprise-only
187
188    // Sort
189    /// Set wether the column is sortable.
190    sortable: Option<bool>,
191
192    /// Set the default sorting method.
193    sort: Option<SortMethod>,
194
195    /// The same as [`ColumnDef::sort`], except only applied when creating a new
196    /// column. Not applied when updating column definitions.
197    initial_sort: Option<SortMethod>,
198
199    /// If sorting more than one column by default, specifies order in which the
200    /// sorting should be applied.
201    sort_index: Option<Option<u32>>,
202
203    /// Vector defining the order in which sorting occurs (if sorting is
204    /// enabled). Expects a vector of any permutation of the [`SortMethod`]
205    /// variants.
206    sorting_order: Option<Vec<SortMethod>>,
207
208    /// Set to `true` if you want the unsorted icon to be shown when no sort is
209    /// applied to this column.
210    #[js_value(rename = "unSortIcon")]
211    unsort_icon: Option<bool>,
212
213    // Spanning
214    //
215    // TODO: support callback function
216    /// Set the span of the column.
217    col_span: Option<u32>,
218
219    // TODO: support callback function
220    /// Set the span of the row.
221    row_span: Option<u32>,
222
223    // Tooltips
224    /// The field of the tooltip to apply to the cell.
225    tooltip_field: Option<String>, // TODO
226
227    // Width
228    /// Initial width in pixels for the cell.
229    width: Option<u32>,
230
231    /// The same as [`ColumnDef::width`], except only applied when creating a
232    /// new column. Not applied when updating column definitions.
233    initial_width: Option<u32>,
234
235    /// Minimum width in pixels for the cell.
236    min_width: Option<u32>,
237
238    /// Maxmum width in pixels for the cell.
239    max_width: Option<u32>,
240
241    /// Used instead of width when the goal is to fill the remaining empty space
242    /// of the grid.
243    flex: Option<u32>,
244
245    /// The same as [`ColumnDef::flex`], except only applied when creating a new
246    /// column. Not applied when updating column definitions.
247    initial_flex: Option<u32>,
248
249    /// Set to `true` to allow this column to be resized.
250    resizable: Option<bool>,
251
252    /// Set to `true` if you want this column's width to be fixed during 'size
253    /// to fit' operations.
254    suppress_size_to_fit: Option<bool>,
255
256    /// Set to `true` if you do not want this column to be auto-resizable by
257    /// double clicking it's edge.
258    suppress_auto_size: Option<bool>,
259}
260
261impl ColumnDef {
262    /// Create a new column definition, specifying the field of the row object
263    /// to get the cell's data from. Deep references into a row object is
264    /// supported via dot notation, i.e 'address.firstLine'.
265    pub fn new<S: AsRef<str>>(field: S) -> Self {
266        Self {
267            field: Some(field.as_ref().to_string()),
268            ..Default::default()
269        }
270    }
271}
272
273/// Allowed values for [`ColumnDef::filter`][crate::ColumnDef::filter].
274#[derive(ToJsValue)]
275pub enum Filter {
276    /// A filter for number comparisons.
277    AgNumberColumnFilter,
278    /// A filter for string comparisons.
279    AgTextColumnFilter,
280    /// A filter for date comparisons.
281    AgDateColumnFilter,
282    /// A filter influenced by how filters work in Microsoft Excel. This is an
283    /// AG Grid Enterprise feature.
284    AgSetColumnFilter,
285    /// Enable the default filter. The default is Text Filter for AG Grid
286    /// Community and Set Filter for AG Grid Enterprise.
287    #[js_value(serialize_as = "true")]
288    True,
289    /// Explicitly disable filtering.
290    #[js_value(serialize_as = "false")]
291    False,
292    // TODO: Custom(FilterComponent)
293}
294
295/// Allowed values for
296/// [`ColumnDef::lock_position`][crate::ColumnDef::lock_position].
297#[derive(ToJsValue)]
298pub enum LockPosition {
299    #[js_value(serialize_as = "true")]
300    True,
301    #[js_value(serialize_as = "false")]
302    False,
303    Left,
304    Right,
305}
306
307/// Allowed values for
308/// [`ColumnDef::pinned`][crate::ColumnDef::pinned] and
309/// [`ColumnDef::initial_pinned`][crate::ColumnDef::initial_pinned].
310#[derive(ToJsValue)]
311pub enum PinnedPosition {
312    #[js_value(serialize_as = "true")]
313    True,
314    #[js_value(serialize_as = "false")]
315    False,
316    Left,
317    Right,
318}
319
320/// Allowed values for
321/// [`ColumnDef::cell_editor_popup_position`][crate::ColumnDef::cell_editor_popup_position].
322#[derive(ToJsValue)]
323pub enum PopupPosition {
324    Over,
325    Under,
326}
327
328/// Allowed values for
329/// [`ColumnDef::menu_tabs`][crate::ColumnDef::menu_tabs].
330#[allow(clippy::enum_variant_names)]
331#[derive(ToJsValue)]
332pub enum MenuTab {
333    FilterMenuTab,
334    GeneralMenuTab,
335    ColumnsMenuTab,
336}