1. 程式人生 > >vue+element ui 使用$refs獲取el-dialog 下的 el-table 元件 ,以及使用table多選,預設選中

vue+element ui 使用$refs獲取el-dialog 下的 el-table 元件 ,以及使用table多選,預設選中

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