外观定制
默认外观接近 Excel。颜色、字体、大小用 CSS 变量修改,行和单元格的强调用类修改。几乎不需要覆盖表格内部的 CSS。
styling.js
import { createGrid } from 'horee-grid'
const rows = Array.from({ length: 100 }, (_, i) => ({ id: i + 1, item: `物料 ${i + 1}`, stock: (i * 37) % 120, safe: 40 }))
const grid = createGrid(host, {
locale: 'zh',
rowKey: 'id',
rowHeight: 32,
columns: [
{ field: 'item', title: '物料', flex: 1 },
{ field: 'stock', title: '库存', type: 'number', width: 100 },
{ field: 'safe', title: '安全库存', type: 'number', width: 100 }
],
rows,
rowClass: r => (r.stock < r.safe ? 'short' : undefined)
})
const css = document.createElement('style')
css.textContent = `
.hg.theme-ocean { --hg-accent: #0b63ce; --hg-sel: #dbe9ff; --hg-head-bg: #eef3fa; --hg-line: #d5deea; }
.hg.theme-ink { --hg-bg: #111418; --hg-fg: #e5e8eb; --hg-line: #2a2f36; --hg-head-bg: #191d22; --hg-head-fg: #aab2bb;
--hg-sel: #273244; --hg-hover: #1a2029; --hg-accent: #f0b429; }
.hg-row.short .hg-cell { color: #d1242f; }`
document.head.append(css)
toolbar.innerHTML = `
<button data-theme="">默认</button><button data-theme="theme-ocean">ocean</button><button data-theme="theme-ink">ink</button>
<button data-lines="all">边框 all</button><button data-lines="row">边框 row</button><button data-lines="none">边框 none</button>`
toolbar.onclick = e => {
const el = host.querySelector('.hg')
const { theme, lines } = e.target.dataset
if (theme !== undefined) {
el.classList.remove('theme-ocean', 'theme-ink')
if (theme) el.classList.add(theme)
}
if (lines) {
// 与 lines 选项添加的类相同(通常在创建时传入 lines: 'row')
el.classList.remove('hg-lines-row', 'hg-lines-none')
if (lines !== 'all') el.classList.add('hg-lines-' + lines)
}
}CSS 变量
设置在表格元素(.hg)或其上层的任意元素上。整个应用一套、每个页面不同、只针对一个表格——方法都一样。
:root {
--hg-font: "Noto Sans SC", sans-serif;
--hg-font-size: 13px;
--hg-accent: #0b63ce;
}
.dark .hg {
--hg-bg: #111418;
--hg-fg: #e5e8eb;
--hg-line: #2a2f36;
--hg-head-bg: #191d22;
}| 变量 | 用途 | 默认值 |
|---|---|---|
--hg-font | 字体 | 继承父元素 |
--hg-font-size | 字号 | 13px |
--hg-bg | 单元格背景 | #fff |
--hg-fg | 文字 | #1f2328 |
--hg-line | 单元格线 · 边框 | #d4d4d4 |
--hg-head-bg | 表头 · 行号背景 | #f3f3f3 |
--hg-head-fg | 表头文字 | #333 |
--hg-foot-bg | 底部固定行(合计)背景 | --hg-head-bg |
--hg-sel | 选中行背景 | #e2f0d9 |
--hg-hover | 鼠标悬停的行 | #f5f9ff |
--hg-accent | 活动单元格 · 排序标记 · 勾选 · 拖动标记 | #107c41 |
--hg-dirty | 修改标记 | #e8590c |
--hg-error | 无效值边框 | #d93025 |
--hg-row-h | 行高(由 rowHeight 选项决定) | 27px |
行高用选项设置
--hg-row-h 由表格根据 rowHeight 选项直接设置。它用于滚动计算,请不要用 CSS 修改,而是设置 rowHeight。表头行高为 headerHeight(默认为行高 + 2)。
单元格线
createGrid(host, { columns, rows, lines: 'row' })lines | 样式 |
|---|---|
'all'(默认) | 横线 · 竖线,与 Excel 相同 |
'row' | 只有横线,适合查询列表 |
'none' | 无线 |
行 · 单元格强调
createGrid(host, {
columns,
rows,
rowClass: row => (row.stock < row.safe ? 'short' : undefined)
}).hg-row.short .hg-cell { color: #d1242f; }
.hg-row.short .hg-cell:first-child { font-weight: 600; }单个单元格用列的 cellClass:
{ field: 'diff', title: '差异', type: 'number', cellClass: ({ value }) => (value ? 'warn' : undefined) }背景色设置在单元格上
给 .hg-row 设置背景是看不到的,因为单元格(.hg-cell)有自己的背景。请用 .hg-row.名称 .hg-cell 设置。
表格添加的类
可以在页面 CSS 中依赖的类。除此之外的内部类名可能随版本变化。
| 类 | 位置 |
|---|---|
.hg | 表格外层元素 |
.hg-row · .hg-cell | 行 · 单元格 |
.hg-cell[data-c="列 id"] | 特定列的单元格 |
.hg-sel | 选中的行(.hg-row) |
.hg-cur | 当前行(setCurrent) |
.hg-active | 活动单元格 |
.hg-dirty | 修改过的单元格 |
.hg-foot | 底部固定行区域 |
.hg-loading | 正在加载服务器数据(.hg) |
.hg-lines-row · .hg-lines-none | lines 选项(.hg) |
深色模式
只需修改变量。本文档的示例点击右上角的 ☾ 按钮也会用同样的方法切换。
[data-theme="dark"] .hg {
--hg-bg: #14181c;
--hg-fg: #e6e9ec;
--hg-line: #2a3037;
--hg-head-bg: #1b2026;
--hg-head-fg: #c3cad2;
--hg-sel: #17352a;
--hg-hover: #1a222a;
--hg-accent: #3ccf7f;
}