feat: 启动自愈清理孤儿数据与零引用上传文件,表格列宽记忆优化

This commit is contained in:
lianjianwei2024
2026-10-11 15:13:59 +08:00
parent ca55912dfb
commit ced87bde6f
7 changed files with 136 additions and 13 deletions

View File

@@ -93,7 +93,7 @@ const COLS = {
// 类似清单 / 组价不一致清单 共用(12 列)
// ⚠️ 与后端 BILLROW() 数组下标一一对应:
// 0=id 1=编码 2=名称 3=项目特征 4=单位 5=工程量 6=单价 7=合价 8=序号 9=单位工程名称
bill: [['', 30, 'c', 'tg'], ['序号', 52, 'c'], ['编码', 120, 'c'], ['类别', 52, 'c'],
bill: [['', 44, 'c', 'tg'], ['序号', 52, 'c'], ['编码', 120, 'c'], ['类别', 52, 'c'],
['单位工程名称', 150, 'l'], ['名称', 210, 'l'], ['项目特征', 260, 'l', 'attr'],
['单位', 56, 'c'], ['工程量', 90, 'r'], ['单价', 100, 'r'], ['合价', 110, 'r'],
['备注', 150, 'l']],
@@ -274,14 +274,16 @@ function billSortDOM(tb) {
按**表头文字指纹**为每张表存一份列宽到 localStorage,重建后自动套用。
指纹跨重画稳定:同一套列即使表元素是新建的也得到同一个键;
排序标记 ▲▼ 不参与指纹(同一张表排序前后是同一个键)。 */
function _colk(tb) {
const ths = tb.querySelectorAll('thead th');
function _colkFromTexts(texts) {
let s = '';
ths.forEach(t => { s += (t.textContent || '').trim().replace(/[▲▼]\s*$/, '') + '|'; });
(texts || []).forEach(t => { s += String(t == null ? '' : t).trim().replace(/[▲▼]\s*$/, '') + '|'; });
let h = 0;
for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) & 0x7fffffff;
return 'h' + h.toString(36);
}
function _colk(tb) {
return _colkFromTexts([...tb.querySelectorAll('thead th')].map(t => t.textContent || ''));
}
function _colwKey(tb) { return 'colw|' + location.pathname + '|' + _colk(tb); }
function colwSave(tb) {
const cg = tb.querySelector('colgroup');
@@ -289,7 +291,10 @@ function colwSave(tb) {
const w = Array.prototype.map.call(cg.children, c => parseFloat(c.style.width) || 0);
// 补宽列(项目特征):显示宽是动态算的,不入库,只存基础宽 —— 否则列宽记忆会越滚越大
if (tb._attrCol != null && tb._attrBase != null) w[tb._attrCol] = tb._attrBase;
if (!w.every(x => x > 0)) return; // 含自动宽列的表不记
// 2026-10-11 主人定「全部列宽都记」:混合宽表(名称/规格等 auto 弹性列)原来整表不存,
// 拖了就丢 —— 改为**按列存**:px 列记实宽、弹性列记 0(colwApply/rawTbl 见 0 跳过,
// 弹性列照旧吸收余宽,撑满设计不破)。
if (!w.some(x => x > 0)) return;
try { localStorage.setItem(_colwKey(tb), JSON.stringify(w)); } catch (e) {}
}
function colwApply(tb, deferred) {
@@ -337,10 +342,15 @@ function flexFit(tb, cols) {
const flexIdx = cols.map((c, i) => c[3] === 'F' ? -1 : i).filter(i => i >= 0);
if (!flexIdx.length) return;
const avail = Math.max(360, tb.parentNode.clientWidth - 23);
const wsum = flexIdx.reduce((s, i) => s + cols[i][1], 0);
const rest = avail - fixedSum;
const w = {};
flexIdx.forEach(i => { w[i] = Math.max(90, Math.round(rest * cols[i][1] / wsum)); });
const rat = flexMemLoad(cols); // 用户拖过的比例记忆优先(2026-10-11 主人定)
if (rat) {
flexIdx.forEach((i, k) => { w[i] = Math.max(30, Math.round(rest * rat[k])); });
} else {
const wsum = flexIdx.reduce((s, i) => s + cols[i][1], 0);
flexIdx.forEach(i => { w[i] = Math.max(90, Math.round(rest * cols[i][1] / wsum)); });
}
// 精确贴合:min 钳位/取整的差额补到最大伸缩列,不挤出横向滚动条
const diff = avail - fixedSum - flexIdx.reduce((s, i) => s + w[i], 0);
if (diff !== 0) { const big = flexIdx.reduce((a, b) => w[a] >= w[b] ? a : b); w[big] += diff; }
@@ -362,6 +372,50 @@ function flexRedistribute(tb, idx) {
const rest = Math.max(90 * others.length, avail - fixedSum - cur);
const each = Math.floor(rest / Math.max(1, others.length));
others.forEach(i => cg.children[i].style.width = each + 'px');
flexMemSave(tb); // 拖完存比例记忆(2026-10-11 主人定:撑满表也记住列宽)
}
/* ---- 伸缩表的比例记忆(2026-10-11 主人定:项目工料机这类「撑满」表也要记住列宽)----
绝对宽度与撑满设计冲突(窗口一变就错位),改记**各伸缩列占剩余宽度的比例**:
拖完(flexRedistribute)存比例,重画/换项目/窗口缩放(flexFit)按当前容器宽换算回放。
键按列定义标题指纹(不含项目、不含窗口尺寸)——打开什么项目都是同一份,与 colw 记忆同口径。 */
function _flexKeyOf(cols) {
return 'flexr|' + location.pathname + '|' + _colkFromTexts(cols.map(c => c[0]));
}
function flexMemLoad(cols) {
try {
const r = JSON.parse(localStorage.getItem(_flexKeyOf(cols)) || 'null');
const n = cols.filter(c => c[3] !== 'F').length;
if (Array.isArray(r) && r.length === n && r.every(x => x > 0.005)) return r;
} catch (e) {}
return null;
}
function flexMemSave(tb) {
if (!tb || !tb._cols) return;
const cg = tb.querySelector('colgroup');
if (!cg) return;
const cols = tb._cols;
const flexIdx = cols.map((c, i) => c[3] === 'F' ? -1 : i).filter(i => i >= 0);
if (!flexIdx.length) return;
const ws = flexIdx.map(i => parseFloat(cg.children[i].style.width) || 0);
const tot = ws.reduce((a, b) => a + b, 0);
if (tot <= 0) return;
try { localStorage.setItem(_flexKeyOf(cols), JSON.stringify(ws.map(w => w / tot))); } catch (e) {}
}
/* 按比例回放到现成 colgroup(admin #tb 的内容定宽通道用:有记忆则记忆优先)。
返回是否套上了。 */
function flexMemApply(tb, cols) {
const rat = flexMemLoad(cols);
if (!rat) return false;
const cg = tb.querySelector('colgroup');
if (!cg || cg.children.length !== cols.length) return false;
const host = tb.parentNode;
if (!host || !host.clientWidth) return false;
const fixedSum = cols.reduce((s, c) => s + (c[3] === 'F' ? c[1] : 0), 0);
const flexIdx = cols.map((c, i) => c[3] === 'F' ? -1 : i).filter(i => i >= 0);
const rest = Math.max(360, host.clientWidth - 23) - fixedSum;
flexIdx.forEach((i, k) => { cg.children[i].style.width = Math.max(30, Math.round(rest * rat[k])) + 'px'; });
return true;
}
// 窗口尺寸变化:撑满屏幕的表重新贴合(防抖 200ms;只认 data-flexfit,
// 不碰后台 #tb 那套内容定宽伸缩逻辑)
@@ -695,7 +749,18 @@ let RVOPEN = null, RVROOT = true;
点击落在拖动手柄 .rs 上时不触发排序。 */
function rawTbl(cols, head, body, al, sort) {
const keep = cols.map((w, i) => i).filter(i => cols[i] !== 0);
const cg = keep.map(i => '<col style="width:' + (typeof cols[i] === 'number' ? cols[i] + 'px' : (cols[i] || 'auto')) + '">').join('');
// 列宽记忆回套(2026-10-11 主人定:原数据表拖完也要记住):构建期按表头指纹取记忆宽,
// px 列替换默认宽;记忆里的 0 = 弹性列(见 colwSave 按列存),保持 auto 吸收余宽。
let saved = null;
try { saved = JSON.parse(localStorage.getItem('colw|' + location.pathname + '|' + _colkFromTexts(keep.map(i => Array.isArray(head[i]) ? head[i][0] : head[i]))) || 'null'); } catch (e) {}
if (!saved || saved.length !== keep.length) saved = null;
const cg = keep.map((i, k) => {
let w;
if (saved && saved[k] > 0) w = saved[k] + 'px';
else if (typeof cols[i] === 'number') w = cols[i] + 'px';
else w = cols[i] || 'auto';
return '<col style="width:' + w + '">';
}).join('');
const th = keep.map((i) => {
const h = head[i];
const t = Array.isArray(h) ? h[0] : h;
@@ -710,7 +775,8 @@ function rawTbl(cols, head, body, al, sort) {
}).join('');
let attr = '';
if (keep.every(i => typeof cols[i] === 'number')) {
attr = ' data-sumw="1" style="width:' + keep.reduce((s, i) => s + cols[i], 0) + 'px"';
const wsum = keep.reduce((s, i, k) => s + (saved && saved[k] > 0 ? saved[k] : cols[i]), 0);
attr = ' data-sumw="1" style="width:' + wsum + 'px"';
}
return '<table' + attr + '><colgroup>' + cg + '</colgroup><thead><tr>' + th + '</tr></thead><tbody>' + body + '</tbody></table>';
}