footer
Adds a summary row to the table's <tfoot>: a sum, an average, a count or your own value per column, formatted like the cells above it.
import { Datagrid, column, footer } from "@akinozgen17/datagrids";
import { agents, statusLabels, statusTones } from "./data.js";
new Datagrid("#mod-footer-page", {
label: "Field agents (page totals)",
columns: [
column.text("name", { title: "Name" }),
column.text("unit", { title: "Unit" }),
column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
column.money("amount", { title: "Amount", currency: "USD", locale: "en-US" }),
],
data: agents(60),
pageSize: 10,
use: [
footer({
cols: {
status: (rows) => `${rows.filter((r) => r.status === "open").length} open`,
amount: "sum",
},
}),
],
});<table id="mod-footer-page"></table>On a server grid, show a total the server computed over every matching row (here the summary.total key of the yajra response):
import { Datagrid, column, serverSide, yajra, footer } from "@akinozgen17/datagrids";
import { statusLabels, statusTones } from "./data.js";
new Datagrid("#mod-footer-response", {
label: "Field agents (server total)",
columns: [
{ key: "ref", title: "Ref" },
{ key: "name", title: "Name" },
column.badge("status", { title: "Status", labels: statusLabels, tones: statusTones }),
column.money("amount", { title: "Amount", currency: "USD", locale: "en-US" }),
],
pageSize: 10,
use: [
serverSide({ url: "/api/agents", protocol: yajra(), csrf: true }),
footer({
scope: "response",
label: "All matching records",
cols: { amount: (rows, raw) => raw?.summary?.total },
}),
],
});<table id="mod-footer-response"></table>When to use it
Use it for totals that belong under their column: amounts, quantities, counts. For totals shown elsewhere on the page (KPI tiles, a heading), use metaBind.
Usage
import { Datagrid, footer } from "@akinozgen17/datagrids";
new Datagrid("#agents", {
label: "Field agents",
columns,
data: rows,
use: [footer({ cols: { amount: "sum" } })],
});Every matching row instead of the current page (client grids), with your own label:
footer({ cols: { amount: "sum", ref: "count" }, scope: "response", label: "All agents" });A server total from the response — raw is the last response body:
footer({ scope: "response", cols: { amount: (rows, raw) => raw?.summary?.total } });Aggregates
| Value | Result |
|---|---|
"sum" | Sum of the column's numbers |
"avg" | Average of the column's numbers (empty cells don't count) |
"count" | Number of non-empty cells |
(rows, raw, grid) => value | Your value: a number (formatted like the column), a string or a DOM node |
Numbers are read the way exports read them: exportValue first, and numeric text in Turkish format ("1.234,50") is understood. Sums are rounded to remove floating-point noise. On money and number columns the result is rendered with the column's own format.
Options
| Option | Type | Default | Description |
|---|---|---|---|
cols | Record<string, "sum" | "avg" | "count" | function> | — (required) | Column key → aggregate. |
scope | "page" | "response" | "page" | page: the rows on screen. response: every filtered row on client grids; on server grids the built-in aggregates still see the page, so read server totals from raw with a function. |
label | string | "Page total" / "Total" (by scope) | Text in the first visible column(s) that carry no value. |
messages | { page, total } | locale (en / tr) | Default labels. |
can | boolean | true | Permission flag: false → no summary row. |
API added to the grid
Available as grid.footer (typed: grid.module(footer)).
| Member | Returns | Description |
|---|---|---|
values() | Record<string, unknown> | The last computed values by column key. |
refresh() | void | Recomputes and repaints the row. |
Events
None of its own. It repaints after every render and columnchange.
Pipelines it plugs into
- Render: writes one
<tr>into the table's<tfoot>(creating it when needed); hidden while the table has no rows. An existing<tfoot>is restored ondestroy(). - Export:
excel({ footer: true }),csv({ footer: true })andprint({ footer: true })add the row after the data — as numbers in Excel.
Works with
- Enhances excel, csv and print (
footer: true). - With colVis: hidden columns are skipped; the label widens over the leading columns without a value.
- On server grids pair it with a server-computed total (yajra
->with('summary', …)).
Accessibility
The label is a <th scope="row">, so screen readers announce "Page total" with each value in the row. Figures use tabular numerals and the same alignment as the column.
Replaces in DataTables
| DataTables (1.10 / 2.x / 3.x) | datagrids |
|---|---|
footerCallback + api.column(i, { page: "current" }).data().reduce(…) | footer({ cols: { amount: "sum" } }) |
api.column(i, { search: "applied" }) (all filtered rows) | scope: "response" (client grids) |
api.ajax.json().summary in footerCallback | A function reading raw |
A hand-written <tfoot> | Created and filled for you |
Gotchas
- The total equals the page total on a server grid. The browser only has the current page; return the server's total from
rawwith a function. - The console says
footer: unknown column "amont". A key incolsmatches no column. - There is no label. The first visible column has a value; the label only uses leading columns without one.
- The row is gone when nothing matches. By design: an empty table has no summary.