From 7f3ed5fe3c1b3e7555576e70e3430863a4556af2 Mon Sep 17 00:00:00 2001 From: tao Date: Fri, 19 Dec 2025 10:07:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=AE=A1=E7=AE=97=E8=A1=A8?= =?UTF-8?q?=E6=A0=BC=E5=88=97=E5=AE=BD=E6=96=B9=E6=B3=95=20=E8=A1=A8?= =?UTF-8?q?=E6=A0=BC=E6=94=B9=E4=B8=BA=E5=88=97=E5=AE=BD=E8=87=AA=E9=80=82?= =?UTF-8?q?=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/table.ts | 32 +++++++++++++++++++++++++ src/views/Line1/L1-data-list/index.vue | 2 +- src/views/Line1/alarm-records/index.vue | 2 +- src/views/Line2/L1-data-list/index.vue | 2 +- src/views/Line2/L4-data-list/index.vue | 2 +- 5 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/utils/table.ts diff --git a/src/utils/table.ts b/src/utils/table.ts new file mode 100644 index 0000000..03b16c9 --- /dev/null +++ b/src/utils/table.ts @@ -0,0 +1,32 @@ +/** + * 计算字符串“字数” + * 中文 / 全角字符:1 + * 英文 / 数字 / 半角符号:0.5 + */ +export function calcTextLength(text: string): number { + let length = 0; + + for (const char of text) { + // charCode > 255 基本可以认为是中文或全角字符 + if (char.charCodeAt(0) > 255) { + length += 1; + } else { + length += 0.5; + } + } + + return length; +} + +/** + * 根据文本计算列宽 + */ +export function calcColumnWidth( + text: string, + unitWidth = 24, + minWidth = 50, + maxWidth = 150 +) { + const width = calcTextLength(text) * unitWidth; + return Math.min(Math.max(width, minWidth), maxWidth); +} diff --git a/src/views/Line1/L1-data-list/index.vue b/src/views/Line1/L1-data-list/index.vue index cdea6c2..ee6989d 100644 --- a/src/views/Line1/L1-data-list/index.vue +++ b/src/views/Line1/L1-data-list/index.vue @@ -39,7 +39,7 @@
+ @change="handleTableChange" row-key="id" size="middle" table-layout="fixed" :scroll="{ x: 'max-content'}">