Dioxus Table
Make data-driven table rendering easy with Dioxus (Live Example)
Installation
Add the following to your Cargo.toml
[]
= "0.1.1"
Quickstart
Attach the derive macro TableData
to a struct that represents a row of a table.
// in mod hotel:
This generates a Dioxus component called Table
in the module hotel
ready to be used in your app.
// in your app:
use ;
And that's it! Easy, right?
You can look at the examples in the examples
directory to get a more complete overview of what dioxus-table can do.
Event Handlers
The generated table component provides two events: Clicking on a row or a head cell. Let's add some event handlers to our previous example.
cx.render
When you click on a head cell or on a data row you'll see some information logged to the console.
Customization
You can customize most aspects of the table rendering. Here is an overview of all available options.
Per-column/field options
Option | Description |
---|---|
class | HTML class(es) added to the <th> and <td> tags |
cell_class | HTML class(es) added only to the <td> cell tag |
head_class | HTML class(es) added only to the <th> head tag |
title | Custom title that is put into the <th> . By default the capitalized field name is used. |
precision | For decimal types this sets the number of digits after the decimal point |
renderer | Custom cell render component |
skip | Don't render this column |
Per-table options
Option | Description |
---|---|
row_class | HTML class(es) added to the <tr> row tags except the first one (the header row) |
head_row_class | Added only to the first <tr> (the header row) |
tag | The HTML tag name used for the root of the table. Defaults to "table" . |
row_renderer | Custom row renderer component |
head_cell_renderer | Custom head cell renderer component |
dyn_row_classes | Enables reactive row classes through the method row_classes() |
Dynamic row classes
A simple way to give feedback to interactions is to add a class to a row element. This makes is easy to
highlight a selected row for example. Above the struct
enable the dyn_row_classes
option.
What classes are added to a row is determined by calling the row_classes()
method. So let's implement it.
The method row_classes()
is called for each row. It receives the index
of the row and the current context.
Please look at the hotel example in the examples
directory for a full example.
Custom renderers
Custom renderers are a powerful way to customize almost all aspects of the table rendering. Yet they are very easy to use.
Custom cell renderer
Probably the most common use for a custom renderer is to customize the representation of a value in a table cell.
Let's say we have a table of books with a title
and a rating
field.
The rating
field is an integer number from 1 to 5 that represents the number of stars a book has received.
With the default renderer we only see a number in the "Rating" column. If we want to display this number as stars we can write a custom renderer.
// The actual renderer component. It has to accept the DefaultTableCellProps.
Now the rating is properly displayed as stars. To see this in action run the hotels example in the examples/
folder.
Custom row and head cell renderers
They work basically the same as the cell renderers but are specified above the struct definition.
To see how to implement them please refer to the default renderers in src/cell_renderers.rs and src/row_enderers.rs. The easiest way to get going is to copy and paste the respective default renderer and customize from there.