解決Antd Table組件表頭不對齊的問題
背景:
在使用Antd的table組件時,由于表頭參數(shù)過多,于是設(shè)置了scroll屬性,在其超出一定寬度后進行滾動
但是在添加了該屬性之后,經(jīng)常會出現(xiàn)表頭不對齊的問題:
針對該問題Google 了一下解決方案,但大多不是很完善,為解決問題?,F(xiàn)整理下完整的解決方案:
1、對表格的每一行 【columns】設(shè)置width屬性(留出一行進行寬度自適應(yīng));
2、scroll屬性中的x選擇一個合適的值(或者直接設(shè)為 max-content);
如果以上兩步仍解決不了對齊問題的話,請繼續(xù)第三步操作
3、對table的每一個td 添加 className=“word-wrap”,并設(shè)置對應(yīng)樣式
(因為td內(nèi)部的內(nèi)容在出現(xiàn)連續(xù)字母或數(shù)字的時候不會主動換行),導(dǎo)致td內(nèi)部寬度撐開,與th寬度不一致
.word-wrap { word-break: break-all; }
以上操作完成之后可能還是有問題(請檢查下是不是表頭中內(nèi)容的寬度默認(rèn)被撐開了),然后重新調(diào)整下column中的width即可
近期在開發(fā)的過程中,另發(fā)現(xiàn)了一種非常有效得解決方案,特與大家分享:
在對columns的每一項設(shè)置了寬度后,如果還是有錯位問題的話,可以嘗試在columns的末位push一個空的column進行占位,這個空的column不用設(shè)置寬度,任其自適應(yīng)。
注意:該column的title需要設(shè)置為空字符串,避免在界面上將其渲染出來
補充知識: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 計算當(dāng)前文字比較列表文字 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組件表頭不對齊的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vueJs函數(shù)readonly與shallowReadonly使用對比詳解
這篇文章主要為大家介紹了vueJs函數(shù)readonly與shallowReadonly使用對比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03vue-admin-template模板添加tagsview的實現(xiàn)
本文主要介紹了vue-admin-template模板添加tagsview的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04解決vue中無法動態(tài)修改jqgrid組件 url地址的問題
下面小編就為大家分享一篇解決vue中無法動態(tài)修改jqgrid組件 url地址的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Vue插槽slot詳細(xì)介紹(對比版本變化,避免踩坑)
Vue中的Slot對于編寫可復(fù)用可擴展的組件是再合適不過了,下面這篇文章主要給大家介紹了關(guān)于Vue插槽slot詳細(xì)介紹的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06Vue項目引用百度地圖并實現(xiàn)搜索定位等功能(案例分析)
這篇文章主要介紹了Vue項目引用百度地圖并實現(xiàn)搜索定位等功能(案例分析),本篇文章為案例分析,技術(shù)點較多,所以篇幅較長,認(rèn)真閱覽的你一定會學(xué)到很多知識,需要的朋友可以參考下2022-09-09詳解三種方式解決vue中v-html元素中標(biāo)簽樣式
這篇文章主要介紹了三種方式解決vue中v-html元素中標(biāo)簽樣式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11