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.13.0/lattice-grid.min.css">
<script src="https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.13.0/lattice-grid.min.js"></script>
<div id="book" style="height:320px"></div>
<div id="reps" style="margin-top:16px"></div>
<div id="skus" style="margin-top:16px"></div>
<script>
const USD = { style: 'currency', currency: 'USD', maximumFractionDigits: 0 };
const book = LatticeGrid.createGrid(document.getElementById('book'), {
rowKey: 'id', 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' },
],
rows, // each sale carries a lines: [{ sku, product, qty, value }] array
});
// Group and rank: revenue per rep, top five, following the book's filter.
LatticeGrid.createGrid(document.getElementById('reps'), {
autoHeight: true,
source: { mode: 'derived', from: book, 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.
LatticeGrid.createGrid(document.getElementById('skus'), {
autoHeight: true,
source: { mode: 'derived', from: book, 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.
const tile = () => document.body.appendChild(document.createElement('div'));
LatticeGrid.createStat({ grid: book, container: tile(), title: 'Company total', of: 'amount', fn: 'sum', scope: 'all' });
</script>