📘
Slickgrid-React
Live DemoGitHub
  • Introduction
  • Getting Started
    • Quick start
  • Styling
    • Dark Mode
    • Styling CSS/SASS/Themes
  • Column Functionalities
    • Cell Menu (Action Menu)
    • Editors
      • Autocomplete
      • new Date Picker (vanilla-calendar)
      • LongText (textarea)
      • Select Dropdown Editor (single/multiple)
    • Filters
      • Autocomplete
      • Input Filter (default)
      • Select Filter (dropdown)
      • Compound Filters
      • Range Filters
      • Custom Filter
      • Styling Filled Filters
      • Single Search Filter
    • Formatters
    • Sorting
  • Events
    • Available events
    • On Events
  • Slick Grid/DataView Objects
    • Slick Grid/DataView Objects
  • Grid Functionalities
    • Auto-Resize / Resizer Service
    • Resize by Cell Content
    • Column Picker
    • Composite Editor Modal
    • Custom Tooltip
    • Add, Update or Highlight a Datagrid Item
    • Dynamically Add CSS Classes to Item Rows
    • Column & Row Spanning
    • Context Menu
    • Custom Footer
    • Excel Copy Buffer Plugin
    • Export to Excel
    • Export to File (csv/txt)
    • Grid Menu
    • Grid State & Presets
    • Grouping & Aggregators
    • Header & Footer Slots
    • Header Menu & Header Buttons
    • Infinite Scroll
    • Pinning (frozen) of Columns/Rows
    • Providing data to the grid
    • Row Detail
    • Row Selection
    • Tree Data Grid
    • Row Based Editing Plugin
  • Developer Guides
    • CSP Compliance
  • Localization
    • with I18N
    • with Custom Locales
  • Backend Services
    • Custom Backend Service
    • OData
    • GraphQL
      • JSON Result Structure
      • Filtering Schema
      • Pagination Schema
      • Sorting Schema
  • Testing
    • Testing Patterns
  • Migrations
    • Migration Guide to 3.x (2023-05-29)
    • Migration Guide to 4.x (2023-12-15)
    • Migration Guide to 5.x (2024-05-10)
    • Versions 6 to 8 are skipped...
    • Migration Guide to 9.x (2025-05-10)
Powered by GitBook
On this page
Edit on GitHub
  1. Grid Functionalities

Column & Row Spanning

PreviousDynamically Add CSS Classes to Item RowsNextContext Menu

Last updated 3 days ago

Description

You can use Colspan and/or Rowspan by using the DataView Item Metadata Provider, however please note that row spanning is under a flag because of its small perf hit (rowspan requires an initial loop through of all row item metadata to map all row span).

[!NOTE] Please note that colspan and rowspan have multiple constraints that you must be aware, any side effects will not keep anything in sync since metadata are based on grid row index based... for example: Filtering/Sorting/Paging/ColumnReorder/ColumnHidding These side effect will require user's own logic to deal with such things!

Demo

Colspan / Rowspan

/

/

Basic Usage

You can see a basic example below where we set static metadata, however you will must often use it with dynamic metadata, and it works in both cases. From the example below, the first object key is a number, 0 in our case, which represents the row index (again this can be dynamic). Then if we continue drilling down, we get a columns property which holds another object containing all the column indexes that will have a span (which can be individual colspan, rowspan or both of them at the same time).

What if we have a side effect that kicks in, for example a Sorting, Filtering, ...? Well, that is where you the developer will have to add your own logic to update this metadata with the expected code logic of what and how it's supposed to behave. Because as mentioned in the note above, the library is pretty dumb and does not know what is the expected behavior for any side effects and it will not change any of the metadata spans, you have to implement such logic yourself (for example, if we drag a column to another position then the rowspan will stay at the same exact column index which is most probably not what you want, you could subscribe to the onColumnsChanged to deal with this one). You can see the full list of Events that you can listen for changes and implement necessary callback to update your metadata accordingly (see docs).

Component

import { Column, GridOption } from 'slickgrid-react';
import { useState } from 'react';

const Example: React.FC = () => {
  const [dataset, setDataset] = useState<any[]>([]);
  const [columns, setColumns] = useState<Column[]>([]);
  const [options, setOptions] = useState<GridOption>();
  const reactGridRef = useRef();

  // metadata can be dynamic too, it doesn't have to be preset
  const metadata: ItemMetadata | Record<number, ItemMetadata> = {
    0: {
      columns: {
        1: { rowspan: 2 },
        2: { colspan: 2 },
        10: { colspan: 3, rowspan: 10 },
        13: { colspan: 2 },
        17: { colspan: 2, rowspan: 2 },
      },
    }
  };

  useEffect(() => defineGrid(), []);

  function reactGridReady(reactGrid: SlickgridReactInstance) {
    reactGridRef.current = reactGrid;
  }

  function defineGrid() {
    setColumns([ /*...*/ ]);
    setOptions({
      enableCellNavigation: true,
      enableCellRowSpan: true, // required for rowspan to work
      dataView: {
        globalItemMetadataProvider: {
          getRowMetadata: (_item, row) => {
            return metadata[row];
          },
        },
      },
      rowTopOffsetRenderType: 'top', // rowspan doesn't render well with 'transform', default is 'top'
    });
  }
}
Employee Timesheets
Demo Component
Large Dataset
Demo Component
List of Available Events