【ElementUI】实现el-table 列宽自适应(v-fit-columns)
一、安装插件
npm install v-fit-columns --save
二、入口文件引入插件
import Vue from 'vue';
import Plugin from 'v-fit-columns';
Vue.use(Plugin);
三、 示例
- 添加v-fit-columns;
- 添加class-name="leave-alone"表示这一列不受v-fit-columns影响(即不进行自适应);
<template>
<el-table
v-fit-columns
ref='table'
:data="tableData"
style="width: 100%">
<el-table-column
class-name="leave-alone"
prop="date"
label="日期"
width="180">
</el-table-column>
<el-table-column
class-name="leave-alone"
prop="name"
label="姓名"
width="180">
</el-table-column>
<el-table-column
prop="address"
label="地址">
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [{
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄'
}, {
date: '2016-05-04',
name: '王小虎',
address: '上海市普陀区金沙江路 1517 弄'
}]
}
}
}
</script>
四、注意点
- 使用v-fit-columns后可能导致表头错位
解决办法:
this.$nextTick(() => {
this.$refs.table.doLayout();
});
this.$forceUpdate();
- 使用v-fit-columns后使用fixed可能导致表头错位;
解决办法:
不使用fixed,其他办法自行查阅资料;