Table of Contents

Class TwTable

Namespace
TwBlazor.Components
Assembly
TwBlazor.dll

Represents a customizable table component for Blazor applications that enables rendering of table headers, body, and footers with configurable styling and behaviour.

public class TwTable : TwBlazorComponentBase, IComponent, IHandleEvent, IHandleAfterRender, ITwComponent, IAsyncDisposable
Inheritance
TwTable
Implements
Inherited Members

Remarks

The TwTable component provides flexibility for displaying tabular data by allowing developers to supply custom content for the header, body, and footer sections using RenderFragment parameters. It supports additional styling options such as striped rows, hover effects, and borders, which can be controlled through component parameters. Additional CSS classes can be applied to each section for further customization. This component is intended to be used as a building block for creating accessible and visually consistent tables in Blazor projects.

Constructors

TwTable()

public TwTable()

Properties

AnnouncePageChanges

Whether the pagination announces the new page to screen readers. Defaults to true. Turn it off when you announce page changes yourself.

[Parameter]
public bool AnnouncePageChanges { get; set; }

Property Value

bool

BodyClass

Additional CSS classes for the body section.

[Parameter]
public string? BodyClass { get; set; }

Property Value

string

Bordered

Whether to show borders on all cells.

[Parameter]
public bool Bordered { get; set; }

Property Value

bool

Caption

Optional caption content for the table, rendered as a <caption> element. Provides an accessible name/description for the table that is programmatically associated with it. Visually hidden by default (via the sr-only class) unless CaptionVisible is set to true.

[Parameter]
public RenderFragment? Caption { get; set; }

Property Value

RenderFragment

CaptionVisible

Whether the Caption should be visible. Defaults to false, rendering the caption visually hidden (but still available to assistive technology).

[Parameter]
public bool CaptionVisible { get; set; }

Property Value

bool

ColumnGroup

Optional column definitions, rendered inside a <colgroup> before the table sections. Use TwTableColumn to give columns a width, so long or breakable content (such as inline code) cannot squeeze a column narrower than its content needs.

[Parameter]
public RenderFragment? ColumnGroup { get; set; }

Property Value

RenderFragment

ColumnsResized

Raised once a person finishes resizing, with the new width of every column, keyed by each TwTableColumn's Id.

[Parameter]
public EventCallback<IReadOnlyDictionary<string, string>> ColumnsResized { get; set; }

Property Value

EventCallback<IReadOnlyDictionary<string, string>>

ComfortablePagination

Whether the pagination uses regular size buttons instead of the default compact ones. Defaults to false.

[Parameter]
public bool ComfortablePagination { get; set; }

Property Value

bool

CurrentPage

Gets or sets the one based page shown when EnablePagination is set. Bind it with @bind-CurrentPage to follow or change the page. Defaults to 1.

[Parameter]
public int CurrentPage { get; set; }

Property Value

int

CurrentPageChanged

Raised when the page changes, including when it moves back because the rows ran out.

[Parameter]
public EventCallback<int> CurrentPageChanged { get; set; }

Property Value

EventCallback<int>

EnablePagination

Whether to split the rows of a TwTableRows<TItem> inside the table into pages, with a TwPagination below the table. Defaults to false. Ignored while EnableVirtualization is set, since the rows then scroll instead.

[Parameter]
public bool EnablePagination { get; set; }

Property Value

bool

EnableVirtualization

Whether to render only the rows of a TwTableRows<TItem> that are scrolled into view, so a table of thousands of rows stays fast. The rows scroll inside the table, which is MaxHeight tall (32rem unless set), and the header stays in view. Defaults to false.

[Parameter]
public bool EnableVirtualization { get; set; }

Property Value

bool

Remarks

Every row should be about ItemSize pixels tall, so keep cell content to one line. When the rows use RowTemplate and put its AriaRowIndex on each row, the table tells assistive technology how many rows it has, so a screen reader still reads "row 5000 of 10000".

FixedLayout

Whether column widths come from the ColumnGroup and the header row instead of the cell content (CSS table-layout: fixed). Columns keep the same width however their content changes, which suits tables whose rows are paged, filtered or loaded on demand. Defaults to false.

[Parameter]
public bool FixedLayout { get; set; }

Property Value

bool

FooterClass

Additional CSS classes for the footer section.

[Parameter]
public string? FooterClass { get; set; }

Property Value

string

HeaderClass

Additional CSS classes for the header section.

[Parameter]
public string? HeaderClass { get; set; }

Property Value

string

HeaderRowCount

Gets or sets how many rows the header holds, used to number the body rows for assistive technology when EnableVirtualization is set. Defaults to 1.

[Parameter]
public int HeaderRowCount { get; set; }

Property Value

int

Hoverable

Whether to show hover effect on rows.

[Parameter]
public bool Hoverable { get; set; }

Property Value

bool

ItemSize

Gets or sets the height of a row in pixels when EnableVirtualization is set, used to work out which rows are in view before they render. Defaults to 41, the height of a row in the default theme.

[Parameter]
public float ItemSize { get; set; }

Property Value

float

MaxHeight

Gets or sets the tallest the table can grow before its rows scroll inside it, as any CSS length such as 24rem or 60vh. Leave it unset for the table to be as tall as its rows. Pair it with StickyHeader to keep the column headings in view.

[Parameter]
public string? MaxHeight { get; set; }

Property Value

string

NoBorder

Whether to hide the outer table border.

[Parameter]
public bool NoBorder { get; set; }

Property Value

bool

OverscanCount

Gets or sets how many extra rows to render above and below the ones in view when EnableVirtualization is set, so fast scrolling does not show gaps. Defaults to 5.

[Parameter]
public int OverscanCount { get; set; }

Property Value

int

PageSize

Gets or sets how many rows a page holds when EnablePagination is set. Defaults to 10.

[Parameter]
public int PageSize { get; set; }

Property Value

int

PageSizeChanged

Raised when a person picks a different page size.

[Parameter]
public EventCallback<int> PageSizeChanged { get; set; }

Property Value

EventCallback<int>

PageSizeOptions

Gets or sets the page sizes a person can pick from. Defaults to 5, 10, 25, 50 and 100.

[Parameter]
public int[] PageSizeOptions { get; set; }

Property Value

int[]

PaginationLabel

Gets or sets the accessible name of the pagination. Defaults to the localized Table_Pagination text ("Table pagination").

[Parameter]
public string? PaginationLabel { get; set; }

Property Value

string

ResizableColumns

Whether people can resize the columns defined in ColumnGroup by dragging the border at the end of a column's header, or by focusing that border and pressing the arrow keys. Turns on FixedLayout so a column keeps the width it is given. Defaults to false.

[Parameter]
public bool ResizableColumns { get; set; }

Property Value

bool

Remarks

Each border is a focusable separator that reports the column's width. Use Label to name it after the column, and Resizable to keep a column at its width. A resize gives every column its current width in pixels, so the others stay put.

RowGroups

Content made of several <tbody> elements, one for each group of rows, rendered in place of TableBody. The context is the body's CSS classes (including Striped, Hoverable and Bordered), so put it on every <tbody> to style the groups like a single body. Start each group with a header cell that has scope="rowgroup", and assistive technology announces it with every row in the group.

[Parameter]
public RenderFragment<string>? RowGroups { get; set; }

Property Value

RenderFragment<string>

ScrollRegionLabel

Gets or sets the accessible name of the scrollable region that wraps the table. Defaults to the localized Table_ScrollRegion text ("Scrollable table").

[Parameter]
public string? ScrollRegionLabel { get; set; }

Property Value

string

ShowPageSize

Whether the pagination shows its page size list. Defaults to true.

[Parameter]
public bool ShowPageSize { get; set; }

Property Value

bool

StickyHeader

Whether the header row stays at the top of the table while its rows scroll beneath it. Only has an effect when the table scrolls, for example with MaxHeight. Defaults to false.

[Parameter]
public bool StickyHeader { get; set; }

Property Value

bool

Striped

Whether to show striped rows.

[Parameter]
public bool Striped { get; set; }

Property Value

bool

TableBody

Content to render in the table body (tbody).

[Parameter]
public RenderFragment? TableBody { get; set; }

Property Value

RenderFragment

TableFooter

Content to render in the table footer (tfoot).

[Parameter]
public RenderFragment? TableFooter { get; set; }

Property Value

RenderFragment

TableHeader

Content to render in the table header (thead).

[Parameter]
public RenderFragment? TableHeader { get; set; }

Property Value

RenderFragment

Methods

BuildRenderTree(RenderTreeBuilder)

Renders the component to the supplied RenderTreeBuilder.

protected override void BuildRenderTree(RenderTreeBuilder __builder)

Parameters

__builder RenderTreeBuilder

CommitColumnWidths(Dictionary<string, int>)

Applies the widths the resize script measured once a person finishes resizing, by dragging a border or from the keyboard.

[JSInvokable]
public Task CommitColumnWidths(Dictionary<string, int> widths)

Parameters

widths Dictionary<string, int>

The width in pixels of each column, keyed by the column's id.

Returns

Task

A task that completes once the widths are applied.

DisposeAsync()

Detaches the resize script and releases the reference it held to the table.

public ValueTask DisposeAsync()

Returns

ValueTask

A task that completes once the table is cleaned up.

OnAfterRenderAsync(bool)

Hands the scroll wrapper to the script that keeps it a Tab stop only while the table overflows it.

protected override Task OnAfterRenderAsync(bool firstRender)

Parameters

firstRender bool

Returns

Task

OnParametersSet()

Invoked when the component's parameters have been set. Ensures that the component has a unique identifier by generating one if none is provided.

protected override void OnParametersSet()

Remarks

If the component's identifier is not set, a unique ID is generated based on the component's type name (with a 'Tw' prefix removed if present) and a GUID. This helps ensure that each component instance can be uniquely identified, which is useful for scenarios such as associating labels with form elements or targeting elements in client-side scripts.