use serde::Serialize;
use serde_with::skip_serializing_none;
use web_sys::HtmlElement;
use crate::{column::ColumnDef, AgGrid, Grid, RowData, RowModelType};
#[skip_serializing_none]
#[derive(Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GridOptions {
pub column_defs: Option<Vec<ColumnDef>>,
pub default_col_def: Option<ColumnDef>,
pub pagination: Option<bool>,
pub row_model_type: Option<RowModelType>,
pub row_data: Option<Vec<RowData>>,
pub cache_block_size: Option<u32>,
}
impl GridOptions {
pub fn new() -> Self {
Default::default()
}
pub fn build(self, div: HtmlElement) -> Grid {
let grid_options =
serde_wasm_bindgen::to_value(&self).expect("failed converting GridOptions to JsValue");
let js_grid = AgGrid::new(div, grid_options);
Grid {
grid_options: self,
api: js_grid.gridOptions().api(),
column_api: js_grid.gridOptions().columnApi(),
}
}
pub fn column_defs(mut self, column_defs: Vec<ColumnDef>) -> Self {
self.column_defs = Some(column_defs);
self
}
pub fn default_col_def(mut self, col_def: ColumnDef) -> Self {
self.default_col_def = Some(col_def);
self
}
pub fn row_data(mut self, row_data: Vec<RowData>) -> Self {
self.row_data = Some(row_data);
self
}
pub fn row_model_type(mut self, row_model_type: RowModelType) -> Self {
self.row_model_type = Some(row_model_type);
self
}
pub fn pagination(mut self, pagination: bool) -> Self {
self.pagination = Some(pagination);
self
}
pub fn cache_block_size(mut self, cache_block_size: u32) -> Self {
self.cache_block_size = Some(cache_block_size);
self
}
}