A Brief Introduction About Column Spanning In Ag Grid

Posted By : Manisha Kirodiwal | 29-Apr-2018

In default scenario, each cell will occupy the width of a column. But using column spanning can change this behavior. Using column spanning can allow cells to span multiple columns

Configuring Column Spanning

The column spanning is set at the column definition of the ag grid. For a cell to span more than one column, return the number of columns to include in the callback means colDef.colSpan.

colDef = {
    headerName: "Country",
    field: "country",
    colSpan: function(params) {
        return params.data.country==='Russia' ? 2 : 1;
    }
    ...
};

Following is the interface for the colSpan callback:

// function you implement on the column definition
function colSpan(params: ColSpanParams) => number;
interface ColSpanParams {
    node: any, // row node in question
    data: RowNode, // data for the row
    colDef: ColDef, // the col def for the column
    column: Column, // the column object in question
    api: GridApi, // the grid's API
    columnApi: ColumnApi, // the grids column API
    context: any // the provided context
}


Below is a simple example using the expansion column. The example does not make much sense, just set the column section in some cells for demonstration purposes, however, we think it is easier to show the column that covers the familiar data of the Olympic winners before entering a little more in their uses . You can see the following:

 

  • The country column is set to span 2 columns when value is 'Russia' and 3 columns when value is 'United States'.Otherwise rest of the time it is normal (1 column).
  • To help indicate distributed column, the county column is shaded in CSS style. 
  • Resizing extended columns will also change the size of extended cells. For example, changing the size of the column just to the right of 'Country' will rescale all the cells that extend over the resized column.
  • The first 2 columns are anchored. If you drag the country to the anchored area, you will notice that the expansion is restricted within the anchored section. E.g. If you place the country as the last anchored column, the expansion will not occur, since the expansion can only occur in the cells of the same region, and the country no longer has more columns within the anchored region.

import { Component, ViewChild } from "@angular/core";
import { HttpClient } from "@angular/common/http";

@Component({
  selector: "my-app",
  template: `
`
})
export class AppComponent {
  private gridApi;
  private gridColumnApi;

  private columnDefs;
  private defaultColDef;

  constructor(private http: HttpClient) {
    this.columnDefs = [
      {
        headerName: "Athlete",
        field: "athlete",
        pinned: "left"
      },
      {
        headerName: "Age",
        field: "age",
        pinned: "left"
      },
      {
        headerName: "Country",
        field: "country",
        colSpan: function(params) {
          var country = params.data.country;
          if (country === "Russia") {
            return 2;
          } else if (country === "United States") {
            return 4;
          } else {
            return 1;
          }
        }
      },
      {
        headerName: "Year",
        field: "year"
      },
      {
        headerName: "Date",
        field: "date"
      },
      {
        headerName: "Sport",
        field: "sport"
      },
      {
        headerName: "Gold",
        field: "gold"
      },
      {
        headerName: "Silver",
        field: "silver"
      },
      {
        headerName: "Bronze",
        field: "bronze"
      },
      {
        headerName: "Total",
        field: "total"
      }
    ];
    this.defaultColDef = { width: 100 };
  }

  onGridReady(params) {
    this.gridApi = params.api;
    this.gridColumnApi = params.columnApi;

    this.http
      .get("https://raw.githubusercontent.com/ag-grid/ag-grid-docs/master/src/olympicWinnersSmall.json")
      .subscribe(data => {
        params.api.setRowData(data);
      });
  }
}

 

About Author

Author Image
Manisha Kirodiwal

Manisha is a Web Application developer and she is good in working with Angular, TypeScript , JavaScript and Jquery. In free time she loves to dance and cooking.

Request for Proposal

Name is required

Comment is required

Sending message..