使用指令, 默认100% 可以自定义减去
export const autoHeight = {
bind: function (el, binding, vnode, oldVnode) {},
inserted: function (el, binding) {
const parentH = el.parentElement.offsetHeight;
const val = binding.value || 0;
if (parentH) {
const h = parentH - Number(val);
el.style.height = h + 'px';
}
},
};
import Vue from 'vue';
import { autoHeight } from './utils/directive';
Vue.directive('autoHeight', autoHeight);
new Vue({
el: '#app',
render: h => h(App),
});
<el-table :max-height="tableH" v-autoHeight:height="autoH"></>
<script>
export default {
methods: {
getHeight() {
this.$nextTick(() => {
if (this.$refs.tableBoxRef) {
const h = this.$refs.tableBoxRef.clientHeight;
this.tableH = h;
}
});
},
},
computed: {
autoH: function () {
if (this.showPagination) {
return 120;
} else {
return 80;
}
},
},
mounted() {
this.getHeight();
},
}
</script>