demo D223
A sales dashboard
Top reps by grouping, most-sold products by unnesting the order lines, and five tiles, one ignoring the filter
groupBy · unnest · scope: 'all' · createStat
Loading a live grid…
The configuration
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/lattice-grid.min.css">
<script type="module">
import { defineLatticeGrid } from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/modules/webcomponent.esm.js';
defineLatticeGrid();
</script>
<lattice-grid id="book" row-key="id" style="display: block; height: 320px"></lattice-grid>
<lattice-grid id="reps" style="display: block; margin-top: 16px"></lattice-grid>
<lattice-grid id="skus" style="display: block; margin-top: 16px"></lattice-grid>
<div id="tiles" style="margin-top: 16px"></div>
<script type="module">
import { createStat } from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/modules/webcomponent.esm.js';
const USD = { style: 'currency', currency: 'USD', maximumFractionDigits: 0 };
const bookEl = document.getElementById('book');
bookEl.config = {
toolPanel: { side: 'left', panels: ['filters', 'columns'] },
columns: [
{ field: 'rep', title: 'Sales rep' },
{ field: 'region', title: 'Region', filter: { type: 'set' } },
{ field: 'amount', title: 'Deal value', type: 'number', format: USD, total: 'sum' },
],
};
bookEl.rows = rows; // each sale carries a lines: [{ sku, product, qty, value }] array
// Group and rank: revenue per rep, top five, following the book's filter.
// bookEl.grid is the live grid inside the element.
document.getElementById('reps').config = {
autoHeight: true,
source: { mode: 'derived', from: bookEl.grid, follow: 'filtered', refresh: 'live', groupBy: 'rep',
select: { revenue: { of: 'amount', fn: 'sum' }, deals: { fn: 'count' }, biggest: { of: 'amount', fn: 'max' } },
sort: [{ col: 'revenue', dir: 'desc' }], limit: 5 },
columns: [{ field: 'rep', title: 'Rep' }, { field: 'revenue', title: 'Revenue', type: 'number', format: USD }],
};
// Unnest: the SKUs live in a lines array, so one sale becomes several rows
// before grouping. groupBy reads the dotted path into the unnested element.
document.getElementById('skus').config = {
autoHeight: true,
source: { mode: 'derived', from: bookEl.grid, follow: 'filtered', refresh: 'live', unnest: 'lines', groupBy: 'lines.sku',
select: { product: { of: 'lines.product', fn: 'first' }, units: { of: 'lines.qty', fn: 'sum' } },
sort: [{ col: 'units', dir: 'desc' }], limit: 5 },
columns: [{ field: 'sku', title: 'SKU' }, { field: 'product', title: 'Product' }, { field: 'units', title: 'Units', type: 'number' }],
};
// A tile over the source. scope: 'all' holds the company total while the grid
// and every other tile follow the filter. createStat comes from the element's
// own module, so it shares one engine with bookEl.grid.
const tile = () => document.getElementById('tiles').appendChild(document.createElement('div'));
createStat({ grid: bookEl.grid, container: tile(), title: 'Company total', of: 'amount', fn: 'sum', scope: 'all' });
</script>