DocsGuides

Guides

Expanding Sub-Rows/Tree Data Guide

How to add expanding sub-rows or tree data features in Shadcn Table

Shadcn Table has support for expanding sub-rows or tree data. This feature is useful for displaying hierarchical data. The sub-rows can be expanded and collapsed by clicking on the expand/collapse icon.

Example

Storybook

NOTE: This feature is for expanding rows of the same data type. If you want to add expansion of more data for the same row, check out the Detail Panel Feature Guide.

Relevant Table Options

Prop NameTypeDescription
autoResetExpandedbooleanEnable this setting to automatically reset the expanded state of the table when grouping state changes.
data*Array<TData>The data for the table to display. This can be an array of anything, but you will need to set up your column definitions to "access" the data. When the data option changes reference (compared via Object.is), the table will reprocess the data. Any other data processing that relies on the core data model (such as grouping, sorting, filtering, etc) will also be reprocessed.
enableExpandAllboolean
enableExpandingboolean
filterFromLeafRowsboolean
getIsRowExpanded(row: Row<TData>) => booleanIf provided, allows you to override the default behavior of determining whether a row is currently expanded.
getRowCanExpand(row: Row<TData>) => booleanIf provided, allows you to override the default behavior of determining whether a row can be expanded.
getRowId(originalRow: TData, index: number, parent?: ST_Row<TData>) => stringThis optional function is used to derive a unique ID for any given row. If not provided the rows index is used (nested rows join together with . using their grandparents' index eg. index.index.index). If you need to identify individual rows that are originating from any server-side operations, it's suggested you use this function to return an ID that makes sense regardless of network IO/ambiguity eg. a userId, taskId, database ID field, etc.
getSubRows(originalRow: TData, index: number) => undefined | TData[]This optional function is used to access the sub rows for any given row. If you are using nested rows, you will need to use this function to return the sub rows object (or undefined) from the row.
groupedColumnModefalse | 'reorder' | 'remove'Grouping columns are automatically reordered by default to the start of the columns list. If you would rather remove them or leave them as-is, set the appropriate mode here.
manualExpandingbooleanEnables manual row expansion. If this is set to true, getExpandedRowModel will not be used to expand rows and you would be expected to perform the expansion in your own data model. This is useful if you are doing server-side expansion.
maxLeafRowFilterDepthnumberSet a maximum number leaf row depth that will be filtered. Preserve child rows of filtered rows by setting this to "0"
onExpandedChangeOnChangeFn<ExpandedState>This function is called when the expanded table state changes. If a function is provided, you will be responsible for managing this state on your own. To pass the managed state back to the table, use the tableOptions.state.expanded option.
paginateExpandedRowsbooleanIf true expanded rows will be paginated along with the rest of the table (which means expanded rows may span multiple pages). If false expanded rows will not be considered for pagination (which means expanded rows will always render on their parents page. This also means more rows will be rendered than the set page size)
positionExpandColumn'first' | 'last'
stExpandAllButtonPropsIconButtonProps | ({ table }) => IconButtonProps
stExpandButtonPropsIconButtonProps | ({ row, table }) => IconButtonProps

Relevant State Options

State OptionTypeDescription
expandedRecord<string, boolean> | boolean

Enable Expanding Sub-Rows

To enable expanding sub-rows, you must first set the enableExpanding table option to true.

However, your data must also be formatted in a way to allow for expanding rows that are in some way related to each other. By default, Shadcn Table will look for a special subRows property on each row of your data and treat any array of rows that it finds as the sub-rows for that row. You can customize or override this behavior by passing a custom getSubRows table option.

const data = [
  {
    id: 1,
    name: 'John Doe',
    subRows: [
      {
        id: 2,
        name: 'Jane Doe',
      },
      //more sub rows...
    ],
  },
  //more rows...
];

const table = useShadcnTable({
  columns,
  data,
  enableExpanding: true,
  getSubRows: (originalRow) => originalRow.subRows, //default, can customize
});

return <ShadcnTable table={table} />;

Expand All Rows Button

By default, Shadcn Table will show the expand all button in the expand column header. You can disable this by setting the enableExpandAll table option to false.

const table = useShadcnTable({
  columns,
  data,
  enableExpanding: true,
  enableExpandAll: false, //hide expand all button in header
});
Storybook

Generate Sub Rows with getSubRows

If your data is not yet in a tree structure, but the data has relationships that can be parsed into a tree, you can use the getSubRows table option to let TanStack Table find the sub rows for each row.

There are a couple key things you have to do to make this work:

  1. Only pass in root (top level) rows in your data prop.
  2. Set the getSubRows table option to a function that scans all the rest of your data and returns the sub rows for a given row.

This can sometimes be useful in combination with lazy loading sub rows.

NOTE: Be conscious of the performance implications of the getSubRows function. It will be called for every row in your table, so it should be performant.

Storybook

Expanded Rows Pagination Behavior

By default, Shadcn Table will treat expanded sub-rows the same as any other row when it comes to pagination. This means that some expanded rows may be on the next page. You can change this behavior by setting the paginateExpandedRows table option to false.

const table = useShadcnTable({
  columns,
  data,
  enableExpanding: true,
  paginateExpandedRows: false, //expanded rows will be on the same page as their parent row
});

Expanded Leaf Row Filtering Behavior

If you are using the filtering features alongside sub-row features, then there are a few behaviors and customizations you should be aware of.

Filter From Leaf Rows

By default, filtering is done from parent rows down (so if a parent row is filtered out, all of its children will be filtered out as well). Setting the filterFromLeafRows table option to true will cause filtering to be done from leaf rows up (which means parent rows will be kept so long as one of their child, or grand-child, etc. rows pass the filtering).

const table = useShadcnTable({
  columns,
  data,
  enableExpanding: true,
  filterFromLeafRows: true, //search for child rows and preserve parent rows
});

Max Leaf Row Filter Depth

By default, filtering is done for all rows (max depth of 100), no matter if they are root level parent rows or the child leaf rows of a parent row. Setting the maxLeafRowFilterDepth table option to 0 will cause filtering to only be applied to the root level parent rows, with all sub-rows remaining unfiltered. Similarly, setting this option to 1 will cause filtering to only be applied to child leaf rows 1 level deep, and so on.

This is useful for situations where you want a row's entire child hierarchy to be visible, regardless of the applied filter.

const table = useShadcnTable({
  columns,
  data,
  enableExpanding: true,
  maxLeafRowFilterDepth: 0, //When filtering root rows, keep all child rows of the passing parent rows
});

Expand All Rows By Default

You can manage the initial state of the expanded rows with the expanded state option in either the initialState or state props.

For example, you may want all rows to be expanded by default. To do this, you can simply set the expanded state option to true.

const table = useShadcnTable({
  columns,
  data,
  enableExpanding: true,
  initialState: { expanded: true }, //all rows expanded by default
});
Storybook

Expand Root Rows Only By Default

Here is a slightly more complex initial expanded state example where all the root rows are expanded by default, but none of the sub rows themselves are expanded by default. We just need to find all of the root row ids and set their key in the expanded initialState option to true.

Storybook

Customize Expand Column

You can customize the expand column by using the displayColumnDefOptions table option.

const table = useShadcnTable({
  columns,
  data,
  enableGrouping: true,
  displayColumnDefOptions: {
    'st-row-expand': {
      enableResizing: true, //allow resizing
      size: 120, //make the expand column wider
    },
  },
});

Lazy Load Sub Rows

If you have a ton of nested data that you want to display, but you don't want to fetch it all up front, you can set up Shadcn Table to only fetch the sub-rows data when the user expands the row.

There are quite a few ways in which you could implement fetching sub-rows lazily. This example is just one way to do it.

The main concept to understand from this example is that you can manage the expanded state option in your own scope, and fetch the data for your table based on that state.

How your data is structured from the server is up to you. It is usually easiest to have the server do the hard work and return the data in a nested tree structure, but you can also return the data in a flat structure and use the getSubRows table option to parse the data into a tree structure.

Storybook

On this page