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上增加一个指令,根据剩余空间,自动设置最大高度,这样表格不会超出整个页面,滚动条只会出现在表格内部 #1662

Open
lxy545480918 opened this issue Mar 5, 2019 · 7 comments

Comments

@lxy545480918
Copy link

No description provided.

@lxy545480918 lxy545480918 changed the title 能不能在el-table上增加一个指令,使其数据量多的时候,只在appmain里面滚动,而不是整个页面 能不能在el-table上增加一个指令,根据剩余空间,自动设置最大高度,这样表格不会超出整个页面,滚动条只会出现在表格内部 Mar 5, 2019
@PanJiaChen
Copy link
Owner

这应该给 element-ui 提吧。。。

@lxy545480918
Copy link
Author

@PanJiaChen 大神,element-ui的效率没你高啊,这个指令可以更加好一点

@yuntao1997
Copy link
Contributor

yuntao1997 commented Mar 13, 2019

刚好写了一个,应该能满足你的要求

/**
 * 表格高度自适应,使用此功能必须先为表格设置初始值
 */
Vue.directive('table-height-adaptive', {

    /**
     * 
     * @param {*} el 
     * @param {*} binding 
     * binding.value 格式为 {
     *      table: $refs.table, //  表格对象
     *      topHeight: 140      //  表格顶边 距离顶部高度,默认值为  140
     *      footerHeight: 120   //  表格底部 距离底部高度,默认值为  120
     * }
     * @param {*} vnode 
     * @param {*} oldVnode 
     */
    update(el, binding, vnode, oldVnode) {
        let topHeight = binding.value.topHeight ? binding.value.topHeight : 140
        let footerHeight = binding.value.footerHeight ? binding.value.footerHeight : 120


        let table = binding.value.table


        let tableHeight = window.innerHeight - el.offsetTop - footerHeight - topHeight;


        table.layout.setHeight(tableHeight)
        table.doLayout()

        // 监听窗口大小变化
        window.addEventListener("resize", () => {
            tableHeight = window.innerHeight - el.offsetTop - footerHeight - topHeight
            table.layout.setHeight(tableHeight)
            table.doLayout()
        })
    }
})

用法如下:

 <el-table ..... **ref="table" height="50" v-table-height-adaptive="{table: $refs.table}"

@PanJiaChen
Copy link
Owner

@yuntao1997 欢迎pr

@yuntao1997
Copy link
Contributor

yuntao1997 commented Mar 13, 2019

@PanJiaChen 已提交 pr

demo预览:http://jsrun.net/a3XKp/edit

#1702

@lxy545480918
Copy link
Author

@yuntao1997 流弊

@1amTheWind
Copy link

@yuntao1997 大神orz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants