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'}">