ag_grid_rs/
grid.rs

1//! Types pertaining to the `Grid` itself.
2
3use ag_grid_core::convert::ToJsValue;
4use wasm_bindgen::prelude::*;
5use web_sys::HtmlElement;
6
7use crate::{column::ColumnApi, gridoptions::DataSource};
8
9/// A handle to the underlying JavaScript grid.
10pub struct Grid {
11    // /// The [`GridOptions`] struct used to construct the grid.
12    // pub grid_options: GridOptions<T>,
13    /// A handle for the AG Grid [`Grid API`].
14    ///
15    /// [`Grid API`]: https://www.ag-grid.com/javascript-data-grid/grid-api/
16    pub api: GridApi,
17    /// A handle for the AG Grid [`Column API`].
18    ///
19    /// [`Column API`]: https://www.ag-grid.com/javascript-data-grid/column-api/
20    pub column_api: ColumnApi,
21}
22
23#[wasm_bindgen]
24extern "C" {
25    #[wasm_bindgen(js_name = Grid)]
26    pub(crate) type AgGrid;
27
28    #[wasm_bindgen(js_name = GridOptions)]
29    pub(crate) type AgGridOptions;
30
31    #[wasm_bindgen(constructor, js_namespace = agGrid, js_class = "Grid")]
32    pub(crate) fn new(eGridDiv: HtmlElement, gridOptions: JsValue) -> AgGrid;
33
34    #[wasm_bindgen(method, getter)]
35    pub(crate) fn gridOptions(this: &AgGrid) -> AgGridOptions;
36
37    #[wasm_bindgen(method, getter)]
38    pub(crate) fn api(this: &AgGridOptions) -> GridApi;
39
40    #[wasm_bindgen(method, getter)]
41    pub(crate) fn columnApi(this: &AgGridOptions) -> ColumnApi;
42}
43
44#[wasm_bindgen]
45extern "C" {
46    /// A handle for the AG Grid [`Grid API`].
47    ///
48    /// [`Grid API`]: https://www.ag-grid.com/javascript-data-grid/grid-api/
49    pub type GridApi;
50
51    #[wasm_bindgen(method)]
52    fn exportDataAsCsv(this: &GridApi);
53
54    #[wasm_bindgen(method)]
55    fn setRowData(this: &GridApi, data: JsValue);
56
57    #[wasm_bindgen(method)]
58    fn setDatasource(this: &GridApi, data_source: DataSource);
59}
60
61impl GridApi {
62    /// Download a CSV export of the grid's data.
63    pub fn export_data_as_csv(&self) {
64        Self::exportDataAsCsv(self)
65    }
66
67    /// Set the row data. Applicable when using
68    /// [`RowModelType::ClientSide`][crate::gridoptions::RowModelType::ClientSide].
69    pub fn set_row_data<T>(&self, row_data: Vec<T>)
70    where
71        T: ToJsValue,
72    {
73        Self::setRowData(self, row_data.to_js_value())
74    }
75
76    /// Set a new datasource. Applicable when using
77    /// [`RowModelType::Infinite`][crate::gridoptions::RowModelType::Infinite].
78    pub fn set_data_source(&self, data_source: DataSource) {
79        Self::setDatasource(self, data_source)
80    }
81}