leptos-struct-table 0.17.0

Generate a complete batteries included leptos data table component from a struct definition.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# Leptos Struct Table

[![Crates.io](https://img.shields.io/crates/v/leptos-struct-table.svg)](https://crates.io/crates/leptos-struct-table)
[![Docs](https://docs.rs/leptos-struct-table/badge.svg)](https://docs.rs/leptos-struct-table/)
[![MIT/Apache 2.0](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)](https://github.com/synphonyte/leptos-struct-table#license)
[![Build Status](https://github.com/synphonyte/leptos-struct-table/actions/workflows/cd.yml/badge.svg)](https://github.com/synphonyte/leptos-struct-table/actions/workflows/cd.yml)

<!-- cargo-rdme start -->

Easily create Leptos table components from structs.

![Hero Image](https://raw.githubusercontent.com/synphonyte/leptos-struct-table/master/hero.webp)

## Features

- **Easy to use** - yet powerful.
- **Async data loading** - The data is loaded asynchronously. This allows to load data from a REST API or a database etc.
- **Selection** - Can be turned off or single/multi select
- **Customization** - You can customize every aspect of the table by plugging in your own components for rendering rows, cells, headers. See [Custom Renderers]#custom-renderers for more information.
- **Headless** - No default styling is applied to the table. You can fully customize the classes that are applied to the table. See [Classes customization]#classes-customization for more information.
- **Sorting** - Optional. If turned on: Click on a column header to sort the table by that column. You can even sort by multiple columns.
- **Virtualization** - Only the visible rows are rendered. This allows for very large tables.
- **Pagination** - Instead of virtualization you can paginate the table.
- **Caching** - Only visible rows are loaded and cached.
- **Editing** - Optional. You can provide custom renderers for editable cells. See [Editable Cells]#editable-cells for more information.

## Usage

```rust
use leptos::prelude::*;
use leptos_struct_table::*;

#[derive(TableRow, Clone)]
#[table(impl_vec_data_provider)]
pub struct Person {
    id: u32,
    name: String,
    age: u32,
}

#[component]
fn Demo() -> impl IntoView {
    let rows = vec![
        Person { id: 1, name: "John".to_string(), age: 32 },
        Person { id: 2, name: "Jane".to_string(), age: 28 },
        Person { id: 3, name: "Bob".to_string(), age: 45 },
    ];

    view! {
        <table>
            <TableContent rows scroll_container="html" />
        </table>
    }
}
```

## Leptos Compatibility

| Crate version        | Compatible Leptos version |
|----------------------|---------------------------|
| <= 0.2               | 0.3                       |
| 0.3                  | 0.4                       |
| 0.4, 0.5, 0.6        | 0.5                       |
| 0.7 – 0.12           | 0.6                       |
| 0.14.0-beta          | 0.7                       |
| 0.15, 0.16, 0.17     | 0.8                       |

## Server-Side Rendering

To use this with Leptos' server-side rendering, you can have to add `leptos-use` as a dependency to your `Cargo.toml` and
then configure it for SSR like the following.

```toml
[dependencies]
leptos-use = "<current version>"
# ...

[features]
hydrate = [
    "leptos/hydrate",
    # ...
]
ssr = [
    "leptos/ssr",
    # ...
    "leptos-use/ssr",
]
```

Please see the [serverfn_sqlx example](https://github.com/Synphonyte/leptos-struct-table/blob/master/examples/serverfn_sqlx/Cargo.toml)
for a working project with SSR.

## Data Providers

As shown in the initial usage example, when you add `#[table(impl_vec_data_provider)]` to your struct,
the table will automatically generate a data provider for you. You can then directly pass a `Vec<T>` to the `rows` prop.
Internally this implements the trait [`TableDataProvider`] for `Vec<T>`.

To leverage the full power of async partial data loading with caching you should implement the trait
[`PaginatedTableDataProvider`] or the trait [`TableDataProvider`] yourself. It's quite easy to do so.
Which of the two traits you choose depends on your data source. If your data source provides
paginated data, as is the case for many REST APIs, you should implement [`PaginatedTableDataProvider`].
Otherwise you should probably implement [`TableDataProvider`].

See the [paginated_rest_datasource example](https://github.com/Synphonyte/leptos-struct-table/blob/master/examples/paginated_rest_datasource/src/data_provider.rs)
and the [serverfn_sqlx example](https://github.com/Synphonyte/leptos-struct-table/blob/master/examples/serverfn_sqlx/src/data_provider.rs)
for working demo projects that implement these traits.

## Macro options

The `#[table(...)]` attribute can be used to customize the generated component. The following options are available:

### Struct attributes

These attributes can be applied to the struct itself.

- **`sortable`** - Specifies that the table should be sortable. This makes the header titles clickable to control sorting.
  You can specify two sorting modes with the prop `sorting_mode` on the `TableContent` component:
  - `sorting_mode=SortingMode::MultiColumn` (the default) allows the table to be sorted by multiple columns ordered by priority.
  - `sorting_mode=SortingMode::SingleColumn"` allows the table to be sorted by a single column. Clicking on another column will simply replace the sorting column.

  See the [simple example]https://github.com/synphonyte/leptos-struct-table/blob/master/examples/simple/src/main.rs and the
  [selectable example]https://github.com/synphonyte/leptos-struct-table/blob/master/examples/selectable/src/main.rs for more information.
- **`classes_provider`** - Specifies the name of the class provider. Used to quickly customize all of the classes that are applied to the table.
  For convenience sensible presets for major CSS frameworks are provided. See [`TableClassesProvider`] and [tailwind example]https://github.com/synphonyte/leptos-struct-table/blob/master/examples/tailwind/src/main.rs for more information.
- **`head_cell_renderer`** - Specifies the name of the header cell renderer component. Used to customize the rendering of header cells. Defaults to [`DefaultTableHeaderRenderer`]. See the [custom_renderers_svg example]https://github.com/Synphonyte/leptos-struct-table/blob/master/examples/custom_renderers_svg/src/main.rs for more information.
- **`impl_vec_data_provider`** - If given, then [`TableDataProvider`] is automatically implemented for `Vec<ThisStruct>` to allow
  for easy local data use. See the [simple example]https://github.com/synphonyte/leptos-struct-table/blob/master/examples/simple/src/main.rs for more information.
- **`row_type`** - Specifies the type of the rows in the table. Defaults to the struct that this is applied to. See the [custom_type example]https://github.com/synphonyte/leptos-struct-table/blob/master/examples/custom_type/src/main.rs for more information.
- **`column_index_type`** - A type by which the columns are indexed, "usize" is the default. "enum" will generate an enum with the row-struct's field names as variants. See the [column_index_type example]https://github.com/synphonyte/leptos-struct-table/blob/master/examples/column_index_type/src/main.rs for more information.
- **`i18n`** - Allows to specify the i18n scope for all fields of the struct as well as the `i18n` module path which defaults to `crate::i18n`. See [I18n]#i18n for more information.

### Field attributes

These attributes can be applied to any field in the struct.

- **`class`** - Specifies the classes that are applied to each cell (head and body) in the field's column. Can be used in conjunction with `classes_provider` to customize the classes.
- **`head_class`** - Specifies the classes that are applied to the header cell in the field's column. Can be used in conjunction with `classes_provider` to customize the classes.
- **`cell_class`** - Specifies the classes that are applied to the body cells in the field's column. Can be used in conjunction with `classes_provider` to customize the classes.
- **`skip`** - Specifies that the field should be skipped. This is useful for fields that are not displayed in the table.
- **`skip_sort`** - Only applies if `sortable` is set on the struct. Specifies that the field should not be used for sorting. Clicking it's header will not do anything.
- **`skip_header`** - Makes the title of the field not be displayed in the head row.
- **`title`** - Specifies the title that is displayed in the header cell. Defaults to the field name converted to title case (`this_field` becomes `"This Field"`).
- **`renderer`** - Specifies the name of the cell renderer component. Used to customize the rendering of cells.
  Defaults to [`DefaultTableCellRenderer`].
 - **`format`** - Quick way to customize the formatting of cells without having to create a custom renderer. See [Formatting]#formatting below for more information.
- **`getter`** - Specifies a method that returns the value of the field instead of accessing the field directly when rendering.
- **`none_value`** - Specifies a display value for `Option` types when they are `None`. Defaults to empty string
- **`i18n`** - Overrides the i18n key for the field. See [I18n]#i18n for more information.

#### Formatting

The `format` attribute can be used to customize the formatting of cells. It is an easier alternative to creating a custom renderer when you just want to customize some basic formatting.
It is type safe and tied to the type the formatting is applied on. see [`CellValue`] and the associated type for the type you are rendering to see a list of options

See:
- [`cell_value::NumberRenderOptions`]


## Features

- **`chrono`** - Adds support for types from the crate `chrono`.
- **`rust_decimal`** - Adds support for types from the crate `rust_decimal`.
- **`time`** - Adds support for types from the crate `time`.
- **`uuid`** - Adds support for types from the crate `uuid`.

## Classes Customization

Classes can be easily customized by using the `classes_provider` attribute on the struct.
You can specify any type that implements the trait [`TableClassesProvider`]. Please see the documentation for that trait for more information.
You can also look at [`TailwindClassesPreset`] for an example how this can be implemented.

Example:

```rust
#[derive(TableRow, Clone)]
#[table(classes_provider = "TailwindClassesPreset")]
pub struct Book {
    id: u32,
    title: String,
}
```

## Field Getters

Sometimes you want to display a field that is not part of the struct but a derived value either
from other fields or sth entirely different. For this you can use either the [`FieldGetter`] type
or the `getter` attribute.

Let's start with [`FieldGetter`] and see an example:

```rust
#[derive(TableRow, Clone)]
#[table(classes_provider = "TailwindClassesPreset")]
pub struct Book {
    id: u32,
    title: String,
    author: String,

    // this tells the macro that you're going to provide a method called `title_and_author` that returns a `String`
    title_and_author: FieldGetter<String>
}

impl Book {
    // Returns the value that is displayed in the column
    pub fn title_and_author(&self) -> String {
        format!("{} by {}", self.title, self.author)
    }
}
```

To provide maximum flexibility you can use the `getter` attribute.

```rust
#[derive(TableRow, Clone)]
#[table(classes_provider = "TailwindClassesPreset")]
pub struct Book {
    // this tells the macro that you're going to provide a method called `get_title` that returns a `String`
    #[table(getter = "get_title")]
    title: String,
}

impl Book {
    pub fn get_title(&self) -> String {
        format!("Title: {}", self.title)
    }
}
```

### When to use `FieldGetter` vs `getter` attribute

A field of type `FieldGetter<T>` is a virtual field that doesn't really exist on the struct.
Internally `FieldGetter` is just a new-typed `PhantomData` and thus is removed during compilation.
Hence it doesn't increase memory usage. That means you should use it for purely derived data.

The `getter` attribute should be used on a field that actually exists on the struct but whose
value you want to modify before it's rendered.

## Custom Renderers

Custom renderers can be used to customize almost every aspect of the table.
They are specified by using the various `...renderer` attributes on the struct or fields or props of the [`TableContent`] component.
To implement a custom renderer please have a look at the default renderers listed below.

On the struct level you can use this attribute:
- **`thead_cell_renderer`** - Defaults to [`DefaultTableHeaderCellRenderer`] which renders `<th><span>Title</span></th>`
  together with sorting functionality (if enabled).

As props of the [`TableContent`] component you can use the following:
- **`thead_renderer`** - Defaults to [`DefaultTableHeadRenderer`] which just renders the tag `thead`.
- **`thead_row_renderer`** - Defaults to [`DefaultTableHeadRowRenderer`] which just renders the tag `tr`.
- **`tbody_renderer`** - Defaults to the tag `tbody`. Takes no attributes.
- **`row_renderer`** - Defaults to [`DefaultTableRowRenderer`].
- **`loading_row_renderer`** - Defaults to [`DefaultLoadingRowRenderer`].
- **`error_row_renderer`** - Defaults to [`DefaultErrorRowRenderer`].
- **`row_placeholder_renderer`** - Defaults to [`DefaultRowPlaceholderRenderer`].

On the field level you can use the **`renderer`** attribute.

It defaults to [`DefaultTableCellRenderer`]
Works for any type that implements the [`CellValue`] trait that is implemented for types in the standard library, popular crates with feature flags and for your own type if you implement this trait for them.

Example:

```rust
#[derive(TableRow)]
pub struct Book {
    title: String,
    #[table(renderer = "ImageTableCellRenderer")]
    img: String,
}

// Easy cell renderer that just displays an image from an URL.
#[component]
fn ImageTableCellRenderer(
    class: String,
    value: Signal<String>,
    row: RwSignal<Book>,
    index: usize,
) -> impl IntoView
{
    view! {
        <td class=class>
            <img src=value alt="Book image" height="64"/>
        </td>
    }
}
```

For more detailed information please have a look at the [custom_renderers_svg example](https://github.com/synphonyte/leptos-struct-table/blob/master/examples/custom_renderers_svg/src/main.rs) for a complete customization.


### Editable Cells

You might have noticed the prop `row` in the custom cell renderer above. This can be used
to edit the data. Simply use the `RwSignal` to access the row and change the fields.

```rust
#[derive(TableRow, Clone, Default, Debug)]
#[table(impl_vec_data_provider)]
pub struct Book {
    id: u32,
    #[table(renderer = "InputCellRenderer")]
    title: String,
}

#[component]
fn InputCellRenderer(
    class: String,
    value: Signal<String>,
    row: RwSignal<Book>,
    index: usize,
) -> impl IntoView {
    let on_change = move |evt| {
        row.write().title = event_target_value(&evt);
    };

    view! {
        <td class=class>
            <input type="text" value=value on:change=on_change />
        </td>
    }
}

// Then in the table component you can listen to the `on_change` event:

#[component]
pub fn App() -> impl IntoView {
    let rows = vec![Book::default(), Book::default()];

    let on_change = move |evt: ChangeEvent<Book>| {
        logging::log!("Changed row at index {}:\n{:#?}", evt.row_index, evt.changed_row.get_untracked());
    };

    view! {
        <table>
            <TableContent rows on_change scroll_container="html" />
        </table>
    }
}
```

Please have a look at the [editable example](https://github.com/Synphonyte/leptos-struct-table/tree/master/examples/editable/src/main.rs) for a fully working example.

## Column index type
Configured via the table annotation on a TableRow struct.

```rust
#[table(columne_index_type = value)]
```

Current supported column index type **values**: `"usize"` or `"enum"`.\
The column type is used to refer to columns in various places, some of which listed below:
 - Custom cell renderers via [`DefaultTableCellRendererProps#index`]DefaultTableCellRendererProps#structfield.index
 - Custom header cell renderers via [`DefaultTableHeaderCellRendererProps#index`]DefaultTableHeaderCellRendererProps#structfield.index
 - Head events via [`TableHeadEvent#index`]TableHeadEvent#structfield.index
 - In [`TableRow#col_name`]TableRow#tymethod.col_name as `col_index` parameter type.
 - In [`get_sorting_for_column`] in both parameters.

### usize column index type
This is the default index type.

It can be set explicitely:
```rust
#[derive(TableRow, Clone, Default, Debug)]
#[table(impl_vec_data_provider, column_index_type = "usize")]
pub struct Book {
    id: u32, // index = 0
    #[table(skip)]
    content: String, // no index (skipped)
    title: String, // index = 1
}
```
Usize indexes start at 0 at the first relevant struct field. Fields marked `skip` do not have an index.

### Enum column index type

Used as follows:
```rust
#[derive(TableRow, Clone, Default, Debug)]
#[table(impl_vec_data_provider, column_index_type = "enum")]
// Proc-macro `table` generates enum "{struct_name}Column", in this case: BookColumn
pub struct Book {
    id: u32, // index = BookColumn::Id
    #[table(skip)]
    content: String, // no index (skipped)
    title: String, // index = BookColumn::Title
}
```

Fields are converted to UpperCammelCase for their generated enum variant.
See the [column_index_type example](https://github.com/synphonyte/leptos-struct-table/blob/master/examples/column_index_type/src/main.rs) for more information.

## Pagination / Virtualization / InfiniteScroll

This table component supports different display acceleration strategies. You can set them through the `display_strategy` prop of
the [`TableContent`] component.

The following options are available. Check their docs for more details.
- [`DisplayStrategy::Virtualization`] default
- [`DisplayStrategy::InfiniteScroll`]
- [`DisplayStrategy::Pagination`]

Please have a look at the [pagination example](https://github.com/Synphonyte/leptos-struct-table/tree/master/examples/pagination/src/main.rs) for more information on how to use pagination.

## I18n

To translate the column titles of the table using `leptos-i18n` you can enable the `"i18n"`
feature. The field names of the struct are used as keys by default and can be customized using the `i18n` attribute.

Please have a look at the
[i18n example](https://github.com/Synphonyte/leptos-struct-table/tree/master/examples/i18n)
and at the sections [Struct attributes](#struct-attributes) and
[Field attributes](#field-attributes) for more information.

## Contribution

All contributions are welcome. Please open an issue or a pull request if you have any ideas or problems.

<!-- cargo-rdme end -->