網站首頁 編程語言 正文
場景
使用ElementUI中的Table 表格時,如果列內容超過列寬,會默認換行,如下
這樣看起來不美觀,還有可能引起其它樣式問題。那么如何解決呢?
方式一
使用Table組件自帶的show-overflow-tooltip
屬性
參數 | 說明 | 類型 | 可選值 | 默認值 |
---|---|---|---|---|
show-overflow-tooltip | 當內容過長被隱藏時顯示 tooltip | Boolean | — | false |
添加該屬性之后,如果內容超出列寬,超長部分會默認省略。當鼠標滑過該內容時,會彈出Tip
提示
<!--示例-->
<el-table-column
prop="departName"
label="部門"
show-overflow-tooltip
>
</el-table-column>
注:該屬性謹慎使用,如果多列使用且內容較多時,會影響頁面性能
方式二
計算每列最大寬度,使內容不換行;配合設置最大字符長度,可以解決大多數場景問題。接下來展示最基礎的列寬計算方式
- 示例如下
/**
* 使用span標簽包裹內容,然后計算span的寬度 width: px
* @param valArr
*/
function getTextWidth(str) {
let padding = 0;//單元格左右padding距離
let width = 0;
let span = document.createElement('span');
span.innerText = str;
span.className = 'getwidth';
document.querySelector('body').appendChild(span);
// 這里可以獲取當前單元格的font-size 以及 內容的中英文、字母等 做更精確的計算
width = document.querySelector('.getwidth').offsetWidth+padding;
document.querySelector('.getwidth').remove();
return width;
}
/**
* 遍歷列的所有內容,獲取最寬一列的寬度
* @param {Array} arr 需要計算的數據
* @param {Number} minwidth 列最小寬度
*/
function getMaxLength (arr,minwidth=60) {
return arr.reduce((acc, item) => {
if (item) {
let calcLen = getTextWidth(item);
if (acc < calcLen) {
acc = calcLen;
}
}
return acc<minwidth?minwidth:acc;
}, 0)
}
/**
* @description 計算列表列寬(把內容撐開)
* @param {Array} columns 列的數組
* @param {Array} tableArr 列表的數組
* */
function calcColumnsWidth(columns, tableArr) {
columns.forEach((item) => {
const arr = tableArr.map((x) => x[item.props]);
item.width = getMaxLength(arr);
arr.push(item.label); // 把每列的表頭也加進去算
});
return columns;
}
<!--獲取列表數據之后,計算每列最大寬度-->
let res = await this.axios.post('/api/xxx/xxxx');
if(res.data.data.length > 0){
const columns = calcColumnsWidth(this.tableHead, res.data.data);
this.tableHead = columns;
}
- 效果如下:列寬自動撐開,列表寬度不夠時,底部會出現滾動軸。
原文鏈接:https://blog.csdn.net/a736755244/article/details/125451498
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-12-28 React組件的生命周期深入理解分析_React
- 2022-04-14 詳解Redis?鍵和字符串常用命令_Redis
- 2022-05-16 輕松讀懂Golang中的數組和切片_Golang
- 2023-05-15 sql語句中臨時表使用實例詳解_MsSql
- 2022-04-28 使用Matlab制作簡易版八分音符醬游戲_C 語言
- 2022-05-10 Element-ui 中 Table 表格的設置表頭/去除下標線/設置行間距等屬性的使用及 slot
- 2022-07-02 使用numpy.mean()?計算矩陣均值方式_python
- 2023-01-07 基于Go語言實現選擇排序算法及優化_Golang
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支