/** * 表格列宽工具 — 1080P 自适应 * * 用法: * import { autoColumns } from '@/utils/tableColumnWidth' * const columns = autoColumns([ * { type: 'seq' }, * { field: 'name', title: '诊断名称' }, * { field: 'time', title: '诊断时间', fixedWidth: true }, * ]) */ // 已知字段的推荐宽度(可按需扩展) const FIELD_WIDTH_MAP = { // 时间类 — 固定宽度 time: 160, date: 120, datetime: 160, createTime: 160, updateTime: 160, diagnosisTime: 160, registerTime: 160, recordTime: 160, operTime: 160, outcomeDate: 120, startDate: 120, endDate: 120, birthDate: 120, inHosTime: 160, outHosTime: 160, // 编码类 — 固定窄宽 code: 100, ybNo: 100, icdCode: 110, chargeCode: 100, dictCode: 100, typeCode: 100, statusCode: 80, // 名称类 — 弹性宽度 name: 200, title: 200, diagnosisName: 200, deptName: 120, orgName: 140, userName: 100, patientName: 100, doctorName: 100, // 状态/类型 — 固定窄宽 status: 80, type: 80, gender: 60, age: 60, sex: 60, // 操作列 action: 120, operation: 120, } // 中文标题估算宽度(每字约 14px + padding 16px) function estimateWidth(title) { if (!title) return 100 const len = title.length return Math.max(80, Math.min(200, len * 14 + 32)) } /** * 智能设置列宽 * @param {Array} columns - 列配置数组 * @returns {Array} - 处理后的列配置 */ export function autoColumns(columns) { return columns.map(col => { const c = { ...col } // 选择列、序号列、展开列 → 固定宽度 if (c.type === 'selection' || c.type === 'checkbox') { c.width = c.width || 50 return c } if (c.type === 'index' || c.type === 'seq') { c.width = c.width || 60 return c } if (c.type === 'expand') return c // 操作列 → 固定宽度 if (c.field === 'action' || c.field === 'operation' || c.title === '操作') { c.width = c.width || 120 return c } // 已有固定 width 的列不动 if (c.width) return c // 已知字段 → 推荐宽度 if (c.field && FIELD_WIDTH_MAP[c.field]) { const recWidth = FIELD_WIDTH_MAP[c.field] // 时间/编码类用固定 width,名称类用 min-width 自适应 if (recWidth <= 120) { c.width = recWidth } else { c.minWidth = recWidth } return c } // 其他列 → 根据标题估算 min-width c.minWidth = c.minWidth || estimateWidth(c.title) return c }) } /** * 简易版:直接给 vxe-table 列设置合理的默认 min-width * 适合不想改列定义的场景 */ export function getDefaultColumnWidth(title, field) { if (field && FIELD_WIDTH_MAP[field]) { return FIELD_WIDTH_MAP[field] } return estimateWidth(title) }