您的位置:首页 > 产品设计 > UI/UE

vue+element ui 使用$refs获取el-dialog 下的 el-table 组件 ,以及使用table多选,默认选中

2018-01-11 16:57 3951 查看
el-dialog代码块:

<el-button @click="opendialog">打开dialog</el-button>

<el-dialog title="我是标题" :visible.sync="role_hidden" width="60%" class="details" @open="show">

        <el-table border style="width:100%" ref="table" :data="roleslist" tooltip-effect="dark" size="small" stripe @selection-change="handleSelectionChange">

         <el-table-column type="selection" width="55"></el-table-column>

          <el-table-column label="内容"  prop="display_name" ></el-table-column>

          <el-table-column label="内容"  prop="description" ></el-table-column>  

          <el-table-column label="内容"  prop="name" ></el-table-column>

        </el-table>

      <el-button type="primary" @click="submit_roles" :loading="rubmitloading" style="margin-top:10px;padding:10px 25px;">确 定</el-button>

  </el-dialog>

这样直接在opendialog方法中直接使用this.$refs.table是获取不到的,那么怎么获取呢?

这时就需要在el-dialog这个标签里面添加一个open方法@open="show",然后在open方法里面进行操作

vue代码:

import vue from 'vue'

methods: {

    show () {

      vue.nextTick(_ => {

console.log(this.$refs.table) // 获取el-dialog中的table

        let selected = this.selects.split(',')

        selected.forEach(i => {

          this.$refs.table.toggleRowSelection(this.roleslist.find(d => parseInt(d.id) === parseInt(i)), true)  // 设置默认选中

        })

      })

    }}

这样就可以啦


参考:https://segmentfault.com/q/1010000008757348
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: