Search documentation v1.0.0

Expandable Table

Expand collapsed tables with a simple button

Source Report Issue

Use the ExpandableTable component to manage large sets of tabular data in a small space with a collapsed state. The ExpandableTable component can be toggled on and off using a button at the end of the table:

Preview
#1John Doe
#2Jane Smith
#3Bob Johnson
#4Emily Davis
#5William Brown
#6Frank Miller
How to use the ExpandableTable components in Astro
---
const data = [
['#1', 'John Doe'],
['#2', 'Jane Smith'],
['#3', 'Bob Johnson'],
['#4', 'Emily Davis'],
['#5', 'William Brown'],
['#6', 'Frank Miller']
]
---
<ExpandableTable
data={data}
/>

The component builds on top of the Table component, meaning you can use all available Table component props with this component. The only prop it expects is a data prop that should describe the columns as an array of strings. You are free to use HTML tags within the cells to format them.

The following example shows a striped table with headings and hover effect using the props inherited from the Table component:

IDName
#1John Doe
#2Jane Smith
#3Bob Johnson
#4Emily Davis
#5William Brown
#6Frank Miller
How to customize the style of the component
---
const data = [ ... ]
const headings = ['ID', 'Name']
---
<ExpandableTable
data={data}
headings={headings}
striped="row"
offsetStripe={true}
hover={true}
/>

Set Visible Rows

By default, the number of visible rows is set to 5. This can be customized using the numberOfVisibleRows prop. If there are less entries than the number of visible rows, the button will not be displayed.

numberOfVisibleRows set to 3
#1John Doe
#2Jane Smith
#3Bob Johnson
#4Emily Davis
#5William Brown
#6Frank Miller
numberOfVisibleRows set to 6
#1John Doe
#2Jane Smith
#3Bob Johnson
#4Emily Davis
#5William Brown
#6Frank Miller
How to customize number of visible rows
<ExpandableTable
data={data}
numberOfVisibleRows={3}
/>

Customizing Buttons

You can also customize buttons through the expandButton prop. This prop expects an object in the form of a Button component props:

Custom button theme
#1John Doe
#2Jane Smith
#3Bob Johnson
#4Emily Davis
#5William Brown
#6Frank Miller
How to customize the button
<ExpandableTable
data={data}
expandButton={{ theme: 'success' }}
/>

API

type ExpandableTableProps = {
numberOfVisibleRows?: number
expandButtonLabel?: string
collapseButtonLabel?: string
expandButton?: ButtonProps
} & Omit<TableProps, 'footer'>
Please see the documentation of the Table component and the documentation of the Button for the TableProps and ButtonProps types.
PropDefaultPurpose
numberOfVisibleRows 5 Sets the number of visible rows for the collapsed state.
expandButtonLabel Expand Sets the label for the expand button.
collapseButtonLabel Collapse Sets the label for the collapse button.
expandButton null Sets the properties of the expand and collapse buttons.