網(wǎng)站首頁 編程語言 正文
場景
使用ElementUI中的Table 表格時(shí),如果列內(nèi)容超過列寬,會(huì)默認(rèn)換行,如下
這樣看起來不美觀,還有可能引起其它樣式問題。那么如何解決呢?
方式一
使用Table組件自帶的show-overflow-tooltip
屬性
參數(shù) | 說明 | 類型 | 可選值 | 默認(rèn)值 |
---|---|---|---|---|
show-overflow-tooltip | 當(dāng)內(nèi)容過長被隱藏時(shí)顯示 tooltip | Boolean | — | false |
添加該屬性之后,如果內(nèi)容超出列寬,超長部分會(huì)默認(rèn)省略。當(dāng)鼠標(biāo)滑過該內(nèi)容時(shí),會(huì)彈出Tip
提示
<!--示例-->
<el-table-column
prop="departName"
label="部門"
show-overflow-tooltip
>
</el-table-column>
注:該屬性謹(jǐn)慎使用,如果多列使用且內(nèi)容較多時(shí),會(huì)影響頁面性能
方式二
計(jì)算每列最大寬度,使內(nèi)容不換行;配合設(shè)置最大字符長度,可以解決大多數(shù)場景問題。接下來展示最基礎(chǔ)的列寬計(jì)算方式
- 示例如下
/**
* 使用span標(biāo)簽包裹內(nèi)容,然后計(jì)算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);
// 這里可以獲取當(dāng)前單元格的font-size 以及 內(nèi)容的中英文、字母等 做更精確的計(jì)算
width = document.querySelector('.getwidth').offsetWidth+padding;
document.querySelector('.getwidth').remove();
return width;
}
/**
* 遍歷列的所有內(nèi)容,獲取最寬一列的寬度
* @param {Array} arr 需要計(jì)算的數(shù)據(jù)
* @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 計(jì)算列表列寬(把內(nèi)容撐開)
* @param {Array} columns 列的數(shù)組
* @param {Array} tableArr 列表的數(shù)組
* */
function calcColumnsWidth(columns, tableArr) {
columns.forEach((item) => {
const arr = tableArr.map((x) => x[item.props]);
item.width = getMaxLength(arr);
arr.push(item.label); // 把每列的表頭也加進(jìn)去算
});
return columns;
}
<!--獲取列表數(shù)據(jù)之后,計(jì)算每列最大寬度-->
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;
}
- 效果如下:列寬自動(dòng)撐開,列表寬度不夠時(shí),底部會(huì)出現(xiàn)滾動(dòng)軸。
原文鏈接:https://blog.csdn.net/a736755244/article/details/125451498
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-06-06 Lombok報(bào)錯(cuò):You aren‘t using a compiler supported by
- 2022-11-02 React中編寫CSS實(shí)例詳解_React
- 2022-12-21 Python中的取整、取余運(yùn)算方法_python
- 2023-04-21 C語言哈希表概念超詳細(xì)講解_C 語言
- 2022-05-10 一起來學(xué)習(xí)C++中remove與erase的理解_C 語言
- 2022-07-11 Android?ListView列表優(yōu)化的方法詳解_Android
- 2022-07-01 c++詳細(xì)講解構(gòu)造函數(shù)的拷貝流程_C 語言
- 2022-07-21 SpringBoot整合SpringCache詳解
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支