日本語

Filtering and pages

A field that filters the rows of a table, pages of a fixed number of rows, and columns shown only while a condition holds.

Result

table_filtering.rs
use hypersig::html::div;
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{NumberFormat, Page, Signals};
use hypersig_ui::{Column, DataTable, TableState, TableText, switch};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Payment {
    id: i32,
    email: String,
    amount: f64,
}

#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Ledger {
    payments: Vec<Payment>,
    show_ids: bool,
    table: TableState,
}

pub(super) fn page() -> Page<Ledger> {
    let payments = [
        ("ken99@example.com", 316.0),
        ("abe45@example.com", 242.0),
        ("monserrat44@example.com", 837.0),
        ("silas22@example.com", 874.0),
        ("carmella@example.com", 721.0),
        ("jason78@example.com", 450.0),
        ("sarah23@example.com", 120.5),
    ]
    .into_iter()
    .zip(1..)
    .map(|((email, amount), id)| Payment {
        id,
        email: email.to_owned(),
        amount,
    })
    .collect();
    let table = DataTable::new(
        "Payments",
        Ledger::table(),
        Ledger::payments(),
        |payment| payment.at(Payment::id()),
    )
    .filter(
        "Filter emails",
        |payment| payment.at(Payment::email()),
    )
    .paginate(4)
    .text(TableText {
        empty: "No results."
            .to_owned(),
        previous_page: "Previous page"
            .to_owned(),
        next_page: "Next page"
            .to_owned(),
        ..TableText::default()
    })
    .column(Column::number("Id", Payment::id()).visible(Ledger::show_ids()))
    .column(Column::text(
        "Email",
        Payment::email(),
    ))
    .column(Column::formatted(
        "Amount",
        Payment::amount(),
        NumberFormat::new().digits(2).prefix("$"),
    ));
    Page::new(
        "Payments",
        Ledger {
            payments,
            show_ids: false,
            table: TableState::default(),
        },
    )
    .child(
        div()
            .css(stack(rem(1)))
            .child(switch(
                "Show ids",
                Ledger::show_ids(),
            ))
            .child(table),
    )
}

Accessibility

The filter is a native <input type="search"> named by its label through aria-label, which is also its placeholder. The range of rows shown, such as 1–4 / 7, sits in an aria-live="polite" paragraph, so a screen reader reads the new range after the visitor types or changes page. The page buttons are native buttons named by TableText, and the one with no page to go to is disabled.

KeyWhat it does
TabMoves focus to the filter field, then through the table, then to the page buttons.
Enter, SpaceOn a page button, moves to that page.

Filtering the rows

filter adds a field above the table and shows only the rows whose text contains what the visitor types:

impl<S: 'static, T: 'static> DataTable<S, T> {
    pub fn filter<E: IntoExpr<S, String>>(
        self,
        label: impl Into<String>,
        text: impl Fn(Item<S, T>) -> E + 'static,
    ) -> Self;
}

text returns the text of a row the filter searches, such as one field or several joined with concat. The browser trims the visitor's text, compares both in lower case, and keeps a row whose text contains it, so KEN finds ken99@example.com. Typing returns the table to its first page, and the field's text is the query of the table's TableState. The text must be a String expression, so a number field is E0277:

use hypersig::Signals;
use hypersig_ui::{DataTable, TableState};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Payment {
    id: i32,
    amount: f64,
}

#[derive(Signals, Serialize, Deserialize)]
struct Ledger {
    payments: Vec<Payment>,
    table: TableState,
}

// error[E0277]: `Sig<Ledger, f64, Local>` is not an expression of type `String`
//     on the page whose signals are `Ledger`
let table = DataTable::new("Payments", Ledger::table(), Ledger::payments(), |payment| {
    payment.at(Payment::id())
})
.filter("Filter amounts", |payment| payment.at(Payment::amount()));

Pages of rows

paginate shows size of the rows that pass the filter at a time, with the range shown and buttons for the previous and next page below the table:

impl<S: 'static, T: 'static> DataTable<S, T> {
    pub fn paginate(self, size: i32) -> Self;
}

The page is the page of the TableState, counted from 0, so a page of the application can open the table at a page by its first value, or move it with a statement. The range reads first–last / count, such as 5–7 / 7 on the second page of seven rows four at a time. A page past the last row, which the list reaches when it shrinks through a patch or a statement, returns the table to its first page.

size is a plain i32, so the types stop no mistake here. A size below 1 panics where paginate is called:

use hypersig::Signals;
use hypersig_ui::{DataTable, TableState};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Payment {
    id: i32,
}

#[derive(Signals, Serialize, Deserialize)]
struct Ledger {
    payments: Vec<Payment>,
    table: TableState,
}

// Panics: a page holds at least one row, not 0.
let table = DataTable::new("Payments", Ledger::table(), Ledger::payments(), |payment| {
    payment.at(Payment::id())
})
.paginate(0);

Columns shown on a condition

Column::visible shows a column, its header and its cells, only while an expression is true:

impl<S: 'static, T: 'static> Column<S, T> {
    pub fn visible(self, when: impl IntoExpr<S, bool>) -> Self;
}

The expression reads the page's signals, such as a bool that a switch is bound to, as in the demo. A signal of another type is E0277:

use hypersig::Signals;
use hypersig_ui::Column;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Payment {
    id: i32,
}

#[derive(Signals, Serialize, Deserialize)]
struct Ledger {
    payments: Vec<Payment>,
    columns: String,
}

// error[E0277]: `Sig<Ledger, String>` is not an expression of type `bool`
//     on the page whose signals are `_`
let id: Column<Ledger, Payment> = Column::number("Id", Payment::id()).visible(Ledger::columns());

The HTML

The table of the example below, with its Id column hidden, renders the filter above the table and the range and buttons below it:

<div>
  <input type="search" aria-label="Filter emails" placeholder="Filter emails" value="">
  <div>
    <table aria-label="Payments">…</table>
  </div>
  <div>
    <p aria-live="polite">1–4 / 7</p>
    <button type="button" aria-label="Previous page" disabled><svg …></svg></button>
    <button type="button" aria-label="Next page"><svg …></svg></button>
  </div>
</div>

Every row is in the HTML, including those the filter or the page hides, and the browser shows only the rows that pass. The Id header and cells are in it too, hidden while the switch is off.

What is not checked

The browser counts the rows on each change of the list, the filter, or the page, and with a filter and pages both on, the work grows with the square of the number of rows. The crate has not measured where that becomes slow, and a list of thousands of rows belongs in a Server table, which receives one page at a time. The filter matches text only as written: it ignores case but not accents, and finds ken in broken.

Example: payments by email

A filter on the email, four rows a page, and an Id column that a switch shows:

use hypersig::html::div;
use hypersig::{NumberFormat, Page, Signals};
use hypersig_ui::{Column, DataTable, TableState, switch};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Payment {
    id: i32,
    email: String,
    amount: f64,
}

#[derive(Signals, Serialize, Deserialize)]
struct Ledger {
    payments: Vec<Payment>,
    show_ids: bool,
    table: TableState,
}

const DOLLARS: NumberFormat = NumberFormat::new().digits(2).prefix("$");

let table = DataTable::new("Payments", Ledger::table(), Ledger::payments(), |payment| {
    payment.at(Payment::id())
})
.filter("Filter emails", |payment| payment.at(Payment::email()))
.paginate(4)
.column(Column::number("Id", Payment::id()).visible(Ledger::show_ids()))
.column(Column::text("Email", Payment::email()))
.column(Column::formatted("Amount", Payment::amount(), DOLLARS));

let payments = (1..=7)
    .map(|id| Payment { id, email: format!("user{id}@example.com"), amount: 100.0 * f64::from(id) })
    .collect();
let state = Ledger { payments, show_ids: false, table: TableState::default() };
let html = Page::new("Payments", state)
    .child(div().child(switch("Show ids", Ledger::show_ids())).child(table))
    .render();

Next, Selecting and expanding rows adds checkboxes and row details.