4 releases

0.2.2 Sep 25, 2022
0.2.1 Sep 19, 2022
0.2.0 Sep 19, 2022
0.1.0 Sep 14, 2022

#570 in WebAssembly

Download history 144/week @ 2023-10-18 133/week @ 2023-10-25 139/week @ 2023-11-01 76/week @ 2023-11-08 74/week @ 2023-11-15 33/week @ 2023-11-22 65/week @ 2023-11-29 46/week @ 2023-12-06 64/week @ 2023-12-13 20/week @ 2023-12-20 9/week @ 2023-12-27 17/week @ 2024-01-03 19/week @ 2024-01-10 20/week @ 2024-01-17 31/week @ 2024-01-24 24/week @ 2024-01-31

98 downloads per month

Apache-2.0 OR MIT

66KB
948 lines

ag-grid-rs   Build Status Latest Version Downloads Docs

Rust bindings for the AG Grid JavaScript table library.

Usage

ag-grid-rs aims to follow the API of AG Grid in an unsurprising way, and generally makes use of the builder pattern for constructing the Rust structures.

An example using the Yew frontend framework is shown below.

use ag_grid_rs::{
    gridoptions::{DataSourceBuilder, RowModelType},
    ColumnDef, GridOptions, ToJsValue, 
};
use gloo_net::http::Request;
use serde::Deserialize;
use wasm_bindgen::JsCast;
use web_sys::HtmlElement;
use yew::prelude::*;

#[function_component(About)]
pub fn about() -> Html {
    // Fire the hook just once on initial load
    use_effect_with_deps(
        |_| {
            // Get the element to which you want to attach the grid
            let grid_div = get_element_by_id("grid-div");
            // Define your columns
            let field_names = vec!["athlete", "age", "country", "year"];
            let cols = field_names
                .iter()
                .map(|name| ColumnDef::new(name).sortable(true))
                .collect();

            // Create your datasource, including a closure that will retunr rows from the
            // server
            let data_source = DataSourceBuilder::new(|params| async move {
                // `params` contains information from AG Grid about which rows to get, how to
                // sort the data, etc
                let data_url = "https://www.ag-grid.com/example-assets/olympic-winners.json";
                let rows = gloo_net::http::Request::get(data_url)
                    .send()
                    .await?
                    .json::<Vec<JsonData>>()
                    .await?;

                Ok((rows, None))
            })
            .build();

            let grid = GridOptions::<JsonData>::new()
                .column_defs(cols)
                .row_model_type(RowModelType::Infinite)
                .datasource(data_source)
                .build(grid_div);

            // `grid` now provides a handle to the grid and column APIs
            || ()
        },
        (),
    );

    html! {
        <>
            <div id="grid-div" class="ag-theme-alpine" style="height: 500px"/>
        </>
    }
}

#[derive(ToJsValue, Deserialize)]
struct JsonData {
    athlete: String,
    age: Option<usize>,
    country: String,
    year: usize,
}

fn get_element_by_id(id: &str) -> HtmlElement {
    web_sys::window()
        .expect("unable to get window object")
        .document()
        .expect("unable to get document object")
        .get_element_by_id(id)
        .expect("unable to find grid-div")
        .dyn_into::<HtmlElement>()
        .unwrap()
}

Dependencies

~13MB
~243K SLoC