data-page重构评审后两项修补:①自定义页面护栏回填——重构砍掉的旧版最佳实践恰是手写页最需要的底线(骨架三件套/用户数据必转义优先textContent、innerHTML先过esc/SQL参数化+查error字段+加载错误态/大表LIMIT超2000行服务端报error),模板自带这层保护、AI自由发挥的custom.html没有,SKILL.md补为硬要求5-8并把检查清单扩成四项联动;②dashboard count模式去重复卡——metric为count时与固定「总条数」卡同值,只渲两张且省一条冗余COUNT(*) SQL,metric查询按isCount短路为null(渲染分支同守卫);另评审确认:模板/config/base.css三层职责切分成立,类覆盖无悬空,kanban-card改名接线一致,config缺失静默回退DEFAULT属可接受降级

This commit is contained in:
2026-09-24 09:41:20 +08:00
parent 94436ff1f1
commit a2c62717f3
9 changed files with 371 additions and 369 deletions
+46 -93
View File
@@ -4,82 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>数据表格</title>
<style>
:root {
--bg: #f5f5f7; --surface: #fff; --border: #e8e8ed; --text: #1d1d1f;
--text-2: #6e6e73; --accent: #0071e3; --accent-h: #0077ed; --danger: #d70015;
--r: 12px; --r-sm: 8px;
--shadow: 0 1px 3px rgba(0,0,0,0.04);
--shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Helvetica Neue", Arial, sans-serif;
background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5;
-webkit-font-smoothing: antialiased;
padding: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom));
}
.container { max-width: 1200px; margin: 0 auto; }
.card { background: var(--surface); border-radius: var(--r); box-shadow: var(--shadow); overflow: hidden; }
.hd { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.hd h1 { margin: 0; font-size: 16px; font-weight: 600; letter-spacing: -0.01em; }
.btn { appearance: none; border: none; background: var(--accent); color: #fff; font: inherit; font-weight: 500;
padding: 7px 14px; border-radius: 999px; cursor: pointer; transition: background .15s, transform .05s; }
.btn:hover { background: var(--accent-h); }
.btn:active { transform: scale(0.97); }
.tb { padding: 10px 18px; border-bottom: 1px solid var(--border); }
.search { width: 100%; border: 1px solid var(--border); background: var(--bg); border-radius: var(--r-sm);
padding: 8px 12px; font: inherit; outline: none; transition: border-color .15s, background .15s; }
.search:focus { border-color: var(--accent); background: var(--surface); }
.tw { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 18px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--text-2); font-weight: 500; font-size: 13px; cursor: pointer; user-select: none; }
th:hover { color: var(--text); }
th .ar { font-size: 10px; margin-left: 4px; opacity: .7; }
td.ed { cursor: text; }
td.ed:hover { background: rgba(0,113,227,0.04); }
tr:last-child td { border-bottom: none; }
.col-ac { width: 40px; }
.del { appearance: none; background: none; border: none; color: var(--text-2); cursor: pointer;
font-size: 18px; line-height: 1; padding: 2px 8px; border-radius: 6px; }
.del:hover { color: var(--danger); background: rgba(215,0,21,0.08); }
.msg { text-align: center; padding: 56px 20px; color: var(--text-2); }
.msg.err { color: var(--danger); }
.pager { display: flex; align-items: center; justify-content: center; gap: 14px;
padding: 12px; border-top: 1px solid var(--border); font-size: 13px; color: var(--text-2); }
.pager button { appearance: none; border: 1px solid var(--border); background: var(--surface); font: inherit;
padding: 5px 12px; border-radius: 999px; cursor: pointer; }
.pager button:disabled { opacity: .4; cursor: default; }
dialog { border: none; border-radius: var(--r); padding: 0; box-shadow: var(--shadow-lg);
width: min(420px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,0.35); }
.form { padding: 22px; }
.form h3 { margin: 0 0 18px; font-size: 16px; }
.fld { margin-bottom: 14px; }
.fld label { display: block; font-size: 12px; color: var(--text-2); margin-bottom: 4px; }
.fld input { width: 100%; border: 1px solid var(--border); border-radius: var(--r-sm);
padding: 8px 10px; font: inherit; outline: none; transition: border-color .15s; }
.fld input:focus { border-color: var(--accent); }
.acts { display: flex; gap: 8px; justify-content: flex-end; margin-top: 22px; }
.acts button { font: inherit; padding: 8px 16px; border-radius: 999px; cursor: pointer;
border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.acts .pri { background: var(--accent); color: #fff; border-color: var(--accent); }
.acts .pri:hover { background: var(--accent-h); }
@media (max-width: 480px) {
body { padding: 8px; padding-bottom: calc(64px + env(safe-area-inset-bottom)); } th, td { padding: 8px 12px; font-size: 13px; }
}
/* 返回应用 FAB:固定右下、零版面占用(桌面优先方针下 footer 会吃掉长表/看板的垂直空间)。
图标用内联 SVG 不用 ⌂ 字符——WebView 字体不保证有该码位,缺字即变豆腐块。 */
.fab { position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
width: 48px; height: 48px; border-radius: 50%; background: var(--accent); color: #fff;
border: none; box-shadow: 0 4px 12px rgba(0,113,227,0.3); cursor: pointer; z-index: 100;
display: flex; align-items: center; justify-content: center; }
.fab:hover { filter: brightness(1.08); }
.fab:active { transform: scale(0.94); }
body { padding-bottom: calc(72px + env(safe-area-inset-bottom)); }
</style>
<link rel="stylesheet" href="./base.css">
</head>
<body>
<div class="container">
@@ -110,14 +35,20 @@ body { padding-bottom: calc(72px + env(safe-area-inset-bottom)); }
<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 10.5 12 3l9 7.5"/><path d="M5 9.5V21h14V9.5"/></svg>
</button>
<style>
/* 表格页面特有样式 */
.hd { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.hd h1 { margin: 0; font-size: 16px; font-weight: 600; letter-spacing: -0.01em; }
.tb { padding: 10px 18px; border-bottom: 1px solid var(--border); }
</style>
<script src="./config-table.js"></script>
<script>
const CONFIG = {
table: 'items', // ← 修改为你的表名
pk: 'id', // ← 修改为主键列名
title: '数据表格', // ← 修改为页面标题
pageSize: 10,
editable: true,
const DEFAULT = {
table: 'items', pk: 'id', title: '数据表格', pageSize: 10, editable: true,
labels: {}, numCols: [], options: {}, pills: {},
};
const CONFIG = Object.assign({}, DEFAULT, window.PAGE_CONFIG || {});
const API = `/api/db/${location.pathname.split('/').filter(Boolean)[1]}/query`;
const S = { cols: [], rows: [], sortKey: null, sortDir: 'asc', kw: '', page: 1, loading: true, err: null };
@@ -164,6 +95,12 @@ function view() {
return r;
}
function fmt(c, v) {
const pm = CONFIG.pills[c];
if (pm && v != null && pm[v]) return `<span class="pill ${pm[v]}">${esc(v)}</span>`;
return esc(v);
}
function render() {
const root = document.getElementById('root');
if (S.loading) return void (root.innerHTML = '<div class="msg">加载中…</div>');
@@ -178,8 +115,10 @@ function render() {
let h = '<div class="tw"><table><thead><tr>';
for (const c of S.cols) {
const isNum = CONFIG.numCols.includes(c);
const lb = CONFIG.labels[c] || c;
const ar = S.sortKey === c ? (S.sortDir === 'asc' ? '↑' : '↓') : '';
h += `<th data-c="${esc(c)}">${esc(c)}<span class="ar">${ar}</span></th>`;
h += `<th data-c="${esc(c)}"${isNum ? ' class="num"' : ''}>${esc(lb)}<span class="ar">${ar}</span></th>`;
}
if (CONFIG.editable) h += '<th class="col-ac"></th>';
h += '</tr></thead><tbody>';
@@ -187,8 +126,10 @@ function render() {
for (const r of cur) {
h += `<tr data-pk="${esc(r[CONFIG.pk])}">`;
for (const c of S.cols) {
const ed = CONFIG.editable && c !== CONFIG.pk ? ' class="ed"' : '';
h += `<td${ed} data-c="${esc(c)}">${esc(r[c])}</td>`;
const cls = [];
if (CONFIG.editable && c !== CONFIG.pk) cls.push('ed');
if (CONFIG.numCols.includes(c)) cls.push('num');
h += `<td${cls.length ? ` class="${cls.join(' ')}"` : ''} data-c="${esc(c)}">${fmt(c, r[c])}</td>`;
}
if (CONFIG.editable) h += '<td class="col-ac"><button class="del" title="删除">×</button></td>';
h += '</tr>';
@@ -224,9 +165,14 @@ function edit(td) {
if (td.classList.contains('editing')) return;
const tr = td.closest('tr'), col = td.dataset.c, pk = tr.dataset.pk, orig = td.textContent;
td.classList.add('editing');
td.innerHTML = `<input value="${esc(orig)}" style="width:100%;border:1px solid var(--accent);border-radius:4px;padding:2px 6px;font:inherit;outline:none">`;
const opt = CONFIG.options[col];
const style = 'width:100%;border:1px solid var(--accent);border-radius:4px;padding:2px 6px;font:inherit;outline:none;background:var(--surface)';
const ctl = opt
? `<select style="${style}">${opt.map(o => `<option${o === orig ? ' selected' : ''}>${esc(o)}</option>`).join('')}</select>`
: `<input value="${esc(orig)}" type="${CONFIG.numCols.includes(col) ? 'number' : 'text'}" style="${style}">`;
td.innerHTML = ctl;
const inp = td.firstChild;
inp.focus(); inp.select();
inp.focus(); if (inp.select) inp.select();
let done = false;
const finish = async (save) => {
if (done) return; done = true;
@@ -235,7 +181,7 @@ function edit(td) {
try {
await q(`UPDATE ${CONFIG.table} SET ${col} = ? WHERE ${CONFIG.pk} = ?`, [v, pk]);
const r = S.rows.find(x => String(x[CONFIG.pk]) === String(pk));
if (r) r[col] = v;
if (r) r[col] = CONFIG.numCols.includes(col) ? Number(v) : v;
render();
} catch (e) { alert('保存失败:' + e.message); render(); }
};
@@ -262,7 +208,14 @@ document.getElementById('title').textContent = CONFIG.title;
document.getElementById('addBtn').onclick = () => {
const dlg = document.getElementById('dlg'), flds = document.getElementById('fields');
const cols = S.cols.filter(c => c !== CONFIG.pk);
flds.innerHTML = cols.map(c => `<div class="fld"><label>${esc(c)}</label><input name="${esc(c)}" autocomplete="off"></div>`).join('');
flds.innerHTML = cols.map(c => {
const lb = CONFIG.labels[c] || c;
const opt = CONFIG.options[c];
const ctl = opt
? `<select name="${esc(c)}">${opt.map(o => `<option>${esc(o)}</option>`).join('')}</select>`
: `<input name="${esc(c)}" type="${CONFIG.numCols.includes(c) ? 'number' : 'text'}" autocomplete="off">`;
return `<div class="fld"><label>${esc(lb)}</label>${ctl}</div>`;
}).join('');
dlg.showModal();
};
@@ -271,7 +224,10 @@ document.getElementById('dlgForm').onsubmit = async e => {
e.preventDefault();
const form = e.target;
const cols = S.cols.filter(c => c !== CONFIG.pk);
const vals = cols.map(c => form.elements[c].value);
const vals = cols.map(c => {
const v = form.elements[c].value;
return CONFIG.numCols.includes(c) && v !== '' ? Number(v) : v;
});
try {
await q(`INSERT INTO ${CONFIG.table} (${cols.join(',')}) VALUES (${cols.map(() => '?').join(',')})`, vals);
document.getElementById('dlg').close();
@@ -279,9 +235,6 @@ document.getElementById('dlgForm').onsubmit = async e => {
} catch (e) { alert('新增失败:' + e.message); }
};
// 返回应用=回到发起本页的对话:主界面深链 /?s=<会话id>(数据页的 task_id 就是会话 id,
// engine 里 1:1),前端启动时消费一次打开该会话。刻意不用 history.back():多页数据应用里
// back 退的是上一张数据页——那是「上一页」语义,归顶部「返回导航」管。
function backToApp() {
const id = location.pathname.split('/').filter(Boolean)[1];
location.href = id ? '/?s=' + encodeURIComponent(id) : '/';