Code Monkey home page Code Monkey logo

ag-grid-rs's Introduction

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.

Standalone examples can be found in the examples/ directory, and a basic example using the Yew frontend framework is shown below.

First, make sure you have the JavaScript AG Grid library available to the web page by including a CDN link in your base HTML page:

// index.html

<!doctype html>
<html lang="en">
    <head>
        <!-- snip -->
        <script src="https://unpkg.com/ag-grid-community/dist/ag-grid-community.min.js"></script>
    </head>
    <!-- snip -->
</html>

Then, in your application:

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(Table)]
pub fn table() -> 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().field(name).sortable(true))
                .collect();

            // Create your datasource, including a closure that will return 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 = 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()
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.