.example-wrapper {
display: flex;
flex-direction: column;
height: 100%;
}
.example-header {
margin-bottom: 10px;
}
#myGrid {
flex: 1 1 0px;
width: 100%;
}
/**
* when pinned to top or bottom, grand total row does
* not update on filter changes
* *****
* - select "pinnedBottom" or "pinnedTop" from the dropdown
* - filter by country
* - grand total data does not update
*/
import {
ClientSideRowModelModule,
ColDef,
GridApi,
GridOptions,
ModuleRegistry,
PinnedRowModule,
TextFilterModule,
RowPinnedType,
ValidationModule,
createGrid,
themeQuartz,
} from "ag-grid-community";
import { ContextMenuModule, RowGroupingModule } from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";
ModuleRegistry.registerModules([
ClientSideRowModelModule,
RowGroupingModule,
ContextMenuModule,
PinnedRowModule,
TextFilterModule,
...(process.env.NODE_ENV !== "production" ? [ValidationModule] : []),
]);
const columnDefs: ColDef[] = [
{ field: "athlete" },
{ field: "country", rowGroup: true, filter: true },
{ field: "sport" },
{ field: "gold", aggFunc: "sum" },
];
let gridApi: GridApi<IOlympicData>;
const gridOptions: GridOptions<IOlympicData> = {
defaultColDef: {
flex: 1,
},
autoGroupColumnDef: {
headerName: "Country",
},
columnDefs,
rowData: null,
enableRowPinning: true,
onFirstDataRendered: () => {
const value = getGrandTotalRow();
if (value === "isRowPinned") {
setGrandTotalRow(gridApi, "bottom");
setIsRowPinned(gridApi, "top");
} else {
setGrandTotalRow(gridApi, value);
}
},
theme: themeQuartz.withParams({
pinnedRowBorder: {
width: 2,
},
}),
};
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
fetch("https://www.ag-grid.com/example-assets/small-olympic-winners.json")
.then((response) => response.json())
.then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
function getGrandTotalRow() {
return document.querySelector<HTMLSelectElement>("#select-grand-total-row")
?.value as GridOptions["grandTotalRow"] | "isRowPinned";
}
function setGrandTotalRow(
api: GridApi<IOlympicData>,
value: GridOptions["grandTotalRow"],
) {
api.setGridOption("grandTotalRow", value);
}
function setIsRowPinned(api: GridApi<IOlympicData>, value: RowPinnedType) {
api.setGridOption("isRowPinned", (node) => {
if (node.level === -1 && node.footer) {
return value;
}
});
}
function update() {
const value = getGrandTotalRow();
if (value === "isRowPinned") {
setGrandTotalRow(gridApi, "bottom");
setIsRowPinned(gridApi, "top");
} else {
setGrandTotalRow(gridApi, value);
}
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).update = update;
}
<!doctype html>
<html lang="en">
<head>
<title>Typescript Example - Row Pinning - Pinning Grand Total</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="robots" content="noindex" />
<link
href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;700&display=swap"
rel="stylesheet"
/>
<style media="only screen">
:root,
body {
height: 100%;
width: 100%;
margin: 0;
box-sizing: border-box;
-webkit-overflow-scrolling: touch;
}
html {
position: absolute;
top: 0;
left: 0;
padding: 0;
overflow: auto;
font-family: -apple-system, "system-ui", "Segoe UI", Roboto,
"Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol",
"Noto Color Emoji";
}
body {
padding: 16px;
overflow: auto;
background-color: transparent;
}
</style>
</head>
<body>
<div class="example-wrapper">
<div class="example-header">
<select id="select-grand-total-row" onchange="update()">
<option value="pinnedBottom">pinnedBottom</option>
<option value="pinnedTop">pinnedTop</option>
<option value="bottom">bottom</option>
<option value="top">top</option>
<option value="isRowPinned">isRowPinned</option>
</select>
</div>
<div id="myGrid" style="height: 100%"></div>
</div>
<script>
(function () {
const appLocation = "";
window.__basePath = appLocation;
})();
</script>
<script>
var appLocation = "";
var boilerplatePath = "";
var systemJsMap = {
"ag-grid-community":
"https://www.ag-grid.com/archive/33.3.0/files/ag-grid-community",
"ag-grid-enterprise":
"https://www.ag-grid.com/archive/33.3.0/files/ag-grid-enterprise",
};
var systemJsPaths = {
"@ag-grid-community/locale":
"https://www.ag-grid.com/archive/33.3.0/files/@ag-grid-community/locale/dist/package/main.cjs.js",
"ag-charts-community":
"https://www.ag-grid.com/archive/33.3.0/files/ag-charts-community/dist/package/main.cjs.js",
"ag-charts-core":
"https://www.ag-grid.com/archive/33.3.0/files/ag-charts-core/dist/package/main.cjs.js",
"ag-charts-enterprise":
"https://www.ag-grid.com/archive/33.3.0/files/ag-charts-enterprise/dist/package/main.cjs.js",
"ag-charts-types":
"https://www.ag-grid.com/archive/33.3.0/files/ag-charts-types/dist/package/main.cjs.js",
};
</script>
<script src="https://cdn.jsdelivr.net/npm/systemjs@0.19.47/dist/system.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import("main.ts").catch(function (err) {
document.body.innerHTML =
'<div class="example-error" style="background:#fdb022;padding:1rem;">' +
"Example Error: " +
err +
"</div>";
console.error(err);
});
</script>
<link rel="stylesheet" href="style.css" />
</body>
</html>
export interface IOlympicData {
athlete: string,
age: number,
country: string,
year: number,
date: string,
sport: string,
gold: number,
silver: number,
bronze: number,
total: number
}
if (typeof window !== 'undefined') {
var waitSeconds = 100;
var head = document.getElementsByTagName('head')[0];
var isWebkit = !!window.navigator.userAgent.match(/AppleWebKit\/([^ ;]*)/);
var webkitLoadCheck = function (link, callback) {
setTimeout(function () {
for (var i = 0; i < document.styleSheets.length; i++) {
var sheet = document.styleSheets[i];
if (sheet.href == link.href) return callback();
}
webkitLoadCheck(link, callback);
}, 10);
};
var cssIsReloadable = function cssIsReloadable(links) {
// Css loaded on the page initially should be skipped by the first
// systemjs load, and marked for reload
var reloadable = true;
forEach(links, function (link) {
if (!link.hasAttribute('data-systemjs-css')) {
reloadable = false;
link.setAttribute('data-systemjs-css', '');
}
});
return reloadable;
};
var findExistingCSS = function findExistingCSS(url) {
// Search for existing link to reload
var links = head.getElementsByTagName('link');
return filter(links, function (link) {
return link.href === url;
});
};
var noop = function () {};
var loadCSS = function (url, existingLinks) {
const stylesUrl = url.includes('styles.css') || url.includes('style.css');
return new Promise((outerResolve, outerReject) => {
setTimeout(
() => {
new Promise(function (resolve, reject) {
var timeout = setTimeout(function () {
reject('Unable to load CSS');
}, waitSeconds * 1000);
var _callback = function (error) {
clearTimeout(timeout);
link.onload = link.onerror = noop;
setTimeout(function () {
if (error) {
reject(error);
outerReject(error);
} else {
resolve('');
outerResolve('');
}
}, 7);
};
var link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = url;
link.setAttribute('data-systemjs-css', '');
if (!isWebkit) {
link.onload = function () {
_callback();
};
} else {
webkitLoadCheck(link, _callback);
}
link.onerror = function (event) {
_callback(event.error || new Error('Error loading CSS file.'));
};
if (existingLinks.length) head.insertBefore(link, existingLinks[0]);
else head.appendChild(link);
})
// Remove the old link regardless of loading outcome
.then(
function (result) {
forEach(existingLinks, function (link) {
link.parentElement.removeChild(link);
});
return result;
},
function (err) {
forEach(existingLinks, function (link) {
link.parentElement.removeChild(link);
});
throw err;
}
);
},
stylesUrl ? 5 : 0
);
});
};
exports.fetch = function (load) {
// dont reload styles loaded in the head
var links = findExistingCSS(load.address);
if (!cssIsReloadable(links)) return '';
return loadCSS(load.address, links);
};
} else {
var builderPromise;
function getBuilder(loader) {
if (builderPromise) return builderPromise;
return (builderPromise = System['import']('./css-plugin-base.js', module.id).then(function (CSSPluginBase) {
return new CSSPluginBase(function compile(source, address) {
return {
css: source,
map: null,
moduleSource: null,
moduleFormat: null,
};
});
}));
}
exports.cssPlugin = true;
exports.fetch = function (load, fetch) {
if (!this.builder) return '';
return fetch(load);
};
exports.translate = function (load, opts) {
if (!this.builder) return '';
var loader = this;
return getBuilder(loader).then(function (builder) {
return builder.translate.call(loader, load, opts);
});
};
exports.instantiate = function (load, opts) {
if (!this.builder) return;
var loader = this;
return getBuilder(loader).then(function (builder) {
return builder.instantiate.call(loader, load, opts);
});
};
exports.bundle = function (loads, compileOpts, outputOpts) {
var loader = this;
return getBuilder(loader).then(function (builder) {
return builder.bundle.call(loader, loads, compileOpts, outputOpts);
});
};
exports.listAssets = function (loads, opts) {
var loader = this;
return getBuilder(loader).then(function (builder) {
return builder.listAssets.call(loader, loads, opts);
});
};
}
// Because IE8?
function filter(arrayLike, func) {
var arr = [];
forEach(arrayLike, function (item) {
if (func(item)) arr.push(item);
});
return arr;
}
// Because IE8?
function forEach(arrayLike, func) {
for (var i = 0; i < arrayLike.length; i++) {
func(arrayLike[i]);
}
}
(function (global) {
process = { env: { NODE_ENV: 'development' } };
System.config({
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {},
meta: {
typescript: {
exports: 'ts',
},
'*.css': { loader: 'css' },
},
paths: {
// paths serve as alias
'npm:': 'https://cdn.jsdelivr.net/npm/',
...systemJsPaths,
},
// map tells the System loader where to look for things
map: {
css: (boilerplatePath.length === 0 ? `./` : `${boilerplatePath}/`) + 'css.js',
ts: 'npm:plugin-typescript@8.0.0/lib/plugin.js',
tslib: 'npm:tslib@2.3.1/tslib.js',
typescript: 'npm:typescript@5.4.5/lib/typescript.min.js',
// appLocation comes from index.html
app: appLocation,
...systemJsMap,
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
css: {},
app: {
main: './main.ts',
defaultExtension: 'ts',
},
'ag-grid-community': {
main: './dist/package/main.cjs.js',
defaultExtension: 'js',
format: 'cjs',
},
'ag-grid-enterprise': {
main: './dist/package/main.cjs.js',
defaultExtension: 'js',
format: 'cjs',
},
'ag-charts-types': {
defaultExtension: 'js',
format: 'cjs',
},
'ag-charts-core': {
defaultExtension: 'js',
format: 'cjs',
},
'ag-charts-community': {
defaultExtension: 'js',
format: 'cjs',
},
'ag-charts-enterprise': {
defaultExtension: 'js',
format: 'cjs',
},
'@ag-grid-community/locale': {
format: 'cjs',
},
},
});
})(this);
window.addEventListener('error', (e) => {
console.error('ERROR', e.message, e.filename);
});
{
"name": "ag-grid-example",
"dependencies": {
"ag-grid-community": "33.3.0",
"ag-grid-enterprise": "33.3.0"
},
"devDependencies": {
"@types/node": "^22"
}
}