Lattice Grid Buy a licence

demo D197

Running totals in Svelte

The one derived value that depends on the display order

running: { of, kind: 'total' }

This grid shows a running total, the one derived value that depends on the display order, accumulating down the rows. It gives a cumulative figure that reads correctly in sequence, so a balance or a tally builds row by row.

Building…
Loading a live grid…

This is the Svelte version. A use action attaches the grid to a plain element, so no framework runtime is passed in and the component stays small, while the configuration is ordinary Svelte state.

The configuration

<script>
  import { createGrid } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.esm.min.js';
  import createLatticeAction from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/svelte.esm.min.js';

  const lattice = createLatticeAction({ createGrid });

  const config = {
    rowKey: 'id',
    columns: [
      { field: 'date', title: 'Date', type: 'dateString', layout: { pin: 'start', width: 150 } },
      { field: 'amount', title: 'Amount', type: 'number', format: { style: 'currency', currency: 'USD' }, total: 'sum' },
      // running, not shadow: it reads in display order, so sorting the grid
      // recomputes every value. Group headings and totals rows are skipped.
      { id: 'cum', title: 'Running', type: 'number', format: { style: 'currency', currency: 'USD' }, running: { of: 'amount', kind: 'total' } },
      { id: 'share', title: 'Cumulative %', type: 'number', format: { style: 'percent', decimals: 1 }, running: { of: 'amount', kind: 'percent' } },
    ],
    state: { sort: [{ col: 'date', dir: 'asc' }] },
    rows,  // one row a day: date, amount
  };
</script>

<svelte:head>
  <link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
</svelte:head>

<div use:lattice={config} style="height: 440px"></div>