docs(data-table): improve docs

This commit is contained in:
Eric Liu 2025-04-20 15:30:40 -07:00
commit 90b067ded3
2 changed files with 108 additions and 102 deletions

View file

@ -8,21 +8,20 @@ components: ["DataTable", "Pagination","Toolbar", "ToolbarContent", "ToolbarSear
import Preview from "../../components/Preview.svelte"; import Preview from "../../components/Preview.svelte";
</script> </script>
`DataTable` is keyed for performance reasons. `DataTable` displays structured data in a tabular format. Use it to present large datasets with features like sorting, filtering, pagination, and row selection.
<InlineNotification svx-ignore lowContrast title="Note:" kind="info" hideCloseButton> <InlineNotification svx-ignore lowContrast title="Note:" kind="info" hideCloseButton>
<div class="body-short-01">Every <code>headers</code> object must have a unique "key" property.</div> <div class="body-short-01">
</InlineNotification> This component is keyed for performance.
<InlineNotification svx-ignore lowContrast title="Note:" kind="info" hideCloseButton> Every <strong>headers</strong> object must have a unique "key" property.
<div class="body-short-01">Every <code>rows</code> object must have a unique "id" property.</div> <div class="body-short-01">Every <strong>rows</strong> object must have a unique "id" property.</div>
</div>
</InlineNotification> </InlineNotification>
## Default ## Default
The key value in `headers` corresponds to the key value defined in `rows`. Create a basic table by providing `headers` and `rows` data. Match the `key` in headers with the corresponding property in rows.
For example, the header key `"name"` will use the value of `rows[index].name`.
<DataTable <DataTable
headers="{[ headers="{[
@ -79,9 +78,7 @@ For example, the header key `"name"` will use the value of `rows[index].name`.
## Slotted cells ## Slotted cells
Use the `"cell"` slot to control the display value per table cell. Individual row and cell data are provided through the `let:row` and `let:cell` directives. Use the `"cell"` slot to customize cell content. Access row and cell data through `let:row` and `let:cell` directives. Use `"cell-header"` slot for header cells.
The slot name for the table header cells is `"cell-header"`.
<DataTable <DataTable
headers="{[ headers="{[
@ -153,6 +150,8 @@ The slot name for the table header cells is `"cell-header"`.
## With title, description ## With title, description
Add a title and description to provide context for the table data.
<DataTable title="Load balancers" description="Your organization's active load balancers." <DataTable title="Load balancers" description="Your organization's active load balancers."
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -208,7 +207,7 @@ The slot name for the table header cells is `"cell-header"`.
## Slottable title, description ## Slottable title, description
The `title` and `description` props are slottable. Use slots to customize the title and description content.
<DataTable <DataTable
headers="{[ headers="{[
@ -270,7 +269,7 @@ The `title` and `description` props are slottable.
## Static width ## Static width
Set `useStaticWidth` to `true` to render the table with a width of "auto" instead of "100%". Set `useStaticWidth` to `true` to render the table with an auto width instead of 100%.
<DataTable useStaticWidth <DataTable useStaticWidth
title="Load balancers" description="Your organization's active load balancers." title="Load balancers" description="Your organization's active load balancers."
@ -328,9 +327,7 @@ title="Load balancers" description="Your organization's active load balancers."
## Custom column widths ## Custom column widths
Specify a `width` or `minWidth` property in the `headers` object to customize the width of each column. Specify `width` or `minWidth` in the `headers` object to set column dimensions. This applies a fixed table layout.
A [table-layout: fixed](https://developer.mozilla.org/en-US/docs/Web/CSS/table-layout#values) rule will be applied to the `table` element when using custom widths.
<InlineNotification svx-ignore lowContrast kind="warning" title="Note:" hideCloseButton> <InlineNotification svx-ignore lowContrast kind="warning" title="Note:" hideCloseButton>
<div class="body-short-01">Custom column widths do not work with a <a class="bx--link" href="#sticky-header">sticky header</a>.</div> <div class="body-short-01">Custom column widths do not work with a <a class="bx--link" href="#sticky-header">sticky header</a>.</div>
@ -340,9 +337,7 @@ A [table-layout: fixed](https://developer.mozilla.org/en-US/docs/Web/CSS/table-l
## Sticky header ## Sticky header
Set `stickyHeader` to `true` for the header to be fixed in place. Set `stickyHeader` to `true` to fix the header in place. This adds a maximum height to force scrolling.
A maximum height will applied to the datatable to force a scrollbar.
<DataTable <DataTable
stickyHeader stickyHeader
@ -363,6 +358,8 @@ A maximum height will applied to the datatable to force a scrollbar.
## With toolbar ## With toolbar
Add a toolbar with search, menu, and actions above the table.
<DataTable title="Load balancers" description="Your organization's active load balancers." <DataTable title="Load balancers" description="Your organization's active load balancers."
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -432,6 +429,8 @@ A maximum height will applied to the datatable to force a scrollbar.
## With toolbar (small size) ## With toolbar (small size)
Use `size="short"` to create a more compact table with a small toolbar.
<DataTable size="short" title="Load balancers" description="Your organization's active load balancers." <DataTable size="short" title="Load balancers" description="Your organization's active load balancers."
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -499,24 +498,22 @@ A maximum height will applied to the datatable to force a scrollbar.
## Filterable ## Filterable
By default, `ToolbarSearch` will not filter `DataTable` rows. Set `shouldFilterRows` to `true` to enable client-side filtering. The default filter performs string comparisons on cell values.
Set `shouldFilterRows` to `true` to enable client-side filtering. The default filtering performs a basic string comparison on cell values that are of a string or a number type. For pagination with filtering, bind to `filteredRowIds` and pass its length to `Pagination`'s `totalItems`.
To use filtering with pagination, bind to the `filteredRowIds` prop and pass its length to `totalItems` in `Pagination`.
Note that in-memory filtering is not optimal for large data sets, where you might consider using server-side search.
<FileSource src="/framed/DataTable/DataTableFilterable" /> <FileSource src="/framed/DataTable/DataTableFilterable" />
## Filterable (custom) ## Filterable (custom)
`shouldFilterRows` also accepts a function and passes it the current row and search value. It expects the function to return a boolean. Pass a function to `shouldFilterRows` to implement custom filtering logic.
<FileSource src="/framed/DataTable/DataTableFilterCustom" /> <FileSource src="/framed/DataTable/DataTableFilterCustom" />
## Zebra stripes ## Zebra stripes
Set `zebra` to `true` to add alternating row colors.
<DataTable zebra <DataTable zebra
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -572,6 +569,8 @@ Note that in-memory filtering is not optimal for large data sets, where you migh
## Tall rows ## Tall rows
Set `size="tall"` for increased row height.
<DataTable size="tall" <DataTable size="tall"
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -627,6 +626,8 @@ Note that in-memory filtering is not optimal for large data sets, where you migh
## Medium rows ## Medium rows
Set `size="medium"` for standard row height.
<DataTable size="medium" <DataTable size="medium"
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -682,6 +683,8 @@ Note that in-memory filtering is not optimal for large data sets, where you migh
## Short rows ## Short rows
Set `size="short"` for compact row height.
<DataTable size="short" <DataTable size="short"
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -737,6 +740,8 @@ Note that in-memory filtering is not optimal for large data sets, where you migh
## Compact rows ## Compact rows
Set `size="compact"` for minimal row height.
<DataTable size="compact" <DataTable size="compact"
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -792,11 +797,7 @@ Note that in-memory filtering is not optimal for large data sets, where you migh
## Sortable ## Sortable
Set `sortable` to `true` to enable table column sorting. Set `sortable` to `true` to enable column sorting. Disable sorting on specific columns by setting `sort: false` in the header object.
To disable sorting on a specific column, set `sort` to `false` in the header object passed to the `headers` prop.
In the example below, the "Protocol" column is not sortable.
<DataTable sortable <DataTable sortable
headers="{[ headers="{[
@ -853,12 +854,14 @@ In the example below, the "Protocol" column is not sortable.
## Sortable with pagination ## Sortable with pagination
For pagination, bind to the `pageSize` and `page` props of `Pagination` and pass their values to `DataTable`. Bind to `pageSize` and `page` props of `Pagination` and pass them to `DataTable`.
<FileSource src="/framed/DataTable/DataTablePagination" /> <FileSource src="/framed/DataTable/DataTablePagination" />
## Sortable with custom display and sort methods ## Sortable with custom display and sort methods
Use `display` and `sort` functions in header objects to customize cell rendering and sorting.
<DataTable sortable title="Load balancers" description="Your organization's active load balancers." <DataTable sortable title="Load balancers" description="Your organization's active load balancers."
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -926,6 +929,8 @@ For pagination, bind to the `pageSize` and `page` props of `Pagination` and pass
## Sortable with nested object values ## Sortable with nested object values
Access nested object properties using dot notation in the header key.
<DataTable sortable title="Load balancers" description="Your organization's active load balancers." <DataTable sortable title="Load balancers" description="Your organization's active load balancers."
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -1005,83 +1010,61 @@ For pagination, bind to the `pageSize` and `page` props of `Pagination` and pass
## Programmatic sorting ## Programmatic sorting
Use the reactive `sortKey` and `sortDirection` props for programmatic sorting. Use `sortKey` and `sortDirection` props to control sorting programmatically. Set `sortKey` to a valid header key and `sortDirection` to "none", "ascending", or "descending".
By default, the table is not sorted by a specific key. The `sortKey` value must be a valid `key` specified in the `headers` object.
Possible values for `sortDirection` include `"none"` or `"ascending"` or `"descending"`.
Setting `sortKey` to `null` and `sortDirection` to `"none"` should reset the table rows to their initial order.
<FileSource src="/framed/DataTable/DataTableProgrammaticSorting" /> <FileSource src="/framed/DataTable/DataTableProgrammaticSorting" />
## Empty column with overflow menu ## Empty column with overflow menu
Some use cases require an empty column in the table body without a corresponding table header. Set `empty: true` in a header object to create an empty column. Use this for overflow menus without a header.
For an object in the `headers` array, set `empty` to `true` to render an empty column.
In the following example, each row in the sortable data table has an overflow menu. There isn't a separate, useless table header column for the overflow menu.
<FileSource src="/framed/DataTable/DataTableAppendColumns" /> <FileSource src="/framed/DataTable/DataTableAppendColumns" />
## Selectable rows (checkbox) ## Selectable rows (checkbox)
Set `selectable` to `true` for rows to be multi-selectable. Set `selectable` to `true` for multi-select functionality. Bind to `selectedRowIds` to track selections. Use `inputName` to customize checkbox names.
Bind to `selectedRowIds` to get the ids of the selected rows.
To customize the `input` name for the checkbox, use the `inputName` prop.
<FileSource src="/framed/DataTable/SelectableDataTable" /> <FileSource src="/framed/DataTable/SelectableDataTable" />
## Batch selection ## Batch selection
To enable batch selection, set `batchSelection` to `true`. Set `batchSelection` to `true` to add a checkbox for selecting all rows. The checkbox shows an indeterminate state when some rows are selected.
This checkbox is used to select all rows. It enters an indeterminate state when some rows are selected.
<FileSource src="/framed/DataTable/DataTableBatchSelection" /> <FileSource src="/framed/DataTable/DataTableBatchSelection" />
## Batch selection with initial selected rows ## Batch selection with initial selected rows
Use the `selectedRowIds` prop to specify which rows should be selected. Use `selectedRowIds` to specify initially selected rows.
<FileSource src="/framed/DataTable/DataTableBatchSelectionInitial" /> <FileSource src="/framed/DataTable/DataTableBatchSelectionInitial" />
## Batch selection with batch actions toolbar ## Batch selection with batch actions toolbar
Add a toolbar for batch actions when rows are selected.
<FileSource src="/framed/DataTable/DataTableBatchSelectionToolbar" /> <FileSource src="/framed/DataTable/DataTableBatchSelectionToolbar" />
## Batch selection with controlled toolbar ## Batch selection with controlled toolbar
By default, `ToolbarBatchActions` is activated if one or more rows is selected. Clicking "Cancel" will deactivate it. Control the batch actions toolbar with the `active` prop. Prevent default cancel behavior in the `on:cancel` event.
Use the `active` prop to control the toolbar. Note that it will still activate if one or more rows are selected.
You can prevent the default "Cancel" behavior in the dispatched `on:cancel` event.
<FileSource src="/framed/DataTable/DataTableBatchSelectionToolbarControlled" /> <FileSource src="/framed/DataTable/DataTableBatchSelectionToolbarControlled" />
## Selectable rows (radio) ## Selectable rows (radio)
Set `radio` to `true` for only one row to be selected at a time. Set `radio` to `true` for single-row selection. Bind to `selectedRowIds` to track the selected row. Use `inputName` to customize radio button names.
Bind to `selectedRowIds` to get the ids of the selected rows. Because it's radio selection, `selectedRowIds` will only contain one id.
To customize the `input` name for the radio button, use the `inputName` prop.
<FileSource src="/framed/DataTable/RadioSelectableDataTable" /> <FileSource src="/framed/DataTable/RadioSelectableDataTable" />
## Non-selectable rows ## Non-selectable rows
Use `nonSelectableRowIds` to specify the ids for rows that should not be selectable. Use `nonSelectableRowIds` to prevent selection of specific rows.
<FileSource src="/framed/DataTable/DataTableNonSelectableRows" /> <FileSource src="/framed/DataTable/DataTableNonSelectableRows" />
## Expandable rows ## Expandable rows
Set `expandable` to `true` for rows to be expandable. Set `expandable` to `true` to make rows expandable. Use the `expanded-row` slot to customize expanded content.
<DataTable expandable <DataTable expandable
headers="{[ headers="{[
@ -1142,16 +1125,20 @@ Set `expandable` to `true` for rows to be expandable.
## Non-expandable rows ## Non-expandable rows
Use `nonExpandableRowIds` to specify the ids for rows that should not be expandable. Use `nonExpandableRowIds` to prevent expansion of specific rows.
<FileSource src="/framed/DataTable/DataTableNonExpandableRows" /> <FileSource src="/framed/DataTable/DataTableNonExpandableRows" />
## Expandable (zebra styles) ## Expandable (zebra styles)
Combine expandable rows with zebra striping.
<FileSource src="/framed/DataTable/DataTableExpandableZebra" /> <FileSource src="/framed/DataTable/DataTableExpandableZebra" />
## Expandable (compact size) ## Expandable (compact size)
Set `size="compact"` for expandable rows with minimal height.
<DataTable size="compact" expandable <DataTable size="compact" expandable
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -1211,6 +1198,8 @@ Use `nonExpandableRowIds` to specify the ids for rows that should not be expanda
## Expandable (short size) ## Expandable (short size)
Set `size="short"` for expandable rows with compact height.
<DataTable size="short" expandable <DataTable size="short" expandable
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -1270,6 +1259,8 @@ Use `nonExpandableRowIds` to specify the ids for rows that should not be expanda
## Expandable (tall size) ## Expandable (tall size)
Set `size="tall"` for expandable rows with increased height.
<DataTable size="tall" expandable <DataTable size="tall" expandable
headers="{[ headers="{[
{ key: "name", value: "Name" }, { key: "name", value: "Name" },
@ -1329,7 +1320,7 @@ Use `nonExpandableRowIds` to specify the ids for rows that should not be expanda
## Batch expansion ## Batch expansion
Set `batchExpansion` to `true` for the ability to expand and collapse all rows at once. Set `batchExpansion` to `true` to expand and collapse all rows at once.
<DataTable batchExpansion <DataTable batchExpansion
headers="{[ headers="{[
@ -1390,42 +1381,54 @@ Set `batchExpansion` to `true` for the ability to expand and collapse all rows a
## Expandable and selectable rows ## Expandable and selectable rows
The `batchExpansion` and `batchSelection` props can be used together. Combine `batchExpansion` and `batchSelection` for tables with both expandable and selectable rows.
<FileSource src="/framed/DataTable/DataTableExpandableSelectable" /> <FileSource src="/framed/DataTable/DataTableExpandableSelectable" />
## Skeleton ## Skeleton
Use `DataTableSkeleton` to show a loading state.
<DataTableSkeleton /> <DataTableSkeleton />
## Skeleton with headers, row count ## Skeleton with headers, row count
Specify headers and row count for the skeleton.
<DataTableSkeleton headers={["Name", "Protocol", "Port", "Rule"]} rows={10} /> <DataTableSkeleton headers={["Name", "Protocol", "Port", "Rule"]} rows={10} />
## Skeleton with object headers ## Skeleton with object headers
`headers` can also be an array of objects. The type is the same as the `headers` prop type used in `DataTable`. Pass header objects to customize the skeleton.
<DataTableSkeleton headers={[{ value: "Name" }, {value: "Protocol"}, {value:"Port"}, { value: "Rule"}]} rows={10} /> <DataTableSkeleton headers={[{ value: "Name" }, {value: "Protocol"}, {value:"Port"}, { value: "Rule"}]} rows={10} />
## Skeleton with empty header ## Skeleton with empty header
Pass an object with `"empty"` set to `true` to render an empty table header column. Add an empty header column with `empty: true`.
<DataTableSkeleton headers={[{ value: "Name" }, {value: "Protocol"}, {value:"Port"}, { value: "Rule"}, { empty: true }]} rows={10} /> <DataTableSkeleton headers={[{ value: "Name" }, {value: "Protocol"}, {value:"Port"}, { value: "Rule"}, { empty: true }]} rows={10} />
## Skeleton without header, toolbar ## Skeleton without header, toolbar
Hide the header and toolbar in the skeleton.
<DataTableSkeleton showHeader={false} showToolbar={false} /> <DataTableSkeleton showHeader={false} showToolbar={false} />
## Skeleton (tall size) ## Skeleton (tall size)
Set `size="tall"` for a taller skeleton.
<DataTableSkeleton showHeader={false} showToolbar={false} size="tall" /> <DataTableSkeleton showHeader={false} showToolbar={false} size="tall" />
## Skeleton (short size) ## Skeleton (short size)
Set `size="short"` for a shorter skeleton.
<DataTableSkeleton showHeader={false} showToolbar={false} size="short" /> <DataTableSkeleton showHeader={false} showToolbar={false} size="short" />
## Skeleton (compact size) ## Skeleton (compact size)
Set `size="compact"` for a minimal skeleton.
<DataTableSkeleton showHeader={false} showToolbar={false} size="compact" /> <DataTableSkeleton showHeader={false} showToolbar={false} size="compact" />

View file

@ -8,37 +8,40 @@
$: console.log("sortDirection", sortDirection); $: console.log("sortDirection", sortDirection);
</script> </script>
<Button <div style="margin-bottom: 1rem">
kind="tertiary" <Button
disabled={sortKey === "port" && sortDirection === "ascending"} size="sm"
on:click={() => { kind="tertiary"
sortKey = "port"; disabled={sortKey === "port" && sortDirection === "ascending"}
sortDirection = "ascending"; on:click={() => {
}} sortKey = "port";
> sortDirection = "ascending";
Sort "port" in ascending order }}
</Button> >
Sort "port" in ascending order
<Button </Button>
kind="tertiary" <Button
disabled={sortKey === "name" && sortDirection === "descending"} size="sm"
on:click={() => { kind="tertiary"
sortKey = "name"; disabled={sortKey === "name" && sortDirection === "descending"}
sortDirection = "descending"; on:click={() => {
}} sortKey = "name";
> sortDirection = "descending";
Sort "name" in descending order }}
</Button> >
Sort "name" in descending order
<Button </Button>
kind="ghost" <Button
on:click={() => { size="sm"
sortKey = null; kind="ghost"
sortDirection = "none"; on:click={() => {
}} sortKey = null;
> sortDirection = "none";
Clear sorting }}
</Button> >
Clear sorting
</Button>
</div>
<DataTable <DataTable
sortable sortable