feat(P4): precompute移植+app.js sources双向回填+回归1003/1003+验收体积/文件/data.json
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Aurora Admin · TreeTable (H5)</title>
|
||||
<link rel="stylesheet" href="../.design_library/aurora-admin/colors_and_type.css">
|
||||
<style>
|
||||
/* 对齐 组件7.txt TreeTable 规范:层级缩进16px/级、展开箭头、父级汇总、选择联动 */
|
||||
.demo { padding: 24px; background: var(--au-color-page-bg); color: var(--au-color-text-body); font-family: var(--font-family); }
|
||||
h1 { font-size: 20px; margin: 0 0 4px; }
|
||||
.sub { color: var(--au-color-text-secondary); margin: 0 0 24px; font-size: 12px; }
|
||||
|
||||
.aa-tt { width: 100%; border: 1px solid var(--au-color-border); border-radius: 8px; overflow: hidden; background: var(--au-color-card-bg); }
|
||||
.aa-tt table { width: 100%; border-collapse: collapse; font-size: 14px; color: var(--au-color-text-body); }
|
||||
.aa-tt thead th { background: var(--au-color-table-header-bg); color: var(--au-color-text-body); font-weight: 600; height: 48px; text-align: left; padding: 0 16px; border-bottom: 1px solid var(--au-color-border); white-space: nowrap; }
|
||||
.aa-tt tbody td { height: 48px; padding: 0 16px; border-bottom: 1px solid var(--au-color-border); text-align: left; }
|
||||
.aa-tt tbody tr:last-child td { border-bottom: none; }
|
||||
.aa-tt tbody tr:hover { background: var(--au-color-page-bg); }
|
||||
.aa-tt tbody tr.is-selected { background: var(--au-color-brand-bg); }
|
||||
.aa-tt .align-right { text-align: right; }
|
||||
.aa-tt .tree-cell { display: flex; align-items: center; gap: 4px; }
|
||||
.aa-tt .tt-arrow { width: 16px; height: 16px; font-size: 12px; color: var(--au-color-text-secondary); transition: transform .15s; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; }
|
||||
.aa-tt .tt-arrow.is-open { transform: rotate(90deg); }
|
||||
.aa-tt .tt-arrow-placeholder { width: 16px; flex-shrink: 0; }
|
||||
.aa-tt .tt-check { width: 14px; height: 14px; cursor: pointer; }
|
||||
.aa-tt .tt-summary { color: var(--au-color-text-secondary); font-size: 12px; margin-left: 6px; }
|
||||
.aa-tt tfoot td { height: 44px; padding: 0 16px; background: var(--au-color-table-header-bg); font-weight: 600; border-top: 1px solid var(--au-color-border); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="demo">
|
||||
<h1>Aurora Admin · TreeTable 组件(纯 H5)</h1>
|
||||
<p class="sub">对齐 TreeTable 规范:层级缩进16px/级、展开/折叠箭头、父级汇总(子节点合计)、选择父级联动子级</p>
|
||||
|
||||
<div class="aa-tt" role="table">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width:48px"><input type="checkbox" id="ttAll" class="tt-check"></th>
|
||||
<th>部门 / 人员</th>
|
||||
<th class="align-right">人数</th>
|
||||
<th class="align-right">预算(万)</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="ttBody"></tbody>
|
||||
<tfoot><tr><td></td><td>合计</td><td class="align-right" id="sumCount">0</td><td class="align-right" id="sumBudget">0</td></tr></tfoot>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const treeData = [
|
||||
{ key: 'tech', name: '技术中心', count: 0, budget: 0, children: [
|
||||
{ key: 'fe', name: '前端组', count: 12, budget: 80 },
|
||||
{ key: 'be', name: '后端组', count: 18, budget: 120 }
|
||||
]},
|
||||
{ key: 'product', name: '产品中心', count: 0, budget: 0, children: [
|
||||
{ key: 'pm', name: '产品组', count: 8, budget: 60 },
|
||||
{ key: 'ux', name: '设计组', count: 6, budget: 40 }
|
||||
]}
|
||||
];
|
||||
|
||||
let expanded = new Set(['tech', 'product']);
|
||||
let selected = new Set();
|
||||
|
||||
function hasChildren(n) { return n.children && n.children.length; }
|
||||
function leafSum(node, key) {
|
||||
if (!hasChildren(node)) return node[key] || 0;
|
||||
return node.children.reduce((s, c) => s + leafSum(c, key), 0);
|
||||
}
|
||||
function flatten(nodes, depth, out) {
|
||||
nodes.forEach(n => {
|
||||
out.push({ node: n, depth });
|
||||
if (hasChildren(n) && expanded.has(n.key)) flatten(n.children, depth + 1, out);
|
||||
});
|
||||
return out;
|
||||
}
|
||||
function allChildrenSelected(node) {
|
||||
if (!hasChildren(node)) return selected.has(node.key);
|
||||
return node.children.every(c => allChildrenSelected(c));
|
||||
}
|
||||
function render() {
|
||||
const rows = flatten(treeData, 0, []);
|
||||
document.getElementById('ttBody').innerHTML = rows.map(({ node, depth }) => {
|
||||
const open = expanded.has(node.key);
|
||||
const arrow = hasChildren(node)
|
||||
? `<span class="tt-arrow ${open ? 'is-open' : ''}" role="button" tabindex="0" onclick="toggle('${node.key}')">▸</span>`
|
||||
: `<span class="tt-arrow-placeholder"></span>`;
|
||||
const check = `<input type="checkbox" class="tt-check" ${selected.has(node.key) ? 'checked' : ''} onclick="event.stopPropagation();check('${node.key}')">`;
|
||||
const count = hasChildren(node) ? leafSum(node, 'count') : node.count;
|
||||
const budget = hasChildren(node) ? leafSum(node, 'budget') : node.budget;
|
||||
const sel = selected.has(node.key) ? ' is-selected' : '';
|
||||
return `<tr class="${sel.trim()}" role="row" tabindex="0" aria-selected="${selected.has(node.key)}" onclick="rowClick('${node.key}')" onkeydown="if(event.key==='Enter'||event.key===' '){event.preventDefault();rowClick('${node.key}')}">
|
||||
<td>${check}</td>
|
||||
<td><div class="tree-cell" style="padding-left:${depth * 16}px">${arrow}<span>${node.name}</span></div></td>
|
||||
<td class="align-right">${count}</td>
|
||||
<td class="align-right">${budget}${hasChildren(node) ? '<span class="tt-summary">(汇总)</span>' : ''}</td>
|
||||
</tr>`;
|
||||
}).join('');
|
||||
document.getElementById('sumCount').textContent = treeData.reduce((s, n) => s + leafSum(n, 'count'), 0);
|
||||
document.getElementById('sumBudget').textContent = treeData.reduce((s, n) => s + leafSum(n, 'budget'), 0);
|
||||
}
|
||||
function toggle(key) { expanded.has(key) ? expanded.delete(key) : expanded.add(key); render(); }
|
||||
function rowClick(key) { selected = new Set([key]); render(); }
|
||||
function check(key) {
|
||||
const node = treeData.flatMap(f => [f]).find(n => n.key === key) || find(key, treeData);
|
||||
const willCheck = !selected.has(key);
|
||||
const desc = [key, ...descendantKeys(node)];
|
||||
desc.forEach(k => willCheck ? selected.add(k) : selected.delete(k));
|
||||
propagate(treeData);
|
||||
render();
|
||||
}
|
||||
function find(key, nodes) {
|
||||
for (const n of nodes) { if (n.key === key) return n; if (n.children) { const f = find(key, n.children); if (f) return f; } }
|
||||
}
|
||||
function descendantKeys(node) { let o = []; (node.children||[]).forEach(c => { o.push(c.key); o = o.concat(descendantKeys(c)); }); return o; }
|
||||
function propagate(nodes) {
|
||||
nodes.forEach(n => {
|
||||
if (hasChildren(n)) { allChildrenSelected(n) ? selected.add(n.key) : selected.delete(n.key); propagate(n.children); }
|
||||
});
|
||||
}
|
||||
render();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,148 @@
|
||||
import React, { useState, useMemo, useRef } from 'react';
|
||||
import './TreeTable.css';
|
||||
|
||||
/**
|
||||
* Aurora Admin TreeTable(React)
|
||||
* 对齐 组件7.txt TreeTable 规范:层级缩进16px/级、展开/折叠箭头、父级汇总(叶子合计)、选择父级联动子级
|
||||
* columns: [{ key, title, align? }]
|
||||
* data: 树形行 [{ key, children, ...字段 }]
|
||||
* summaryColumn: 指定数值列,父级与表尾显示叶子求和
|
||||
*/
|
||||
function hasChildren(n) { return !!(n.children && n.children.length); }
|
||||
function leafSum(node, key) {
|
||||
if (!hasChildren(node)) return Number(node[key]) || 0;
|
||||
return node.children.reduce((s, c) => s + leafSum(c, key), 0);
|
||||
}
|
||||
function leafKeys(data, rowKey) {
|
||||
const out = [];
|
||||
const walk = (nodes) => nodes.forEach(n => { if (hasChildren(n)) walk(n.children); else out.push(n[rowKey]); });
|
||||
walk(data);
|
||||
return out;
|
||||
}
|
||||
function descendantKeys(node, rowKey) { let o = []; (node.children || []).forEach(c => { o.push(c[rowKey]); o = o.concat(descendantKeys(c, rowKey)); }); return o; }
|
||||
function allChildrenSelected(node, set, rowKey) { if (!hasChildren(node)) return set.has(node[rowKey]); return node.children.every(c => allChildrenSelected(c, set, rowKey)); }
|
||||
|
||||
export default function TreeTable({
|
||||
columns = [],
|
||||
data = [],
|
||||
rowKey = 'key',
|
||||
selectable = false,
|
||||
selectedKeys = [],
|
||||
defaultExpandedKeys = [],
|
||||
summaryColumn = '',
|
||||
summary = { label: '合计' },
|
||||
onSelectionChange
|
||||
}) {
|
||||
const [expanded, setExpanded] = useState(() => new Set(defaultExpandedKeys));
|
||||
const [selected, setSelected] = useState(() => new Set(selectedKeys));
|
||||
|
||||
const visibleList = useMemo(() => {
|
||||
const out = [];
|
||||
const walk = (nodes, depth) => nodes.forEach(n => {
|
||||
out.push({ node: n, depth });
|
||||
if (hasChildren(n) && expanded.has(n[rowKey])) walk(n.children, depth + 1);
|
||||
});
|
||||
walk(data, 0);
|
||||
return out;
|
||||
}, [data, expanded, rowKey]);
|
||||
|
||||
const totalOf = (key) => data.reduce((s, n) => s + leafSum(n, key), 0);
|
||||
const allSelected = data.length > 0 && leafKeys(data, rowKey).every(k => selected.has(k));
|
||||
const leaves = leafKeys(data, rowKey);
|
||||
const nSel = leaves.filter(k => selected.has(k)).length;
|
||||
const someSelected = nSel > 0 && nSel < leaves.length;
|
||||
|
||||
const toggleExpand = (key) => setExpanded(prev => { const n = new Set(prev); n.has(key) ? n.delete(key) : n.add(key); return n; });
|
||||
|
||||
const toggleCheck = (node) => {
|
||||
const key = node[rowKey];
|
||||
const willCheck = !selected.has(key);
|
||||
const keys = [key, ...descendantKeys(node, rowKey)];
|
||||
const next = new Set(selected);
|
||||
keys.forEach(k => willCheck ? next.add(k) : next.delete(k));
|
||||
const propagate = (nodes) => nodes.forEach(n => {
|
||||
if (hasChildren(n)) { allChildrenSelected(n, next, rowKey) ? next.add(n[rowKey]) : next.delete(n[rowKey]); propagate(n.children); }
|
||||
});
|
||||
propagate(data);
|
||||
setSelected(next);
|
||||
onSelectionChange && onSelectionChange([...next]);
|
||||
};
|
||||
|
||||
const toggleAll = () => {
|
||||
const next = allSelected ? [] : leafKeys(data, rowKey);
|
||||
setSelected(new Set(next));
|
||||
onSelectionChange && onSelectionChange(next);
|
||||
};
|
||||
|
||||
const onRowClick = (node) => { setSelected(new Set([node[rowKey]])); onSelectionChange && onSelectionChange([node[rowKey]]); };
|
||||
|
||||
const cellValue = (node, key) => (hasChildren(node) && key === summaryColumn ? leafSum(node, key) : node[key]);
|
||||
|
||||
return (
|
||||
<div className="aa-tt">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
{selectable && <th className="col-selection" style={{ width: 48 }}>
|
||||
<input type="checkbox" className="tt-check" checked={allSelected}
|
||||
ref={el => el && (el.indeterminate = someSelected)} onChange={toggleAll} />
|
||||
</th>}
|
||||
{columns.map(col => (
|
||||
<th key={col.key} className={col.align === 'right' ? 'align-right' : ''}>{col.title}</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{visibleList.map(({ node, depth }) => (
|
||||
<tr key={node[rowKey]} className={selected.has(node[rowKey]) ? 'is-selected' : ''} onClick={() => onRowClick(node)}>
|
||||
{selectable && (
|
||||
<td className="col-selection">
|
||||
<input type="checkbox" className="tt-check" checked={selected.has(node[rowKey])}
|
||||
onClick={e => e.stopPropagation()} onChange={() => toggleCheck(node)} />
|
||||
</td>
|
||||
)}
|
||||
{columns.map(col => (
|
||||
<td key={col.key} className={col.align === 'right' ? 'align-right' : ''}>
|
||||
{col.key === columns[0].key ? (
|
||||
<div className="tree-cell" style={{ paddingLeft: depth * 16 }}>
|
||||
{hasChildren(node) ? (
|
||||
<span className={`tt-arrow${expanded.has(node[rowKey]) ? ' is-open' : ''}`} onClick={e => { e.stopPropagation(); toggleExpand(node[rowKey]); }}>▸</span>
|
||||
) : <span className="tt-arrow-placeholder" />}
|
||||
<span>{node[col.key]}</span>
|
||||
{hasChildren(node) && summaryColumn && <span className="tt-summary">(汇总)</span>}
|
||||
</div>
|
||||
) : cellValue(node, col.key)}
|
||||
</td>
|
||||
))}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
{summaryColumn && (
|
||||
<tfoot>
|
||||
<tr>
|
||||
{selectable && <td />}
|
||||
<td>{summary.label || '合计'}</td>
|
||||
{columns.slice(1).map(col => (
|
||||
<td key={col.key} className={col.align === 'right' ? 'align-right' : ''}>
|
||||
{col.key === summaryColumn ? totalOf(col.key) : ''}
|
||||
</td>
|
||||
))}
|
||||
</tr>
|
||||
</tfoot>
|
||||
)}
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
用法示例:
|
||||
import TreeTable from './TreeTable';
|
||||
const columns = [
|
||||
{ key: 'name', title: '部门/人员' },
|
||||
{ key: 'count', title: '人数', align: 'right' },
|
||||
{ key: 'budget', title: '预算(万)', align: 'right' }
|
||||
];
|
||||
const data = [{ key: 'tech', name: '技术中心', children: [{ key: 'fe', name: '前端组', count: 12, budget: 80 }] }];
|
||||
<TreeTable columns={columns} data={data} selectable summaryColumn="budget" defaultExpandedKeys={['tech']} onSelectionChange={setSel} />
|
||||
*/
|
||||
@@ -0,0 +1,161 @@
|
||||
<template>
|
||||
<div class="aa-tt">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th v-if="selectable" class="col-selection" style="width:48px">
|
||||
<input type="checkbox" class="tt-check" :checked="allSelected" v-indeterminate="someSelected" @change="toggleAll">
|
||||
</th>
|
||||
<th
|
||||
v-for="col in columns" :key="col.key"
|
||||
:class="{ 'align-right': col.align === 'right' }"
|
||||
>{{ col.title }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr
|
||||
v-for="row in visibleList" :key="row.node[rowKey]"
|
||||
:class="{ 'is-selected': isSelected(row.node[rowKey]) }"
|
||||
@click="onRowClick(row.node)"
|
||||
>
|
||||
<td v-if="selectable" class="col-selection">
|
||||
<input type="checkbox" class="tt-check" :checked="isSelected(row.node[rowKey])" @click.stop="toggleCheck(row.node)">
|
||||
</td>
|
||||
<td
|
||||
v-for="col in columns" :key="col.key"
|
||||
:class="{ 'align-right': col.align === 'right' }"
|
||||
>
|
||||
<div v-if="col.key === columns[0].key" class="tree-cell" :style="{ paddingLeft: row.depth * 16 + 'px' }">
|
||||
<span
|
||||
v-if="hasChildren(row.node)"
|
||||
class="tt-arrow" :class="{ 'is-open': isExpanded(row.node[rowKey]) }"
|
||||
@click.stop="toggleExpand(row.node[rowKey])"
|
||||
>▸</span>
|
||||
<span v-else class="tt-arrow-placeholder"></span>
|
||||
<span>{{ row.node[col.key] }}</span>
|
||||
<span v-if="hasChildren(row.node) && summaryColumn" class="tt-summary">(汇总)</span>
|
||||
</div>
|
||||
<template v-else>
|
||||
{{ cellValue(row.node, col.key) }}
|
||||
</template>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
<tfoot v-if="showSummary">
|
||||
<tr>
|
||||
<td v-if="selectable"></td>
|
||||
<td>{{ summary.label || '合计' }}</td>
|
||||
<td v-for="col in columns.slice(1)" :key="col.key" :class="{ 'align-right': col.align === 'right' }">
|
||||
{{ col.key === summaryColumn ? totalOf(col.key) : '' }}
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'AaTreeTable',
|
||||
directives: { indeterminate(el, b) { el.indeterminate = !!b.value; } },
|
||||
props: {
|
||||
columns: { type: Array, required: true }, // [{ key, title, align }]
|
||||
data: { type: Array, required: true }, // 树形行 [{ key, children, ... }]
|
||||
rowKey: { type: String, default: 'key' },
|
||||
selectable: { type: Boolean, default: false },
|
||||
selectedKeys: { type: Array, default: () => [] },
|
||||
defaultExpandedKeys: { type: Array, default: () => [] },
|
||||
summaryColumn: { type: String, default: '' }, // 对该数值列按叶子求和并在父级显示
|
||||
summary: { type: Object, default: () => ({ label: '合计' }) }
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
expanded: new Set(this.defaultExpandedKeys),
|
||||
selected: new Set(this.selectedKeys)
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
showSummary() { return !!this.summaryColumn; },
|
||||
visibleList() {
|
||||
const out = [];
|
||||
const walk = (nodes, depth) => {
|
||||
nodes.forEach(n => {
|
||||
out.push({ node: n, depth });
|
||||
if (this.hasChildren(n) && this.expanded.has(n[this.rowKey])) walk(n.children, depth + 1);
|
||||
});
|
||||
};
|
||||
walk(this.data, 0);
|
||||
return out;
|
||||
},
|
||||
allSelected() { return this.data.length > 0 && this.leafKeys().every(k => this.selected.has(k)); },
|
||||
someSelected() {
|
||||
const leaves = this.leafKeys();
|
||||
const n = leaves.filter(k => this.selected.has(k)).length;
|
||||
return n > 0 && n < leaves.length;
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
hasChildren(n) { return n.children && n.children.length; },
|
||||
leafSum(node, key) {
|
||||
if (!this.hasChildren(node)) return Number(node[key]) || 0;
|
||||
return node.children.reduce((s, c) => s + this.leafSum(c, key), 0);
|
||||
},
|
||||
leafKeys() {
|
||||
const out = [];
|
||||
const walk = (nodes) => nodes.forEach(n => {
|
||||
if (this.hasChildren(n)) walk(n.children); else out.push(n[this.rowKey]);
|
||||
});
|
||||
walk(this.data);
|
||||
return out;
|
||||
},
|
||||
totalOf(key) { return this.data.reduce((s, n) => s + this.leafSum(n, key), 0); },
|
||||
isExpanded(k) { return this.expanded.has(k); },
|
||||
isSelected(k) { return this.selected.has(k); },
|
||||
cellValue(node, key) {
|
||||
if (this.hasChildren(node) && key === this.summaryColumn) return this.leafSum(node, key);
|
||||
return node[key];
|
||||
},
|
||||
toggleExpand(key) { this.expanded.has(key) ? this.expanded.delete(key) : this.expanded.add(key); this.expanded = new Set(this.expanded); },
|
||||
onRowClick(node) { this.selected = new Set([node[this.rowKey]]); this.$emit('update:selectedKeys', [node[this.rowKey]]); this.$emit('row-click', node); },
|
||||
descendantKeys(node) { let o = []; (node.children || []).forEach(c => { o.push(c[this.rowKey]); o = o.concat(this.descendantKeys(c)); }); return o; },
|
||||
allChildrenSelected(node) { if (!this.hasChildren(node)) return this.selected.has(node[this.rowKey]); return node.children.every(c => this.allChildrenSelected(c)); },
|
||||
toggleCheck(node) {
|
||||
const key = node[this.rowKey];
|
||||
const willCheck = !this.selected.has(key);
|
||||
const keys = [key, ...this.descendantKeys(node)];
|
||||
keys.forEach(k => willCheck ? this.selected.add(k) : this.selected.delete(k));
|
||||
const propagate = (nodes) => nodes.forEach(n => {
|
||||
if (this.hasChildren(n)) { this.allChildrenSelected(n) ? this.selected.add(n[this.rowKey]) : this.selected.delete(n[this.rowKey]); propagate(n.children); }
|
||||
});
|
||||
propagate(this.data);
|
||||
this.selected = new Set(this.selected);
|
||||
this.$emit('update:selectedKeys', [...this.selected]);
|
||||
},
|
||||
toggleAll() {
|
||||
const next = this.allSelected ? [] : this.leafKeys();
|
||||
this.selected = new Set(next);
|
||||
this.$emit('update:selectedKeys', next);
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<!-- 样式对齐 组件7.txt TreeTable 规范 -->
|
||||
<style>
|
||||
.aa-tt { width: 100%; border: 1px solid #E8ECF1; border-radius: 8px; overflow: hidden; background: #fff; }
|
||||
.aa-tt table { width: 100%; border-collapse: collapse; font-size: 14px; color: #262626; }
|
||||
.aa-tt thead th { background: #FAFAFA; color: #262626; font-weight: 600; height: 48px; text-align: left; padding: 0 16px; border-bottom: 1px solid #E8ECF1; white-space: nowrap; }
|
||||
.aa-tt tbody td { height: 48px; padding: 0 16px; border-bottom: 1px solid #E8ECF1; text-align: left; }
|
||||
.aa-tt tbody tr:last-child td { border-bottom: none; }
|
||||
.aa-tt tbody tr:hover { background: #F5F7FA; }
|
||||
.aa-tt tbody tr.is-selected { background: #F0F5FF; }
|
||||
.aa-tt .align-right { text-align: right; }
|
||||
.aa-tt .col-selection { text-align: center; }
|
||||
.aa-tt .tree-cell { display: flex; align-items: center; gap: 4px; }
|
||||
.aa-tt .tt-arrow { width: 16px; height: 16px; font-size: 12px; color: #6E6E6E; transition: transform .15s; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; }
|
||||
.aa-tt .tt-arrow.is-open { transform: rotate(90deg); }
|
||||
.aa-tt .tt-arrow-placeholder { width: 16px; flex-shrink: 0; }
|
||||
.aa-tt .tt-check { width: 14px; height: 14px; cursor: pointer; }
|
||||
.aa-tt .tt-summary { color: #6E6E6E; font-size: 12px; margin-left: 6px; }
|
||||
.aa-tt tfoot td { height: 44px; padding: 0 16px; background: #FAFAFA; font-weight: 600; border-top: 1px solid #E8ECF1; }
|
||||
</style>
|
||||
@@ -0,0 +1,145 @@
|
||||
<template>
|
||||
<div class="aa-tt">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th v-if="selectable" class="col-selection" style="width:48px">
|
||||
<input type="checkbox" class="tt-check" :checked="allSelected" :indeterminate.prop="someSelected" @change="toggleAll">
|
||||
</th>
|
||||
<th v-for="col in columns" :key="col.key" :class="{ 'align-right': col.align === 'right' }">{{ col.title }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr
|
||||
v-for="row in visibleList" :key="row.node[rowKey]"
|
||||
:class="{ 'is-selected': isSelected(row.node[rowKey]) }"
|
||||
@click="onRowClick(row.node)"
|
||||
>
|
||||
<td v-if="selectable" class="col-selection">
|
||||
<input type="checkbox" class="tt-check" :checked="isSelected(row.node[rowKey])" @click.stop="toggleCheck(row.node)">
|
||||
</td>
|
||||
<td
|
||||
v-for="col in columns" :key="col.key"
|
||||
:class="{ 'align-right': col.align === 'right' }"
|
||||
>
|
||||
<div v-if="col.key === columns[0].key" class="tree-cell" :style="{ paddingLeft: row.depth * 16 + 'px' }">
|
||||
<span
|
||||
v-if="hasChildren(row.node)"
|
||||
class="tt-arrow" :class="{ 'is-open': isExpanded(row.node[rowKey]) }"
|
||||
@click.stop="toggleExpand(row.node[rowKey])"
|
||||
>▸</span>
|
||||
<span v-else class="tt-arrow-placeholder"></span>
|
||||
<span>{{ row.node[col.key] }}</span>
|
||||
<span v-if="hasChildren(row.node) && summaryColumn" class="tt-summary">(汇总)</span>
|
||||
</div>
|
||||
<template v-else>{{ cellValue(row.node, col.key) }}</template>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
<tfoot v-if="showSummary">
|
||||
<tr>
|
||||
<td v-if="selectable"></td>
|
||||
<td>{{ summary.label || '合计' }}</td>
|
||||
<td v-for="col in columns.slice(1)" :key="col.key" :class="{ 'align-right': col.align === 'right' }">
|
||||
{{ col.key === summaryColumn ? totalOf(col.key) : '' }}
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed, reactive } from 'vue';
|
||||
|
||||
const props = defineProps({
|
||||
columns: { type: Array, required: true }, // [{ key, title, align }]
|
||||
data: { type: Array, required: true }, // 树形行 [{ key, children, ... }]
|
||||
rowKey: { type: String, default: 'key' },
|
||||
selectable: { type: Boolean, default: false },
|
||||
selectedKeys: { type: Array, default: () => [] },
|
||||
defaultExpandedKeys: { type: Array, default: () => [] },
|
||||
summaryColumn: { type: String, default: '' },
|
||||
summary: { type: Object, default: () => ({ label: '合计' }) }
|
||||
});
|
||||
const emit = defineEmits(['update:selectedKeys', 'row-click']);
|
||||
|
||||
const state = reactive({
|
||||
expanded: new Set(props.defaultExpandedKeys),
|
||||
selected: new Set(props.selectedKeys)
|
||||
});
|
||||
|
||||
const hasChildren = (n) => !!(n.children && n.children.length);
|
||||
function leafSum(node, key) {
|
||||
if (!hasChildren(node)) return Number(node[key]) || 0;
|
||||
return node.children.reduce((s, c) => s + leafSum(c, key), 0);
|
||||
}
|
||||
function leafKeys() {
|
||||
const out = [];
|
||||
const walk = (nodes) => nodes.forEach(n => { if (hasChildren(n)) walk(n.children); else out.push(n[props.rowKey]); });
|
||||
walk(props.data);
|
||||
return out;
|
||||
}
|
||||
function descendantKeys(node) { let o = []; (node.children || []).forEach(c => { o.push(c[props.rowKey]); o = o.concat(descendantKeys(c)); }); return o; }
|
||||
function allChildrenSelected(node) { if (!hasChildren(node)) return state.selected.has(node[props.rowKey]); return node.children.every(c => allChildrenSelected(c)); }
|
||||
|
||||
const showSummary = computed(() => !!props.summaryColumn);
|
||||
const visibleList = computed(() => {
|
||||
const out = [];
|
||||
const walk = (nodes, depth) => nodes.forEach(n => {
|
||||
out.push({ node: n, depth });
|
||||
if (hasChildren(n) && state.expanded.has(n[props.rowKey])) walk(n.children, depth + 1);
|
||||
});
|
||||
walk(props.data, 0);
|
||||
return out;
|
||||
});
|
||||
const allSelected = computed(() => props.data.length > 0 && leafKeys().every(k => state.selected.has(k)));
|
||||
const someSelected = computed(() => { const l = leafKeys(); const n = l.filter(k => state.selected.has(k)).length; return n > 0 && n < l.length; });
|
||||
|
||||
function isExpanded(k) { return state.expanded.has(k); }
|
||||
function isSelected(k) { return state.selected.has(k); }
|
||||
function totalOf(key) { return props.data.reduce((s, n) => s + leafSum(n, key), 0); }
|
||||
function cellValue(node, key) {
|
||||
if (hasChildren(node) && key === props.summaryColumn) return leafSum(node, key);
|
||||
return node[key];
|
||||
}
|
||||
function toggleExpand(key) { state.expanded.has(key) ? state.expanded.delete(key) : state.expanded.add(key); state.expanded = new Set(state.expanded); }
|
||||
function onRowClick(node) { state.selected = new Set([node[props.rowKey]]); emit('update:selectedKeys', [node[props.rowKey]]); emit('row-click', node); }
|
||||
function toggleCheck(node) {
|
||||
const key = node[props.rowKey];
|
||||
const willCheck = !state.selected.has(key);
|
||||
const keys = [key, ...descendantKeys(node)];
|
||||
keys.forEach(k => willCheck ? state.selected.add(k) : state.selected.delete(k));
|
||||
const propagate = (nodes) => nodes.forEach(n => {
|
||||
if (hasChildren(n)) { allChildrenSelected(n) ? state.selected.add(n[props.rowKey]) : state.selected.delete(n[props.rowKey]); propagate(n.children); }
|
||||
});
|
||||
propagate(props.data);
|
||||
state.selected = new Set(state.selected);
|
||||
emit('update:selectedKeys', [...state.selected]);
|
||||
}
|
||||
function toggleAll() {
|
||||
const next = allSelected.value ? [] : leafKeys();
|
||||
state.selected = new Set(next);
|
||||
emit('update:selectedKeys', next);
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- 样式对齐 组件7.txt TreeTable 规范 -->
|
||||
<style scoped>
|
||||
.aa-tt { width: 100%; border: 1px solid #E8ECF1; border-radius: 8px; overflow: hidden; background: #fff; }
|
||||
.aa-tt table { width: 100%; border-collapse: collapse; font-size: 14px; color: #262626; }
|
||||
.aa-tt thead th { background: #FAFAFA; color: #262626; font-weight: 600; height: 48px; text-align: left; padding: 0 16px; border-bottom: 1px solid #E8ECF1; white-space: nowrap; }
|
||||
.aa-tt tbody td { height: 48px; padding: 0 16px; border-bottom: 1px solid #E8ECF1; text-align: left; }
|
||||
.aa-tt tbody tr:last-child td { border-bottom: none; }
|
||||
.aa-tt tbody tr:hover { background: #F5F7FA; }
|
||||
.aa-tt tbody tr.is-selected { background: #F0F5FF; }
|
||||
.aa-tt .align-right { text-align: right; }
|
||||
.aa-tt .col-selection { text-align: center; }
|
||||
.aa-tt .tree-cell { display: flex; align-items: center; gap: 4px; }
|
||||
.aa-tt .tt-arrow { width: 16px; height: 16px; font-size: 12px; color: #6E6E6E; transition: transform .15s; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; }
|
||||
.aa-tt .tt-arrow.is-open { transform: rotate(90deg); }
|
||||
.aa-tt .tt-arrow-placeholder { width: 16px; flex-shrink: 0; }
|
||||
.aa-tt .tt-check { width: 14px; height: 14px; cursor: pointer; }
|
||||
.aa-tt .tt-summary { color: #6E6E6E; font-size: 12px; margin-left: 6px; }
|
||||
.aa-tt tfoot td { height: 44px; padding: 0 16px; background: #FAFAFA; font-weight: 600; border-top: 1px solid #E8ECF1; }
|
||||
</style>
|
||||
Reference in New Issue
Block a user