horee-gridb0.0.3

Grouping · Tree · Merge

Group rows by value and see subtotals, expand parent–child rows as a tree, or draw equal values above and below as one cell. All three change only what is shown; the source row array stays as it is.

Row grouping

grouping.js
import { createGrid } from 'horee-grid'

const depts = ["Sales","Admin","Production"]
const teams = ["Team 1","Team 2"]
const rows = Array.from({ length: 60 }, (_, i) => ({
  id: i + 1,
  item: `Item ${i + 1}`,
  dept: depts[(i * 7) % 3],
  team: teams[(i * 5) % 2],
  amount: (((i * 104729) % 90) + 10) * 10000,
  qty: ((i * 31) % 9) + 1
}))

const grid = createGrid(host, {
  columns: [
    { field: 'item', title: "Item", width: 170 },
    { field: 'dept', title: "Dept", width: 100 },
    { field: 'team', title: "Team", width: 90 },
    { field: 'amount', title: "Amount", type: 'number', aggregate: 'sum', width: 130, editable: true },
    { field: 'qty', title: "Qty", type: 'number', aggregate: 'avg', decimals: 1, width: 80, editable: true }
  ],
  rows,
  group: { by: ['dept'] },
  locale: 'en'
})

toolbar.innerHTML = `
  <button data-a="dept">By dept</button>
  <button data-a="two">By dept · team</button>
  <button data-a="bottom">Subtotal below</button>
  <button data-a="none">Ungroup</button>
  <button data-a="expand">Expand all</button>
  <button data-a="collapse">Collapse all</button>`
toolbar.onclick = e => {
  const a = e.target.dataset.a
  if (a === 'dept') grid.setGroup({ by: ['dept'] })
  if (a === 'two') grid.setGroup({ by: ['dept', 'team'] })
  if (a === 'bottom') grid.setGroup({ by: ['dept', 'team'], subtotal: 'bottom' })
  if (a === 'none') grid.setGroup(null)
  if (a === 'expand') grid.expandAll()
  if (a === 'collapse') grid.collapseAll()
}
createGrid(host, {
  columns: [
    { field: 'dept', title: 'Dept' },
    { field: 'team', title: 'Team' },
    { field: 'amount', title: 'Amount', type: 'number', aggregate: 'sum' },
    { field: 'qty', title: 'Qty', type: 'number', aggregate: 'avg', decimals: 1 }
  ],
  rows,
  group: { by: ['dept', 'team'] }      // first = outer level
})

grid.setGroup({ by: ['dept'], subtotal: 'bottom' })   // change
grid.setGroup(null)                                   // ungroup
  • Groups are ordered by value, with the blank group last. Sort the grouped column descending and the groups follow.
  • Rows inside a group follow the current sort. Only rows that pass the filter are grouped.
  • A group row shows an expand button, the value and the row count in the first column.

Aggregates · subtotals

Give a column aggregate and it is calculated for each group.

aggregateValue
'sum' · 'avg' · 'min' · 'max'Only values readable as numbers count ('1,000' is a number too). Blank if there are none
'count'Rows in the group
(rows) => valueYour own calculation. Receives the group's rows (after filtering)
  • Aggregates show on the group header row by default. With subtotal: 'bottom', an expanded group gets a subtotal row below it; a collapsed group shows them on its header row.
  • Editing a cell recalculates the aggregates and the row's group straight away.
  • Aggregate text follows the column's format · type · decimals. format also receives cell.group (the group info).

Expand · collapse

ActionResult
Click the ▸ buttonExpands or collapses that group
Space on a group rowSame
grid.toggleRow(row, open?)Group row or tree parent row. Without open it toggles
grid.expandAll() · grid.collapseAll()Expand all · collapse all

To start collapsed, use group: { by: [...], expanded: false }.

Working with group rows

Group rows and subtotal rows are made by the grid. They cannot be edited and are left out of selection, copy, Excel export and status bar figures.

  • getViewRows() returns only the visible data rows (rows in collapsed groups are left out). Row numbers and the positions in scrollToRow(index) and insertRows(rows, index) also count data rows.
  • isGroupRow(row) and grid.getGroupInfo(row) tell whether a row is a group row and give its info (level · column · value · rows · values · open · subtotal). Use them where you receive a row, such as onRowClick.
  • Grouping and tree are not used with server data (dataSource).

Tree

tree.js
import { createGrid } from 'horee-grid'

const rows = [["1",null,"Assets",0],["11","1","Current assets",0],["111","11","Cash",1200000],["112","11","Bank deposits",35400000],["113","11","Receivables",18750000],["12","1","Non-current assets",0],["121","12","Buildings",250000000],["122","12","Equipment",8300000],["2",null,"Liabilities",0],["21","2","Payables",12600000],["22","2","Accrued expenses",4100000],["3",null,"Equity",0],["31","3","Capital",100000000]].map(([code, parent, name, balance]) => ({ code, parent, name, balance }))

const grid = createGrid(host, {
  columns: [
    { field: 'name', title: "Account", width: 220 },
    { field: 'code', title: "Code", width: 80, align: 'center' },
    { field: 'balance', title: "Balance", type: 'number', width: 150 }
  ],
  rows,
  tree: { idField: 'code', parentField: 'parent', column: 'name' },
  locale: 'en'
})

toolbar.innerHTML = `
  <button data-a="expand">Expand all</button>
  <button data-a="collapse">Collapse all</button>
  <button data-a="filter">Filter: deposits</button>
  <button data-a="clear">Clear filter</button>`
toolbar.onclick = e => {
  const a = e.target.dataset.a
  if (a === 'expand') grid.expandAll()
  if (a === 'collapse') grid.collapseAll()
  if (a === 'filter') grid.setFilters([{ id: 'code', op: 'eq', value: '112' }])
  if (a === 'clear') grid.setFilters([])
}

When each row has its own id and its parent's id, it shows as a tree. Pass the row array flat.

createGrid(host, {
  columns: [{ field: 'name', title: 'Account' }, { field: 'amount', title: 'Amount', type: 'number' }],
  rows: [
    { code: '1', parent: null, name: 'Assets' },
    { code: '11', parent: '1', name: 'Current assets' },
    { code: '111', parent: '11', name: 'Cash' }
  ],
  tree: { idField: 'code', parentField: 'parent', column: 'name' }
})
  • The column column gets indentation by depth and an expand button.
  • A row whose parent cannot be found (blank or unknown parent id) is at the top level.
  • Sorting happens only among siblings. A filter shows matching rows together with their ancestors.
  • Expanding and collapsing work as for row grouping (▸ button, toggleRow · expandAll · collapseAll, expanded: false). If the tree column cell is not editable, Space toggles too.

Merge equal values

merge.js
import { createGrid } from 'horee-grid'

const rows = [["East","Downtown","Jan",2000000],["East","Downtown","Feb",7000000],["East","Harbor","Jan",5000000],["East","Harbor","Feb",10000000],["West","Airport","Jan",9000000],["West","Airport","Feb",5000000]].map(([region, shop, month, sales]) => ({ region, shop, month, sales }))
const columns = merge => [
  { field: 'region', title: "Region", width: 100, merge },
  { field: 'shop', title: "Store", width: 120, merge },
  { field: 'month', title: "Month", width: 80, align: 'center' },
  { field: 'sales', title: "Sales", type: 'number', width: 140 }
]

const grid = createGrid(host, { columns: columns(true), rows, locale: 'en' })

toolbar.innerHTML = `
  <button data-a="on">Merge</button>
  <button data-a="off">Merge off</button>`
toolbar.onclick = e => {
  const a = e.target.dataset.a
  if (a) grid.setColumns(columns(a === 'on'))
}
columns: [
  { field: 'dept', title: 'Dept', merge: true },
  { field: 'team', title: 'Team', merge: true },
  { field: 'name', title: 'Name' }
]
  • Neighbouring cells above and below with the same displayed text are drawn as one. Blank values are not merged.
  • When a merge run in a merged column to the left ends, the merged columns to its right end too. A different dept keeps teams apart even if they have the same name.
  • Recalculated after sorting, filtering and editing. Works with row grouping (runs end at group rows).
  • Only the look changes. Selection, editing and copy still work per row.