原本想在 jsfiddle 重現下的,結果沒有成功,就給你們看截圖吧css
vue版本:2.5.3
element-ui版本:1.4.13
瀏覽器:chrome 66.0.3359.170
複製代碼
你們能夠發現,固定了高度的 table 不只出現了縱向滾動條,橫向滾動條也出現了(只能滾動一點點),其實寬度是足夠顯示的。html
而後將問題在網上搜尋,查到了相關的issues github.com/ElemeFE/ele… github.com/ElemeFE/ele…vue
其中#78的問答者給出了一個解決方案github.com/QingWei-Li/…git
有一點和#78類似,個人 table 也是配置化的,表頭和內容都是做爲屬性傳過去,可能致使表格在初次渲染時寬度計算不許確,致使最後寬度超過了 2px(經查驗是 border 的寬度);但解決方案不適用,一方面項目採用的是 cdn 加載方式,另外一方面我嘗試了 1.4.13 版本和解決方案的 1.0.1 版本代碼結構不一致,而我對 element 組件不瞭解無從下手,因此我嘗試本身去解決。github
<template>
<div>
<el-table border :data="data" height="550" v-loading="isLoading">
<el-table-column v-for="{value, label, options} in columns" :key="value" :label="label" :prop="value">
<template slot-scope="{row}">
<span v-if="!options">{{row[value]}}</span>
<div v-else-if="options && options.filter">{{row[value] | filterData(options.filter)}}</div>
</template>
</el-table-column>
<el-table-column label="操做">
<template slot-scope="{row}">
<!--這裏可在父級插入自定義操做按鈕-->
<slot :row="row"/>
</template>
</el-table-column>
</el-table>
<Pagination :total="total" @fetchData="$emit('fetchData')"/>
</div>
</template>
<script>
import Pagination from 'FORM/Pagination'
import {formatTimestamp} from 'UTILS'
export default {
props: {
isLoading: {
type: Boolean,
default: false,
required: true
},
columns: {
type: Array,
default: [],
required: true
},
data: {
type: Array,
default: [],
required: true
},
total: {
type: Number,
default: 0,
required: true
}
},
components: {
Pagination
},
filters: {
filterData: function (value, filter) {
if (!filter) return value
switch (filter) {
case 'formatDate':
return formatTimestamp(value)
break;
default:
return value
}
}
}
}
</script>
複製代碼
在配置化組件的updated
鉤子更新 columns 數據(中間作了一層轉換,沒有直接修改 props ),但 table 貌似不會將數據進行雙向綁定(這方面不肯定,只是我調試時修改 table 的內容不能實時更新,須要刷新瀏覽器),因此 table 沒有出現改變,也就不會觸發第二次寬度計算。chrome
在瀏覽器中調試發現去掉.el-table
的左右 border 後,是能夠解決內容溢出產生滾動條的問題的;具體作法是在 updated 1s後給el-table
綁定一個 class,去除 border,但沒有 border 太不美觀了。。。element-ui
也是在瀏覽器中調試,發現將.el-table__body-wrapper
的overflow屬性隱藏再顯示,橫向滾動條就不會出現了,應該是從新添加overflow: auto;
使得瀏覽器自身從新計算了 table 的寬度,因此寬度足夠的狀況下不須要顯示橫向滾動條;瀏覽器
那作法就是:bash
<!-- template -->
<el-table ref="configurationTable" border :data="data" :height="height" v-loading="isLoading" :class="['configurationTable', {afterRenderClass: showAfterRenderClass}]">
<!-- xxx -->
</el-table>
複製代碼
// script
data () {
return {
showAfterRenderClass: false
}
},
updated () {
/** * 用於隱藏固定高度時顯示沒必要要的scrollX的定時器 * 這是el-table初次渲染時寬度計算的bug,可在渲染後經過從新賦予overflow: auto調整 */
const handleScrollX = setInterval(() => {
// 檢測是否已經生成table節點,若是不是就每隔0.5s檢測一次
// 只有生成了真實節點纔可以經過修改overflow屬性讓瀏覽器從新計算
if (this.$refs.configurationTable) {
this.showAfterRenderClass = true
clearInterval(handleScrollX)
}
}, 500)
}
複製代碼
/** style (注意不要設爲scoped) */
/** configurationTable和afterRenderClass都是爲了標記僅這個組件內修改 */
.configurationTable .el-table__body-wrapper {
overflow: hidden;
}
.afterRenderClass {
.el-table__body-wrapper {
overflow: auto;
}
}
複製代碼
由於element-ui v1已經中止維護了,因此也就不提新的issue了,以後仍是須要遷移到element-ui2(若是你們有遷移工具的話歡迎推薦,官方踩過坑也能夠推薦),否則留下這個黑魔法增長維護成本。app