/** * ============================================ * GX-WMS 通用列表(Grid)组件 * * 功能: * 1. 表头固定(上下滚动时不消失) * 2. 列宽可拖动(拖动表头右边调整,列与列之间互不影响) * 3. 行 hover 高亮 * 4. 风险标签自动着色(断线/预警/安全) * 5. 数字自动格式化(0 灰色、负数红色) * 6. 横向滚动自然 * 7. 自动重新绑定(数据刷新后) * * 用法: * 1. 引入 CSS: * 2. 引入 JS: * 3. HTML 结构: *
* * ... * *
列名
*
* 4. JS 渲染后调用:Grid.bind('myGrid'); * 5. 工具函数: * Grid.riskTag('断线') => 断线 * Grid.num(120) => 120 * Grid.num(0) => 0 * Grid.num(-5) => -5 * Grid.muted('未填写') => 未填写 * ============================================ */ const Grid = (function() { /** * 绑定列宽拖动 + 重新绑定的工具 * @param {string} containerId - 容器元素的 id(含 class="gx-grid") */ function bind(containerId) { const wrap = document.getElementById(containerId); if (!wrap) return; const table = wrap.querySelector('table'); if (!table) return; // 确保有 ,按列设置宽度(改 col 宽度,浏览器自动应用到整列,无需遍历 td,性能好) let colgroup = table.querySelector('colgroup'); if (!colgroup) { colgroup = document.createElement('colgroup'); table.insertBefore(colgroup, table.firstChild); } const theadRow = table.querySelector('thead tr'); const colCount = theadRow ? theadRow.children.length : 0; const cols = []; for (let i = 0; i < colCount; i++) { let col = colgroup.children[i]; if (!col) { col = document.createElement('col'); colgroup.appendChild(col); } cols.push(col); } const ths = wrap.querySelectorAll('thead th'); ths.forEach((th, idx) => { if (th.querySelector('.gx-resizer')) return; // 用 th 当前宽度初始化 col 宽度(保持原列宽不被 colgroup 覆盖) if (cols[idx]) { const w = th.style.width || th.getBoundingClientRect().width + 'px'; cols[idx].style.width = w; } const r = document.createElement('div'); r.className = 'gx-resizer'; th.appendChild(r); r.addEventListener('mousedown', function(e) { e.preventDefault(); e.stopPropagation(); const startX = e.pageX; const startW = th.getBoundingClientRect().width; let raf = null; let pendingW = startW; r.classList.add('active'); const onMove = function(ev) { const dx = ev.pageX - startX; pendingW = Math.max(8, startW + dx); // 最小 8px,允许一直收缩到很小 if (raf) return; // 已有待处理的帧,跳过(该帧会用最新 pendingW) raf = requestAnimationFrame(function() { raf = null; if (cols[idx]) cols[idx].style.width = pendingW + 'px'; th.style.width = pendingW + 'px'; // 同步 th,保证表头与列内容对齐 }); }; const onUp = function() { if (raf) { cancelAnimationFrame(raf); raf = null; } // 最终确保应用一次 if (cols[idx]) cols[idx].style.width = pendingW + 'px'; th.style.width = pendingW + 'px'; r.classList.remove('active'); document.removeEventListener('mousemove', onMove); document.removeEventListener('mouseup', onUp); }; document.addEventListener('mousemove', onMove); document.addEventListener('mouseup', onUp); }); }); } /** * 风险标签 */ function riskTag(risk) { if (!risk) return '未判定'; const cls = ({'断线':'broken','预警':'warn','安全':'safe'})[risk] || 'empty'; return '' + risk + ''; } /** * 数字格式化 */ function num(v) { if (v === null || v === undefined || v === '' || v === '-') return '-'; const n = Number(v); if (isNaN(n)) return v; if (n === 0) return '0'; if (n < 0) return '' + n + ''; return '' + n + ''; } /** * 占位符 */ function muted(v, placeholder) { if (!v) return '' + (placeholder || '-') + ''; return v; } return { bind: bind, riskTag: riskTag, num: num, muted: muted }; })();