Aligning two or more grids means columns will be kept aligned in all grids. In other words, column changes to one grid (column width, column order, column visibility, etc.) are reflected in the other grid. This is useful if you have two grids, one above the other such that their columns are vertically aligned, and you want to keep the columns aligned.
Configuration Copy Link
Configure via the grid option alignedGrids.
A list of grids to treat as Aligned Grids.
Provide a list if the grids / apis already exist or return via a callback to allow the aligned grids to be retrieved asynchronously.
If grids are aligned then the columns and horizontal scrolling will be kept in sync. |
To link two grids provide alignedGrids with callbacks that return the corresponding grid apis.
gridOptionsFirst = {
alignedGrids: () => [secondApi]
...
};
gridOptionsSecond = {
alignedGrids: () => [firstApi]
...
}
firstApi = createGrid(gridDiv1, gridOptionsFirst);
secondApi = createGrid(gridDiv2, gridOptionsSecond); Example: Aligned Grids Copy Link
Below shows two grids, both aligned with the other (so any column change to one will be reflected in the other). The following should be noted:
- When either grid is scrolled horizontally, the other grid follows.
- Showing / hiding a column on either grid (via the checkbox) will show / hide the column on the other grid, despite the API being called on one grid only.
- When a column is resized on either grid, the other grid follows.
- When a column group is opened on either grid, the other grid follows.
import {
AlignedGridsModule,
ClientSideRowModelModule,
ColDef,
ColGroupDef,
ColumnApiModule,
ColumnAutoSizeModule,
GridOptions,
ModuleRegistry,
NumberFilterModule,
TextFilterModule,
createGrid,
enableDevValidations,
} from "ag-grid-community";
// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
enableDevValidations();
}
ModuleRegistry.registerModules([
ColumnApiModule,
TextFilterModule,
NumberFilterModule,
ColumnAutoSizeModule,
AlignedGridsModule,
ClientSideRowModelModule,
]);
const columnDefs: (ColDef | ColGroupDef)[] = [
{ field: "athlete" },
{ field: "age" },
{ field: "country" },
{ field: "year" },
{ field: "sport" },
{
headerName: "Medals",
children: [
{
colId: "total",
columnGroupShow: "closed",
valueGetter: "data.gold + data.silver + data.bronze",
},
{ columnGroupShow: "open", field: "gold" },
{ columnGroupShow: "open", field: "silver" },
{ columnGroupShow: "open", field: "bronze" },
],
},
];
const defaultColDef: ColDef = {
filter: true,
minWidth: 100,
};
// this is the grid options for the top grid
const gridOptionsTop: GridOptions = {
defaultColDef,
columnDefs,
alignedGrids: () => [bottomApi],
autoSizeStrategy: {
type: "fitGridWidth",
},
};
const gridDivTop = document.querySelector<HTMLElement>("#myGridTop")!;
const topApi = createGrid(gridDivTop, gridOptionsTop);
// this is the grid options for the bottom grid
const gridOptionsBottom: GridOptions = {
defaultColDef,
columnDefs,
alignedGrids: () => [topApi],
};
const gridDivBottom = document.querySelector<HTMLElement>("#myGridBottom")!;
const bottomApi = createGrid(gridDivBottom, gridOptionsBottom);
function onCbAthlete(value: boolean) {
// we only need to update one grid, as the other is a slave
topApi!.setColumnsVisible(["athlete"], value);
}
function onCbAge(value: boolean) {
// we only need to update one grid, as the other is a slave
topApi!.setColumnsVisible(["age"], value);
}
function onCbCountry(value: boolean) {
// we only need to update one grid, as the other is a slave
topApi!.setColumnsVisible(["country"], value);
}
function setData(rowData: any[]) {
topApi!.setGridOption("rowData", rowData);
bottomApi!.setGridOption("rowData", rowData);
}
fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
.then((response) => response.json())
.then((data) => setData(data));
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).onCbAthlete = onCbAthlete;
(<any>window).onCbAge = onCbAge;
(<any>window).onCbCountry = onCbCountry;
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).onCbAthlete = onCbAthlete;
(<any>window).onCbAge = onCbAge;
(<any>window).onCbCountry = onCbCountry;
}
.test-header {
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: 13px;
margin-bottom: 5px;
}
<div class="test-header" style="height: 5%">
<label><input type="checkbox" checked onchange="onCbAthlete(this.checked)" />Athlete</label>
<label><input type="checkbox" checked onchange="onCbAge(this.checked)" />Age</label>
<label><input type="checkbox" checked onchange="onCbCountry(this.checked)" />Country</label>
</div>
<div id="myGridTop" style="height: 45%"></div>
<div style="height: 5%"></div>
<div id="myGridBottom" style="height: 45%"></div>
Events Copy Link
The events which are fired as part of the grid alignment relationship are as follows:
- Horizontal Scroll
- Column Hidden / Shown
- Column Moved
- Column Group Opened / Closed
- Column Resized
- Column Pinned
Pivots Copy Link
The pivot functionality does not work with aligned grids. This is because pivoting data changes the columns, which would make the aligned grids incompatible, as they are no longer sharing the same set of columns.
Example: Aligned Grid as Footer Copy Link
So why would you want to align grids like this? It's great for aligning grids that have different data but similar columns. Maybe you want to include a footer grid with 'summary' data. Maybe you have two sets of data, but one is aggregated differently to the other.
This example is a bit more useful. In the bottom grid, we show a summary row. Also note the following:
- The top grid has no horizontal scroll bar, suppressed via a grid option.
- The bottom grid has no header, suppressed via a grid option.
autoSizeStrategyis only passed to the top grid, the bottom grid receives the new column widths from the top grid.
Example: Align Column Groups Copy Link
It is possible that you have column groups that are split because of pinning or the order of the columns. The grid below has only two groups that are split, displayed as many split groups. The column aligning also works here in that a change to a split group will open / close all the instances of that group in both tables.
import {
AlignedGridsModule,
ClientSideRowModelModule,
ColGroupDef,
ColumnApiModule,
ColumnAutoSizeModule,
GridApi,
GridOptions,
ModuleRegistry,
TextFilterModule,
createGrid,
enableDevValidations,
} from "ag-grid-community";
// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
enableDevValidations();
}
ModuleRegistry.registerModules([
TextFilterModule,
ColumnAutoSizeModule,
ClientSideRowModelModule,
AlignedGridsModule,
ColumnApiModule,
]);
const columnDefs: ColGroupDef[] = [
{
headerName: "Group 1",
groupId: "Group1",
children: [
{ field: "athlete", pinned: true },
{ field: "age", pinned: true, columnGroupShow: "open" },
{ field: "country" },
{ field: "year", columnGroupShow: "open" },
{ field: "date" },
{ field: "sport", columnGroupShow: "open" },
],
},
{
headerName: "Group 2",
groupId: "Group2",
children: [
{ field: "athlete", pinned: true },
{ field: "age", pinned: true, columnGroupShow: "open" },
{ field: "country" },
{ field: "year", columnGroupShow: "open" },
{ field: "date" },
{ field: "sport", columnGroupShow: "open" },
],
},
];
let topApi: GridApi;
let bottomApi: GridApi;
// this is the grid options for the top grid
const gridOptionsTop: GridOptions = {
defaultColDef: {
filter: true,
minWidth: 120,
},
columnDefs: columnDefs,
alignedGrids: () => [bottomApi],
autoSizeStrategy: {
type: "fitGridWidth",
},
};
// this is the grid options for the bottom grid
const gridOptionsBottom: GridOptions = {
defaultColDef: {
filter: true,
flex: 1,
minWidth: 120,
},
columnDefs: columnDefs,
alignedGrids: () => [topApi],
};
function setData(rowData: any[]) {
topApi!.setGridOption("rowData", rowData);
bottomApi!.setGridOption("rowData", rowData);
// mix up some columns
topApi!.moveColumnByIndex(11, 4);
topApi!.moveColumnByIndex(11, 4);
}
const gridDivTop = document.querySelector<HTMLElement>("#myGridTop")!;
topApi = createGrid(gridDivTop, gridOptionsTop);
const gridDivBottom = document.querySelector<HTMLElement>("#myGridBottom")!;
bottomApi = createGrid(gridDivBottom, gridOptionsBottom);
fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
.then((response) => response.json())
.then((data) => {
setData(data);
});
<div id="myGridTop" style="height: 45%"></div>
<div style="height: 5%"></div>
<div id="myGridBottom" style="height: 45%"></div>
Event Propagation Copy Link
When a grid fires an event, it will be processed by all registered aligned grids. However if a grid is processing such an event, it will not fire an event to other aligned grids. For example, consider the grids A, B and C where B is aligned to A and C is aligned to B (ie A -> B -> C). If A gets a column resized, it will fire the event to B, but B will not fire the event to C. If C is also dependent on A, it needs to be set up directly. This stops cyclic dependencies between grids causing infinite firing of events if two grids are aligned to each other.