Skip to content

Master Rows

The Master Row Plugin enhances the RevoGrid grid component by enabling the creation and management of master-detail rows. It is an extremely complex and advanced feature, despite its seemingly simple nature. Read more below.

Source code
import { defineCustomElements } from '@revolist/revogrid/loader';
import { EXPAND_COLUMN, MasterRowPlugin, CellColumnFocusVerifyPlugin, ColumnStretchPlugin, OverlayPlugin, type RowMasterConfig } from '@revolist/revogrid-pro';
import { makeData, type Person } from '../composables/makeData';
import { currentTheme } from '../composables/useRandomData';
defineCustomElements();
export function load(parentSelector: string) {
const grid = document.createElement('revo-grid');
const { isDark } = currentTheme();
grid.theme = isDark() ? 'darkCompact' : 'compact';
grid.source = makeData(500, 20);
grid.columns = [
{ prop: 'expand', focus: () => false, ...EXPAND_COLUMN },
{ name: 'First Name', prop: 'firstName', size: 180 },
{ name: 'Last Name', prop: 'lastName' },
];
grid.plugins = [
MasterRowPlugin,
OverlayPlugin,
CellColumnFocusVerifyPlugin,
ColumnStretchPlugin,
];
const masterRow: RowMasterConfig = {
rowHeight: 60,
template: (h, data) => {
const subItems = data.model.subRows?.map((subRow: Person) =>
h('span', { class: { 'master-row': true } }, [subRow.firstName + ' ' + subRow.lastName, ', ']),
);
return h('div', null, [h('strong', {}, 'Team: '), subItems]);
},
};
grid.additionalData = {
masterRow,
stretch: 'last',
};
document.querySelector(parentSelector)?.appendChild(grid);
}

  • Expandable Rows: Easily expand rows to reveal additional details.
  • Custom Templates: Define how master rows are rendered using customizable templates.

  1. Import Necessary Plugins and Utilities:

    import { EXPAND_COLUMN, MasterRowPlugin, OverlayPlugin, CellColumnFocusVerifyPlugin } from '@revolist/revogrid-pro';
    import type { RowMasterConfig } from '@revolist/revogrid';
    import './row-master.style.css';
  2. Define Columns:

    const columns: HTMLRevoGridElement['columns'] = [
    {
    prop: 'expand',
    ...EXPAND_COLUMN,
    },
    // ...
    ];
  3. Initialize the Grid with Plugins:

    const grid = document.querySelector('revo-grid');
    grid.source = makeData(10, 20);
    grid.columns = columns;
    // Define plugin
    grid.plugins = [
    MasterRowPlugin, // required
    OverlayPlugin, // required
    ];
    const masterRow: RowMasterConfig = {
    template: (h, data) => {
    return data.model.subRows?.map((subRow: Person) =>
    h('span', { class: { 'master-row': true } }, [
    h('img', { src: subRow.avatar, width: '20', height: '20' }),
    `${subRow.firstName} ${subRow.lastName} `,
    ]),
    );
    },
    };
    grid.additionalData = {
    masterRow,
    rowHeight: 100,
    };
    }

The Master Row Plugin can be customized using the RowMasterConfig interface. The primary configuration option is the template function, which defines how the master row content is rendered.

PropertyTypeDescription
template(h: Function, data: any) => JSX.ElementA function that returns the JSX structure for the master row. It receives a rendering function h and the data context data.

Customize the master row by defining a template function that returns the desired JSX structure. This allows for flexible and dynamic rendering based on the row data.

const masterRow: RowMasterConfig = {
template: (h, data) => {
return data.model.subRows?.map((subRow: Person) =>
h('div', { class: { 'custom-master-row': true } }, [
h('img', { src: subRow.avatar, width: '30', height: '30' }),
`${subRow.firstName} ${subRow.lastName}`,
]),
);
},
};

A plugin class that manages master-detail rows within the RevoGrid component.

  • constructor(revogrid: HTMLRevoGridElement, providers: PluginProviders)

    Initializes the plugin with the RevoGrid instance and plugin providers.

  • expandRow(type: DimensionRows, virtRowIndex: number)

    Expands a specific row to show its master details.

  • collapseRow(type: DimensionRows, rowVIndexes: number[])

    Collapses one or more rows, hiding their master details.

  • destroy()

    Cleans up event listeners and other resources when the plugin is destroyed.

The plugin listens to various RevoGrid events to manage the state of master rows, including:

  • AFTER_GRID_RENDER_EVENT
  • BEFORE_ROW_RENDER_EVENT
  • ROW_MASTER
  • OVERLAY_NODE
  • …and more.