Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加 el-table 自适应高度指令 #1702

Merged
merged 6 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/directive/el-table/adaptive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

import { addResizeListener, removeResizeListener } from 'element-ui/src/utils/resize-event'

/**
* How to use
* <el-table height="100px" v-el-height-adaptive-table="{bottomOffset: 30}">...</el-table>
* el-table height is must be set
* bottomOffset: 30(default) // The height of the table from the bottom of the page.
*/

const doResize = (el, binding, vnode) => {
const { componentInstance: $table } = vnode

const { value } = binding

if (!$table.height) {
throw new Error(`el-$table must set the height. Such as height='100px'`)
}
const bottomOffset = (value && value.bottomOffset) || 30

if (!$table) return

const height = window.innerHeight - el.getBoundingClientRect().top - bottomOffset
$table.layout.setHeight(height)
$table.doLayout()
}

export default {
bind(el, binding, vnode) {
el.resizeListener = () => {
doResize(el, binding, vnode)
}

addResizeListener(el, el.resizeListener)
},
inserted(el, binding, vnode) {
doResize(el, binding, vnode)
},
unbind(el) {
removeResizeListener(el, el.resizeListener)
}
}
14 changes: 14 additions & 0 deletions src/directive/el-table/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import adaptive from './adaptive'

const install = function(Vue) {
Vue.directive('el-height-adaptive-table', adaptive)
}

if (window.Vue) {
window['el-height-adaptive-table'] = adaptive
Vue.use(install); // eslint-disable-line
}

adaptive.install = install
export default adaptive