140 lines
5.1 KiB
JavaScript
Executable File
140 lines
5.1 KiB
JavaScript
Executable File
/**
|
||
* ============================================
|
||
* GX-WMS 通用列表(Grid)组件
|
||
*
|
||
* 功能:
|
||
* 1. 表头固定(上下滚动时不消失)
|
||
* 2. 列宽可拖动(拖动表头右边调整,列与列之间互不影响)
|
||
* 3. 行 hover 高亮
|
||
* 4. 风险标签自动着色(断线/预警/安全)
|
||
* 5. 数字自动格式化(0 灰色、负数红色)
|
||
* 6. 横向滚动自然
|
||
* 7. 自动重新绑定(数据刷新后)
|
||
*
|
||
* 用法:
|
||
* 1. 引入 CSS:<link rel="stylesheet" href="../common/grid.css">
|
||
* 2. 引入 JS:<script src="../common/grid.js"></script>
|
||
* 3. HTML 结构:
|
||
* <div id="myGrid" class="gx-grid">
|
||
* <table>
|
||
* <thead><tr><th style="width:80px">列名</th>...</tr></thead>
|
||
* <tbody id="myTbody"></tbody>
|
||
* </table>
|
||
* </div>
|
||
* 4. JS 渲染后调用:Grid.bind('myGrid');
|
||
* 5. 工具函数:
|
||
* Grid.riskTag('断线') => <span class="tag broken">断线</span>
|
||
* Grid.num(120) => <span class="num">120</span>
|
||
* Grid.num(0) => <span class="num zero">0</span>
|
||
* Grid.num(-5) => <span class="num neg">-5</span>
|
||
* Grid.muted('未填写') => <span class="muted">未填写</span>
|
||
* ============================================
|
||
*/
|
||
|
||
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;
|
||
|
||
// 确保有 <colgroup>,按列设置宽度(改 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 '<span class="tag empty">未判定</span>';
|
||
const cls = ({'断线':'broken','预警':'warn','安全':'safe'})[risk] || 'empty';
|
||
return '<span class="tag ' + cls + '">' + risk + '</span>';
|
||
}
|
||
|
||
/**
|
||
* 数字格式化
|
||
*/
|
||
function num(v) {
|
||
if (v === null || v === undefined || v === '' || v === '-') return '<span class="muted">-</span>';
|
||
const n = Number(v);
|
||
if (isNaN(n)) return v;
|
||
if (n === 0) return '<span class="num zero">0</span>';
|
||
if (n < 0) return '<span class="num neg">' + n + '</span>';
|
||
return '<span class="num">' + n + '</span>';
|
||
}
|
||
|
||
/**
|
||
* 占位符
|
||
*/
|
||
function muted(v, placeholder) {
|
||
if (!v) return '<span class="muted">' + (placeholder || '-') + '</span>';
|
||
return v;
|
||
}
|
||
|
||
return { bind: bind, riskTag: riskTag, num: num, muted: muted };
|
||
})();
|