1. Data Grid docs
  2. Rows and records

Master/detail

Expand a record into a panel of your own under its row, for order lines, a history or a chart, built from the record's values.

Master/detail shows more about one record without leaving the list: an order's lines under the order, a customer's recent tickets under the customer. Each record gets an arrow; expanding it opens a panel under its row that you build from the record, and it scrolls with the records. One panel is open at a time, or several with detailMode: 'multiple'.

Open the detail example: orders with their lines

Use it

detailRenderer(record) returns a DOM node or a string for the panel. record is { id, values, raw }:

  • id: the record's ID;
  • values: display text of the columns on screen, by field name;
  • raw: the values behind them, numbers for number and date columns (every value in an editable local copy, otherwise null for text).

JavaScript

import { mountTable } from '@8sheets/data-grid';

const grid = await mountTable(document.querySelector('#orders'), {
  title: 'Orders',
  data: orders,
  detailRenderer: (record) => {
    const panel = document.createElement('section');
    panel.innerHTML = '<h3></h3><table><thead><tr><th>Product</th><th>Qty</th><th>Amount</th></tr></thead><tbody></tbody></table>';
    panel.querySelector('h3').textContent = `Order ${record.values.Order} · ${record.values.Customer}`;
    // Data you already have, or fetch it: the panel can fill in when the answer arrives.
    fetch(`/api/orders/${encodeURIComponent(record.id)}/lines`)
      .then((r) => r.json())
      .then((lines) => {
        for (const line of lines) {
          const row = panel.querySelector('tbody').insertRow();
          for (const v of [line.product, line.qty, line.amount.toFixed(2)]) row.insertCell().textContent = v;
        }
      });
    return panel;
  },
});

React

import { useMemo } from 'react';
import { createRoot } from 'react-dom/client';
import { DataGrid } from '@8sheets/data-grid-react';
import { OrderLines } from './OrderLines.jsx';

export function Orders({ orders }) {
  const options = useMemo(() => ({
    title: 'Orders',
    data: orders,
    // The grid wants a DOM node: render a React component into one.
    detailRenderer: (record) => {
      const node = document.createElement('div');
      createRoot(node).render(<OrderLines orderId={record.id} customer={record.values.Customer} />);
      return node;
    },
  }), [orders]);
  return <div style={{ height: 600 }}><DataGrid options={options} /></div>;
}

Vue

<script setup>
import { computed, createApp } from 'vue';
import { DataGrid } from '@8sheets/data-grid-vue';
import OrderLines from './OrderLines.vue';

const props = defineProps({ orders: Array });
const options = computed(() => ({
  title: 'Orders',
  data: props.orders,
  // The grid wants a DOM node: mount a small Vue app into one.
  detailRenderer: (record) => {
    const node = document.createElement('div');
    createApp(OrderLines, { orderId: record.id, customer: record.values.Customer }).mount(node);
    return node;
  },
}));
</script>

<template>
  <div style="height: 600px"><DataGrid :options="options" /></div>
</template>

Angular

import { ApplicationRef, Component, EnvironmentInjector, Input, createComponent, inject } from '@angular/core';
import { DataGridComponent, DataGridOptions } from '@8sheets/data-grid-angular';
import { OrderLines } from './order-lines.component';

@Component({
  selector: 'app-orders',
  imports: [DataGridComponent],
  template: `<eight-data-grid [options]="options" style="height: 600px" />`,
})
export class Orders {
  private app = inject(ApplicationRef);
  private injector = inject(EnvironmentInjector);
  options: DataGridOptions = {};

  @Input() set orders(data: Record<string, unknown>[]) {
    this.options = {
      title: 'Orders',
      data,
      // The grid wants a DOM node: create the component and hand over its element.
      detailRenderer: (record) => {
        const ref = createComponent(OrderLines, { environmentInjector: this.injector });
        ref.setInput('orderId', record.id);
        this.app.attachView(ref.hostView);
        return ref.location.nativeElement;
      },
    };
  }
}

From code

expandRecord(id) opens a record's panel and scrolls to it, closing any other unless detailMode is 'multiple'. collapseRecord(id) closes one record's panel and collapseRecord() closes every panel. getExpandedRecord() says which record's panel opened last, and getExpandedRecords() lists every open one, in the order they opened. openRecordDetails(id) opens the record dialog with every field. To show your own view on a double-click instead of the dialog, cancel rowDoubleClick:

JavaScript

const grid = await mountTable(el, {
  data: orders,
  detailRenderer: orderLines,
  onRowDoubleClick: (e) => {
    e.preventDefault();             // no record dialog
    grid.expandRecord(e.id);        // open the panel instead
  },
});
await grid.expandRecord(42);        // open order 42 on load

React

<DataGrid
  ref={ref}
  options={options}
  onRowDoubleClick={(e) => {
    e.preventDefault();
    ref.current.expandRecord(e.id);
  }}
/>

Vue

<DataGrid
  ref="dataGrid"
  :options="options"
  @row-double-click="(e) => { e.preventDefault(); dataGrid.expandRecord(e.id); }"
/>

Angular

// template: <eight-data-grid #grid [options]="options" (rowDoubleClick)="open($event)" />
@ViewChild('grid') grid!: DataGridComponent;
open(e: { id: string | number; preventDefault(): void }) {
  e.preventDefault();
  this.grid.expandRecord(e.id);
}

Several panels at once

With detailMode: 'multiple', expanding a record leaves the others open, so a user can compare two orders' lines one above the other. The option is live, and 'single' (the default) goes back to one panel at a time.

const grid = await mountTable(el, { data: orders, detailRenderer: orderLines, detailMode: 'multiple' });
await grid.expandRecord(42);
await grid.expandRecord(57);
grid.getExpandedRecords(); // [42, 57]
grid.collapseRecord(42);   // 57 stays open
grid.collapseRecord();     // closes every panel

The context menu's Expand details opens a record's panel too; see Context menu.

How it behaves

  • With the default detailMode: 'single', one record is expanded at a time; expanding another closes the first. A second click on the arrow closes it.
  • Each panel is built once per expansion and kept while it is open, so its state survives scrolling. It scrolls with the records, up to 360 px tall, and scrolls inside itself beyond that.
  • The panel is a labelled region next to its row, reachable with the keyboard.
  • Double-clicking a row, or Enter on a cell of a read-only grid, still opens the record dialog with every field, unless a rowDoubleClick or cellDoubleClick handler cancels it.

Limits

  • A panel isn't unmounted for you when it closes: components rendered into it (React roots, Vue apps, Angular views) stay alive until you clean them up, for example by keeping the last one and unmounting it when the next panel is built.
  • The panel is not part of exports.
  • With a server source, raw holds what your rows' raw arrays carry.