Files
Gset/static/js/components/DataTable.js
T
treeandClaude Opus 5.5 f8d1ccc526 初始提交:5G 基站退服时长智能分析与故障闭环管控系统
Flask + SQLite 后端,免构建 React + ECharts 前端,含合成样例数据。
测试参考数据(FOR-TEST-ONLY)与运行时数据库不入库。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 14:01:48 +08:00

81 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {GlassButton, GlassCheckbox, html} from 'vendor';
import {num} from '../format.js';
/**
* 通用表格。
* columns: [{key, title, render?(value, row), sortable?, wrap?}]
* sort: {key, asc},配合 onSort(key) 实现点击表头排序
* selected / onSelect: 传入时显示勾选列(selected 为 Set<id>)
*/
export function DataTable({columns, rows, rowKey = 'id', sort, onSort, selected, onSelect, maxHeight = 560}) {
const selectable = !!onSelect;
// 表头“全选”框的状态:全部选中 / 部分选中(显示横线)/ 未选中
let headerChecked = false;
if (selectable && rows.length > 0) {
const allOn = rows.every(r => selected.has(r[rowKey]));
const someOn = rows.some(r => selected.has(r[rowKey]));
if (allOn) {
headerChecked = true;
} else if (someOn) {
headerChecked = 'mixed';
}
}
const toggleAll = on => onSelect(new Set(on ? rows.map(r => r[rowKey]) : []));
const toggleOne = (id, on) => {
const next = new Set(selected);
on ? next.add(id) : next.delete(id);
onSelect(next);
};
const header = (c, i) => html`
<th key=${c.key + i} aria-sort=${sort?.key === c.key ? (sort.asc ? 'ascending' : 'descending') : undefined}>
${c.sortable && onSort
? html`<button className="th-sort" onClick=${() => onSort(c.key)}>
${c.title}<span aria-hidden="true">${sort?.key === c.key ? (sort.asc ? '↑' : '↓') : '↕'}</span>
</button>`
: c.title}
</th>`;
const row = (r, ri) => html`
<tr key=${r[rowKey] ?? ri}>
${selectable && html`
<td className="check">
<${GlassCheckbox} aria-label="选择" checked=${selected.has(r[rowKey])} onCheckedChange=${on => toggleOne(r[rowKey], on)}/>
</td>`}
${columns.map((c, i) => html`
<td key=${c.key + i} className=${c.wrap ? 'wrap' : undefined}>${c.render ? c.render(r[c.key], r) : r[c.key]}</td>`)}
</tr>`;
return html`
<div className="table-wrap" style=${{maxHeight}}>
<table>
<thead>
<tr>
${selectable && html`
<th className="check">
<${GlassCheckbox} aria-label="全选" checked=${headerChecked} onCheckedChange=${toggleAll}/>
</th>`}
${columns.map(header)}
</tr>
</thead>
<tbody>
${rows.length === 0 && html`<tr><td className="empty" colSpan=${columns.length + (selectable ? 1 : 0)}>暂无数据</td></tr>`}
${rows.map(row)}
</tbody>
</table>
</div>`;
}
/** 分页条 */
export function Pager({page, size, total, onPage, prefix}) {
const pages = Math.max(1, Math.ceil(total / size));
return html`
<div className="pager">
<span>${prefix ?? `共 ${num(total)} 条`} · 第 ${page}/${pages} 页</span>
<${GlassButton} variant="gray" controlSize="small" disabled=${page <= 1} onClick=${() => onPage(page - 1)}>上一页</${GlassButton}>
<${GlassButton} variant="gray" controlSize="small" disabled=${page >= pages} onClick=${() => onPage(page + 1)}>下一页</${GlassButton}>
</div>`;
}