Master Detail - Collapsing and Expanding Rows

View as Markdown
You can control the collapsed/expanded state of rows in the master-detail configuration.
By default, all row details are collapsed. You can very easily change this by using the defaultRowDetailState prop.
Specyfing the default row detail state
const defaultRowDetailState: RowDetailStateObject = {
  collapsedRows: true,
  expandedRows: [39, 54],
};

// <infinite-table
//   [columns]="columns"
//   [defaultRowDetailState]="defaultRowDetailState"
//   [rowDetailRenderer]="renderDetail"
// />
#
Master detail DataGrid with some row details expanded by default
Some of the rows in the master DataGrid are expanded by default.
Also, we have a default sort defined, by the country and city columns.
open
loading demo…

Understanding and defining the collapse/expand state for row details#

When you want to specify a different collapse/expand state of the row details (since by default they are all collapsed, and you might want to expand some of them), you need to use the defaultRowDetailState prop, or its controlled counterpart - the [(rowDetailState)]model prop.
The row detail state can be defined in two ways:
  • either specify collapsedRows: true (which means all rows are collapsed by default) and specify an array of expandedRows, which will contain the ids of the rows that should be rendered as expanded.
const defaultRowDetailState = {
  collapsedRows: true,
  expandedRows: ['id-1', 'id-2', 'id-56'],
};
  • or specify expandedRows: true (which means all rows are expanded by default) and specify an array of collapsedRows, which will contain the ids of the rows that should be rendered as collapsed.
const rowDetailState = {
  expandedRows: true,
  collapsedRows: ['id-1', 'id-2', 'id-56'],
};
You can pass these objects into either the defaultRowDetailState (uncontrolled) or the [(rowDetailState)]model (controlled).
[(rowDetailState)]model is a model: the grid writes the state back when the user expands or collapses a row. Bind it two-way to a signal, or bind the input and update your signal from the (rowDetailStateChange) output, as the examples below do.
As an alternative to using the object literals as specified above, you can import the RowDetailState class from @infinite-table/infinite-angular and use it to define the state of the row details. You can pass instances of RowDetailState into the defaultRowDetailState or [(rowDetailState)]model props.
Passing an instance of RowDetailState to the InfiniteTable
import { RowDetailState } from '@infinite-table/infinite-angular';

const rowDetailState = new RowDetailState({
  collapsedRows: true,
  expandedRows: [2, 3, 4],
});

// <infinite-table [columns]="columns" [rowDetailState]="rowDetailState" />
Passing an object literal to the InfiniteTable
<infinite-table
  [columns]="columns"
  [rowDetailState]="{
    collapsedRows: true,
    expandedRows: [2, 3, 4],
  }"
/>
See our type definitions for more details on row detail state.
#
Master detail DataGrid with listener for row expand/collapse
Some of the rows in the master DataGrid are expanded by default.
We use the controlled [(rowDetailState)]model prop to manage the state of the row details and update it by using (rowDetailStateChange).
open
loading demo…

Listening to row detail state changes#

In order to be notified when the collapse/expand state of row details changes, you can use the (rowDetailStateChange) prop.
The output emits the new [(rowDetailState)]model. Please note this is an instance of RowDetailState. If you want to use the object literal, make sure you call getState() on the instance of RowDetailState.
Using the onRowDetailStateChange listener
@Component({
  selector: 'master-detail',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source primaryKey="id" [data]="data">
      <infinite-table
        [rowDetailState]="rowDetailState()"
        (rowDetailStateChange)="onRowDetailStateChange($event)"
        [columnMinWidth]="50"
        [columns]="masterColumns"
        [rowDetailHeight]="200"
        [rowDetailRenderer]="renderDetail"
      />
    </infinite-data-source>
  `,
})
export class MasterDetail {
  protected readonly data = data;
  protected readonly masterColumns = masterColumns;
  protected readonly renderDetail = renderDetail;

  protected readonly rowDetailState = signal<RowDetailStateObject>({
    collapsedRows: true as const,
    expandedRows: [39, 54],
  });

  protected onRowDetailStateChange(
    rowDetailStateInstance: RowDetailState | RowDetailStateObject | undefined,
  ) {
    if (rowDetailStateInstance instanceof RowDetailState) {
      this.rowDetailState.set(rowDetailStateInstance.getState());
    }
  }
}
When using the controlled [(rowDetailState)]model, the grid applies the user interaction itself and writes the new state back to the model. Use the (rowDetailStateChange) output to keep your own signal in sync - or to replace the new state with your own, as the single row expand example below does.
This allows you to manage the state of the row details yourself - making it easy to expand/collapse all rows, or to expand/collapse a specific row by simply updating the value of the [(rowDetailState)]model prop.
@Component({
  selector: 'expand-collapse-all',
  imports: [InfiniteTable],
  template: `
    <button (click)="expandAll()">Expand All</button>
    <button (click)="collapseAll()">Collapse All</button>
    <infinite-table [columns]="columns" [rowDetailState]="rowDetailState()" />
  `,
})
export class ExpandCollapseAll {
  protected readonly columns = columns;

  protected readonly rowDetailState = signal<RowDetailStateObject>({
    collapsedRows: true,
    expandedRows: [39, 54],
  });

  protected expandAll() {
    this.rowDetailState.set({
      collapsedRows: [],
      expandedRows: true,
    });
  }

  protected collapseAll() {
    this.rowDetailState.set({
      collapsedRows: true,
      expandedRows: [],
    });
  }
}
If you prefer the more imperative approach, you can still use the Row Detail API to expand or collapse details for rows.

Single row expand#

Using the controlled [(rowDetailState)]model prop is very powerful - it allows you to configure the expand state to only allow one row to be expanded at a time, if that's something you need.
This means that if any other row(s) are expanded and you expand a new row, the previously expanded rows will all be collapsed.
#
Master detail only one row expanded at a time
In this demo we allow only one row to be expanded at any given time.
We use the controlled [(rowDetailState)]model prop to manage the state of the row details and update it by using (rowDetailStateChange).
open
loading demo…