解決Antd Table組件表頭不對齊的問題
背景:
在使用Antd的table組件時,由于表頭參數(shù)過多,于是設置了scroll屬性,在其超出一定寬度后進行滾動

但是在添加了該屬性之后,經(jīng)常會出現(xiàn)表頭不對齊的問題:

針對該問題Google 了一下解決方案,但大多不是很完善,為解決問題。現(xiàn)整理下完整的解決方案:
1、對表格的每一行 【columns】設置width屬性(留出一行進行寬度自適應);
2、scroll屬性中的x選擇一個合適的值(或者直接設為 max-content);
如果以上兩步仍解決不了對齊問題的話,請繼續(xù)第三步操作
3、對table的每一個td 添加 className=“word-wrap”,并設置對應樣式
(因為td內部的內容在出現(xiàn)連續(xù)字母或數(shù)字的時候不會主動換行),導致td內部寬度撐開,與th寬度不一致
.word-wrap {
word-break: break-all;
}
以上操作完成之后可能還是有問題(請檢查下是不是表頭中內容的寬度默認被撐開了),然后重新調整下column中的width即可
近期在開發(fā)的過程中,另發(fā)現(xiàn)了一種非常有效得解決方案,特與大家分享:
在對columns的每一項設置了寬度后,如果還是有錯位問題的話,可以嘗試在columns的末位push一個空的column進行占位,這個空的column不用設置寬度,任其自適應。
注意:該column的title需要設置為空字符串,避免在界面上將其渲染出來
補充知識:vue實現(xiàn)超過兩行顯示展開收起
基于vue-cli2,sass,vant(ui組件):https://youzan.github.io/vant/#/zh-CN/home
具體代碼如下:
<template>
<div>
<div class="group">
<div class="text more" ref="more">
占位
</div>
<div class="list" v-for="(item, index) in historyList" :key="index">
<van-row>
<van-col span="12">{{ item.version }}</van-col>
<van-col class="t_right l_text" span="12">{{ item.time }}</van-col>
</van-row>
<div class="l_title">{{ item.title }}</div>
<div
class="text"
ref="textContainer"
:class="{ retract: item.status }"
:style="{ 'max-height': item.status ? textHeight : '' }"
>
{{ item.content }}
</div>
<span
v-if="item.status !== null"
class="link"
@click="more(index)"
>{{ item.status ? "展開" : "收起" }}</span
>
</div>
</div>
</div>
</template>
<script>
export default {
data () {
return {
textHeight: '',
historyList: [
{
version: '7.1.4',
title: '本次更新',
content:
'-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦;-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦',
time: '2周前'
},
{
version: '7.1.4',
title: '本次更新',
content:
'-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦',
time: '5周前'
},
{
version: '7.1.4',
title: '本次更新',
content:
'-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦;-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦',
time: '6周前'
},
{
version: '7.1.4',
title: '本次更新',
content:
'-聽模塊新增“文章難度分析”功能~,為你分析文章中詞匯、語速等難度,推薦',
time: '9周前'
}
]
}
},
mounted () {
this.historyList.forEach((ele, index) => {
this.$set(
this.historyList,
index,
Object.assign({}, ele, { status: null })
)
})
// DOM 加載完執(zhí)行
this.$nextTick(() => {
this.calculateText()
//console.log(this.historyList)
})
window.onresize = () => {
this.historyList.forEach((ele, index) => {
this.$set(
this.historyList,
index,
Object.assign({}, ele, { status: null })
)
})
setTimeout(() => {
this.calculateText()
}, 0)
}
},
methods: {
// 計算文字 顯示展開 收起
calculateText () {
// 獲取一行文字的height 計算當前文字比較列表文字
let oneHeight = this.$refs.more.scrollHeight
let twoHeight = oneHeight * 2 || 40
this.textHeight = `${twoHeight}px`
let txtDom = this.$refs.textContainer
for (let i = 0; i < txtDom.length; i++) {
let curHeight = txtDom[i].offsetHeight
if (curHeight > twoHeight) {
this.$set(
this.historyList,
i,
Object.assign({}, this.historyList[i], { status: true })
)
} else {
this.$set(
this.historyList,
i,
Object.assign({}, this.historyList[i], { status: null })
)
}
}
},
more (index) {
this.historyList[index].status = !this.historyList[index].status
}
}
}
</script>
<style lang="scss" scoped>
.group {
.list {
padding: 5px 0;
border-bottom: 1px solid #eaeaea;
}
.text {
position: relative;
color: #000;
font-size: 14px;
}
.more {
visibility: hidden;
}
.link {
font-size: 12px;
color: #2d95fe;
}
.retract {
position: relative;
overflow: hidden;
}
.retract:after {
content: "...";
position: absolute;
bottom: 0;
right: 2px;
width: 25px;
padding-left: 25px;
background: linear-gradient(to right, transparent, #fff 45%);
}
}
</style>
以上這篇解決Antd Table組件表頭不對齊的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vueJs函數(shù)readonly與shallowReadonly使用對比詳解
這篇文章主要為大家介紹了vueJs函數(shù)readonly與shallowReadonly使用對比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
vue-admin-template模板添加tagsview的實現(xiàn)
本文主要介紹了vue-admin-template模板添加tagsview的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
解決vue中無法動態(tài)修改jqgrid組件 url地址的問題
下面小編就為大家分享一篇解決vue中無法動態(tài)修改jqgrid組件 url地址的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Vue項目引用百度地圖并實現(xiàn)搜索定位等功能(案例分析)
這篇文章主要介紹了Vue項目引用百度地圖并實現(xiàn)搜索定位等功能(案例分析),本篇文章為案例分析,技術點較多,所以篇幅較長,認真閱覽的你一定會學到很多知識,需要的朋友可以參考下2022-09-09

