By default, each cell will take up the height of one row. You can change this behaviour to allow cells to span multiple rows. This feature is similar to 'cell merging' in Excel or 'row spanning' in HTML tables.
Configuring Row Spanning
To allow row spanning, the grid must have property
Row spanning is then configured at the column definition level. To have a cell
span more than one row, return how many rows to span in the callback
suppressRowTransform=trueis used to stop the grid positioning rows using CSS
transformand instead the grid will use CSS
transformis that CSS
transformcreates a stacking context which constrains CSS
z-indexfrom placing cells on top of other cells in another row. Having cells extend into other rows is necessary for row span which means it will not work when using CSS
transform. The downside to not using
transformis performance; row animation (after sort or filter) will be slower.
The interface for the
rowSpan callback is as follows:
Row Spanning Simple Example
Below shows a simple example using row spanning. The example doesn't make much sense, it just arbitrarily sets row span on some cells for demonstration purposes.
- The Athlete column is configured to span 2 rows for 'Aleksey Nemov' and 4 rows for 'Ryan Lochte'.
- The Athlete column is configured to apply a CSS class to give a background to the cell. This is important because if a background was not set, the cell background would be transparent and the underlying cell would still be visible.
Row Spanning Complex Example
Row spanning will typically be used for creating reports with ag-Grid. Below is something that would be more typical of the row spanning feature. The following can be noted from the example:
- Column Show row spans by 4 rows when it has content.
- Column Show uses CSS class rules to specify background and border.
- Column Show has a custom cell renderer to make use of the extra space.
Constraints with Row Spanning
Row Spanning breaks out of the row / cell calculations that a lot of features in the grid are based on. If using Row Spanning, be aware of the following:
- Responsibility is with the developer to not span past the last row. This is especially true if sorting and filtering (e.g. a cell may span outside the grid after the data is sorted and the cell's row ends up at the bottom of the grid).
- Responsibility is with the developer to apply a background style to spanning cells so that overwritten cells cannot be seen.
- Overwritten cells will still exist, but will not be visible. This means cell navigation will go to the other cells - e.g. if a row spanned cell has focus, and the user hits the 'arrow down' key, the focus will go to a hidden cell.
- Row span does not work with dynamic row height or auto-height. The row span assumes default row height is used when calculating how high the cell should be.
- Sorting and filtering will provide strange results when row spanning. For example a cell may span 4 rows, however applying a filter or a sort will probably change the requirements of what rows should be spanned.
- Range Selection will not work correctly when spanning cells. This is because it is not possible to cover all scenarios, as a range is no longer a perfect rectangle.