Code Monkey home page Code Monkey logo

yew-table's Introduction

Yew Table

A simple table component for the Yew web framework.

Usage

Use the Table component by setting the columns, data and options properties:

impl Renderable<Model> for Model {
    fn view(&self) -> Html<Self> {
        // Define the columns. The first string is the field name, the second is the label.
        let columns = columns![
            ("id", "Id.")
            ("description", "Description")
            ("due_date", "Due date")
            ("status", "Status")
            ("is_favorite", "Favorite", "Fav.")
            ("is_archived", "Archived", "Arch.")
        ];

        let options = TableOptions {
            orderable: true,
        };

        html! {
            <>
                // Here, self.tasks is a vector of structs
                <Table<Task>: columns=columns, data=&self.tasks, options=Some(options),/>
            </>
        }
    }
}

Implement the TableData trait for the struct to be used:

#[derive(Default, Clone, PartialEq, Serialize)]
pub struct Task {
    pub id: String,
    // ...
}

impl TableData for Task {
    fn get_field_as_html(&self, field_name: &str) -> Html<Table<Self>> {
        match field_name {
            // Define how each field should be rendered. No restrictions.
            "id" => html! {
                { &self.id }
            },
            // ...
        }
    }

    fn get_field_as_value(&self, field_name: &str) -> TableResult<Value> {
        let value = match field_name {
            // Provide a processed version of your value. Keep the computation cheap! 
            "id" => serde_value::to_value(&self.id),
            // ...
        };
        Ok(value.unwrap())
    }
}

Example

An example Yew app showing a plain table can be found in the examples folder. Just run the contained run.sh script.

To-dos

  • Add sortability
  • Add searchability
  • Improve accessibility
  • Add pagination

License

MIT © 2019 Alexis Luengas

yew-table's People

Contributors

lexluengas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

aknarts gvlpedro

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.