Column Spanning

By default, each cell will take up the width of one column. You can change this behavior to allow cells to span multiple
columns. This feature is similar to ‘cell merging’ in Excel or ‘column spanning’ in HTML tables.

Note about column spanning
Range selection (An AG Grid Enterprise feature) 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.

Configuring Column Spanning

Configure column spanning on column definitions. To have a cell span more than one column, return how many columns to
span in the colSpan property.

columnDefs = [
    {
        'field': 'country',
        # col span is 2 for rows with Russia, but 1 for everything else
        'colSpan': {"function": "params.data.country === 'Russia' ? 2 : 1"},
    }
]

Below is a basic example of using column spanning. It arbitrarily sets column span on some cells to demonstrate column
spanning features.

In this example:

View the CSS classes used for this example

These CSS classes must be added to any *.css file in the assets folder.

See Loading CSS files for more information.

.spanned-col {
    background-color: #119dff;
}

View JavaScript functions used for this example

These JavaScript functions must be added to the dashAgGridFunctions.js file in the assets folder.

See JavaScript Functions
for more information.

var dagfuncs = window.dashAgGridFunctions = window.dashAgGridFunctions || {};

dagfuncs.colSpanningSimple = function (params) {
    const country = params.data.country;
    if (country === 'Russia') {
        // have all Russia cells in column country of width of 2 columns
        return 2;
    } else if (country === 'United States') {
        // have all United States cells in column country of width of 4 columns
        return 4;
    } else {
        // all other rows should be just normal
        return 1;
    }
}

Column Spanning Complex Example

Column spanning is typically used for creating reports with AG Grid. Below is something that would be more typical of
the column spanning feature.

In this example:

View the CSS classes used for this example

These CSS classes must be added to any *.css file in the assets folder.

See Loading CSS files for more information.

.header-cell {
    background-color: #119dff;
    font-size: 25px;
    font-weight: bold;
    text-align: center;
}

.quarters-cell {
    background-color: #66c2a5;
    font-weight: bold;
}

View JavaScript functions used for this example

These JavaScript functions must be added to the dashAgGridFunctions.js file in the assets folder.

See JavaScript Functions
for more information.

var dagfuncs = window.dashAgGridFunctions = window.dashAgGridFunctions || {};

function isHeaderRow(params) {
    return params.data.section === 'big-title';
}

function isQuarterRow(params) {
    return params.data.section === 'quarters';
}

dagfuncs.janColSpan = function (params) {
    if (isHeaderRow(params)) {
        return 6;
    } else if (isQuarterRow(params)) {
        return 3;
    } else {
        return 1;
    }
}

dagfuncs.aprColSpan = function (params) {
    if (isQuarterRow(params)) {
        return 3;
    } else {
        return 1;
    }
}