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
Then row spanning is set configured at the column definition level. To have a cell
span more than one row, return how many rows to span in the callback
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 demonstrations 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 background to the cell. This is important as if background was not given, 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 is 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 (eg 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 application to apply background style to spanning cells so that cells overwritten cannot be seen.
- Overwritten cells will still exist, but will not be visible. This means cell navigation will go to the other cells - eg if a row spanned cell has focus, and the user hits '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, a range is no longer a perfect rectangle.