Lattice Grid Buy a licence

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

Building…
Loading a live grid…

The configuration

<script>
  import { createGrid, createStat } from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/lattice-grid.esm.min.js';
  import createLatticeAction from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/modules/svelte.esm.min.js';

  const lattice = createLatticeAction({ createGrid });

  const USD = { style: 'currency', currency: 'USD', maximumFractionDigits: 0 };

  // The book captured as it is created, so the derived grids below can name it.
  let bookGrid;
  let tileEl;

  const bookConfig = {
    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
    // onGrid hands over the live grid the moment it is created. A tile over the
    // book: scope: 'all' holds the company total while the grid and the derived
    // grids follow the filter.
    onGrid: (grid) => {
      bookGrid = grid;
      createStat({ grid, container: tileEl, title: 'Company total', of: 'amount', fn: 'sum', scope: 'all' });
    },
  };

  // Group and rank: revenue per rep, top five, following the book's filter. The
  // getter defers the read of the source to when this grid is created, by which
  // point the book above it exists.
  const repsConfig = {
    autoHeight: true,
    source: { mode: 'derived', get from() { return bookGrid; }, 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.
  const skusConfig = {
    autoHeight: true,
    source: { mode: 'derived', get from() { return bookGrid; }, 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' }],
  };
</script>

<svelte:head>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/lattice-grid.min.css">
</svelte:head>

<div use:lattice={bookConfig} style="height: 320px"></div>
<div use:lattice={repsConfig} style="margin-top: 16px"></div>
<div use:lattice={skusConfig} style="margin-top: 16px"></div>
<div bind:this={tileEl} style="margin-top: 16px"></div>