horee-gridb0.0.5

分组 · 树 · 合并

按相同的值把行分组并查看小计,把父子关系展开为树,或把上下相邻的相同值画成一个单元格。三者都只改变页面上的显示,原始行数组保持不变。

行分组

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

const depts = ["销售","管理","生产"]
const teams = ["一组","二组"]
const rows = Array.from({ length: 60 }, (_, i) => ({
  id: i + 1,
  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: "品目", width: 170 },
    { field: 'dept', title: "部门", width: 100 },
    { field: 'team', title: "组", width: 90 },
    { field: 'amount', title: "金额", type: 'number', aggregate: 'sum', width: 130, editable: true },
    { field: 'qty', title: "数量", type: 'number', aggregate: 'avg', decimals: 1, width: 80, editable: true }
  ],
  rows,
  group: { by: ['dept'] },
  locale: 'zh'
})

toolbar.innerHTML = `
  <button data-a="dept">按部门</button>
  <button data-a="two">按部门 · 组</button>
  <button data-a="bottom">下方小计</button>
  <button data-a="none">取消分组</button>
  <button data-a="expand">全部展开</button>
  <button data-a="collapse">全部折叠</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: '部门' },
    { field: 'team', title: '组' },
    { field: 'amount', title: '金额', type: 'number', aggregate: 'sum' },
    { field: 'qty', title: '数量', type: 'number', aggregate: 'avg', decimals: 1 }
  ],
  rows,
  group: { by: ['dept', 'team'] }      // 前面的是外层
})

grid.setGroup({ by: ['dept'], subtotal: 'bottom' })   // 更改
grid.setGroup(null)                                   // 取消
  • 分组按值排序,空值分组在最后。把分组列降序排序,分组也按降序排列。
  • 分组内的行按当前排序排列。只对符合筛选的行分组。
  • 分组行在第一列显示展开按钮、值和行数。

汇总 · 小计

给列设置 aggregate,就会为每个分组计算该值。

aggregate值
'sum' · 'avg' · 'min' · 'max'只计入能读作数字的值('1,000' 也算数字)。一个都没有则为空
'count'分组内的行数
(rows) => 值自定义计算。接收分组内的行(筛选后)
  • 汇总默认显示在分组标题行。设为 subtotal: 'bottom' 时,展开的分组下方会出现小计行,折叠的分组则显示在标题行。
  • 修改单元格的值后,汇总和分组位置会立即重新计算。
  • 汇总单元格的文字遵循列的 format · type · decimals。format 还会收到 cell.group(分组信息)。

展开 · 折叠

操作结果
点击 ▸ 按钮展开或折叠该分组
在分组行按 Space相同
grid.toggleRow(row, open?)分组行或树的父行。不传 open 则切换
grid.expandAll() · grid.collapseAll()全部展开 · 全部折叠

要一开始就折叠,使用 group: { by: [...], expanded: false }。

处理分组行

分组行和小计行由表格生成,不能编辑,并且不参与选择、复制、Excel 导出和状态栏计算。

  • getViewRows() 只返回可见的数据行(折叠分组中的行不计)。行号以及 scrollToRow(index)、insertRows(rows, index) 的位置也按数据行计算。
  • 用 isGroupRow(row) 和 grid.getGroupInfo(row) 判断是否为分组行并取得其信息(level · column · value · rows · values · open · subtotal)。在 onRowClick 等接收行的地方使用。
  • 服务器数据(dataSource)不使用分组和树。

树

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

const rows = [["1",null,"资产",0],["11","1","流动资产",0],["111","11","现金",1200000],["112","11","银行存款",35400000],["113","11","应收账款",18750000],["12","1","非流动资产",0],["121","12","房屋",250000000],["122","12","办公设备",8300000],["2",null,"负债",0],["21","2","应付账款",12600000],["22","2","其他应付款",4100000],["3",null,"所有者权益",0],["31","3","实收资本",100000000]].map(([code, parent, name, balance]) => ({ code, parent, name, balance }))

const grid = createGrid(host, {
  columns: [
    { field: 'name', title: "科目", width: 220 },
    { field: 'code', title: "代码", width: 80, align: 'center' },
    { field: 'balance', title: "余额", type: 'number', width: 150 }
  ],
  rows,
  tree: { idField: 'code', parentField: 'parent', column: 'name' },
  locale: 'zh'
})

toolbar.innerHTML = `
  <button data-a="expand">全部展开</button>
  <button data-a="collapse">全部折叠</button>
  <button data-a="filter">筛选: 存款</button>
  <button data-a="clear">取消筛选</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([])
}

每行有自己的 id 和父 id 时,就显示为树。行数组按扁平形式传入即可。

createGrid(host, {
  columns: [{ field: 'name', title: '科目' }, { field: 'amount', title: '金额', type: 'number' }],
  rows: [
    { code: '1', parent: null, name: '资产' },
    { code: '11', parent: '1', name: '流动资产' },
    { code: '111', parent: '11', name: '现金' }
  ],
  tree: { idField: 'code', parentField: 'parent', column: 'name' }
})
  • column 指定的列按深度缩进,并带有展开按钮。
  • 找不到父行的行(父 id 为空或不存在)位于最上层。
  • 排序只在同级之间进行。筛选会同时显示匹配的行及其祖先。
  • 展开和折叠与行分组相同(▸ 按钮、toggleRow · expandAll · collapseAll、expanded: false)。如果树列的单元格不可编辑,按 Space 也能切换。

合并相同值

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

const rows = [["北京","朝阳店","1月",2000000],["北京","朝阳店","2月",7000000],["北京","海淀店","1月",5000000],["北京","海淀店","2月",10000000],["上海","浦东店","1月",9000000],["上海","浦东店","2月",5000000]].map(([region, shop, month, sales]) => ({ region, shop, month, sales }))
const columns = merge => [
  { field: 'region', title: "地区", width: 100, merge },
  { field: 'shop', title: "门店", width: 120, merge },
  { field: 'month', title: "月", width: 80, align: 'center' },
  { field: 'sales', title: "销售额", type: 'number', width: 140 }
]

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

toolbar.innerHTML = `
  <button data-a="on">合并</button>
  <button data-a="off">关闭合并</button>`
toolbar.onclick = e => {
  const a = e.target.dataset.a
  if (a) grid.setColumns(columns(a === 'on'))
}
columns: [
  { field: 'dept', title: '部门', merge: true },
  { field: 'team', title: '组', merge: true },
  { field: 'name', title: '姓名' }
]
  • 上下相邻单元格的显示文字相同时画成一个。空值不合并。
  • 左侧合并列的区段一断开,右侧的合并列也随之断开。部门不同时,即使组名相同也分开显示。
  • 排序、筛选、修改值后重新计算。可以与行分组一起使用(在分组行处断开)。
  • 只改变外观。选择、编辑、复制仍按行进行。