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

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/lattice-grid.min.css">
<style>
  #book > div { height: 320px; }
  #reps, #skus { margin-top: 16px; }
  #tiles { margin-bottom: 16px; }
</style>
<div id="app"></div>

<script type="module">
  import * as Vue from 'https://esm.sh/vue@3';
  import { createGrid, createStat } from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/lattice-grid.esm.min.js';
  import createLatticeGrid from 'https://cdn.jsdelivr.net/npm/@toclocoinc/lattice-grid@1.28.0/modules/vue.esm.min.js';

  const LatticeGrid = createLatticeGrid({ vue: Vue, createGrid });

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

  Vue.createApp({
    components: { LatticeGrid },
    data() {
      return {
        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
        },
        // The derived shapes read the book's live instance, so they wait for mounted().
        reps: null,
        skus: null,
      };
    },
    mounted() {
      const book = this.$refs.book.grid();

      // Group and rank: revenue per rep, top five, following the book's filter.
      this.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.
      this.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.getElementById('tiles').appendChild(document.createElement('div'));
      createStat({ grid: book, container: tile, title: 'Company total', of: 'amount', fn: 'sum', scope: 'all' });
    },
    template: `
      <div>
        <div id="tiles"></div>
        <div id="book"><lattice-grid ref="book" v-bind="book" /></div>
        <div id="reps"><lattice-grid v-if="reps" v-bind="reps" /></div>
        <div id="skus"><lattice-grid v-if="skus" v-bind="skus" /></div>
      </div>
    `,
  }).mount('#app');
</script>