Applications can display an overlay on demand regardless of grid state. This is achieved by providing an active overlay which can be one of the provided overlays or be a custom overlay component.
import {
ClientSideRowModelModule,
ColDef,
GridApi,
GridOptions,
ModuleRegistry,
createGrid,
enableDevValidations,
} from "ag-grid-community";
import { CustomOverlay, CustomParams } from "./customOverlay";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([ClientSideRowModelModule]);
interface IAthlete {
athlete: string;
country: string;
}
const columnDefs: ColDef[] = [
{ field: "athlete", flex: 1 },
{ field: "country", flex: 1 },
];
const rowData: IAthlete[] = [
{ athlete: "Michael Phelps", country: "United States" },
{ athlete: "Natalie Coughlin", country: "United States" },
{ athlete: "Aleksey Nemov", country: "Russia" },
{ athlete: "Alicia Coutts", country: "Australia" },
];
let gridApi: GridApi<IAthlete>;
const activeOverlayParams: CustomParams = {
count: 1,
};
const gridOptions: GridOptions<IAthlete> = {
columnDefs,
rowData,
activeOverlay: CustomOverlay,
activeOverlayParams,
};
function showActiveOverlay() {
gridApi.setGridOption("activeOverlay", CustomOverlay);
}
function clearActiveOverlay() {
gridApi.setGridOption("activeOverlay", undefined);
}
function incParam() {
activeOverlayParams.count++;
gridApi.setGridOption("activeOverlayParams", activeOverlayParams);
}
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).showActiveOverlay = showActiveOverlay;
(<any>window).clearActiveOverlay = clearActiveOverlay;
(<any>window).incParam = incParam;
}
Display an Active Overlay Copy Link
To display an overlay on demand set the activeOverlay / activeOverlayParams grid option. To clear the overlay set activeOverlay = undefined.
Display an overlay on demand. If provided takes precedence over the grid provided overlays. agLoadingOverlay, agNoRowsOverlay, agNoMatchingRowsOverlay, agExportingOverlay components map. undefined to clear. |
Custom parameters to be supplied to the activeOverlay component in addition to IOverlayParams. Updating the params will trigger a refresh of the active overlay. |
Implement the IOverlayComp interface to provide a custom overlay the grid will supply IOverlayParams whenever the component is created or refreshed.
interface IOverlayComp<TData = any, TContext = any, TParams extends Readonly<IOverlayParams<TData, TContext>> = IOverlayParams<TData, TContext>> {
// Return the DOM element of your component, this is what the grid puts into the DOM
getGui(): HTMLElement;
// Gets called once by grid when the component is being removed; if your component needs to do any cleanup, do it here
destroy?(): void;
// The init(params) method is called on the component once.
init?(params: TParams): AgPromise<void> | void;
// Gets called when the `overlayComponentParams` grid option is updated
refresh?(params: TParams): void;
}The example below demonstrates using the grid provided overlays as an active overlay. Note the following:
- activeOverlays take precedence over the provided loading overlay.
- activeOverlay can be displayed no matter what the grid state, i.e showing the no-rows overlay even when there are rows.
- StatusOverlay is registered in the components map and shown by setting
activateOverlay = "statusOverlay"to the key used.
import {
ClientSideRowModelModule,
ColDef,
GridApi,
GridOptions,
ModuleRegistry,
createGrid,
enableDevValidations,
} from "ag-grid-community";
import { StatusOverlay } from "./statusOverlay";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([ClientSideRowModelModule]);
interface IAthlete {
athlete: string;
country: string;
}
const columnDefs: ColDef<IAthlete>[] = [
{ field: "athlete", flex: 1 },
{ field: "country", flex: 1 },
];
const rowData: IAthlete[] = [
{ athlete: "Michael Phelps", country: "United States" },
{ athlete: "Alicia Coutts", country: "Australia" },
];
let gridApi: GridApi<IAthlete>;
let statusOverlayCounter = 0;
const gridOptions: GridOptions<IAthlete> = {
columnDefs,
rowData,
components: {
statusOverlay: StatusOverlay,
},
};
function showNoRowsOverlay() {
gridApi.updateGridOptions({
activeOverlay: "agNoRowsOverlay",
activeOverlayParams: undefined,
});
}
function showStatusOverlay() {
gridApi.updateGridOptions({
activeOverlay: "statusOverlay",
activeOverlayParams: {
myCounter: ++statusOverlayCounter,
},
});
}
function hideOverlay() {
gridApi.updateGridOptions({
activeOverlay: undefined,
activeOverlayParams: undefined,
});
}
function setLoading(isChecked: boolean) {
gridApi.updateGridOptions({
loading: isChecked ? true : undefined,
});
}
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
const loadingToggle =
document.querySelector<HTMLInputElement>("#loading-toggle");
if (loadingToggle) {
loadingToggle.addEventListener("change", () =>
setLoading(loadingToggle.checked),
);
setLoading(loadingToggle.checked);
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).showNoRowsOverlay = showNoRowsOverlay;
(<any>window).showStatusOverlay = showStatusOverlay;
(<any>window).hideOverlay = hideOverlay;
}