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
<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
相关文章推荐
- 前端踩坑小结:当elementui的el-dialog组件中包含子组件时,用refs调用子组件时的undefined问题
- vue 使用element-ui的el-input监听不了回车事件
- element-ui 组件的 table 多选,翻页有记忆功能
- 解决element-ui中el-menu组件作为vue-router模式在刷新页面后default-active属性与当前路由页面不一致问题的方法
- VUE 使用新版本 element-ui 组件库 Select 组件时, value 值为对象时的 BUG 处理
- Table 组件使用指南之七:获取选中的Rows
- vue2.0 + element UI 中 el-table 数据导出Excel的方法
- vue2.0的小坑——当Element UI 中的el-dialog包含子组件时,refs报undefind的问题
- vue2.0 使用element-ui里的upload组件实现多图上传。采用FORMDATA的方式上传。
- vue使用element-ui的el-input监听不了回车事件解决
- VueJS ElementUI el-table 的 formatter 和 scope template 不能同时存在
- element UI table表格组件使用[超级完整功能]
- vue中使用element-ui的el-input监听不了回车事件???
- UI - 修改tableView多选默认蓝色选中按钮 , 全选 , 取消全选
- 在vue项目中使用element-ui的Upload上传组件
- vue elementui table默认显示子表格
- vue--element-ui--tree组件解决从外部按钮取消选中状态
- VueJS ElementUI el-table 的 formatter 和 scope template 不能同时存在
- vue使用element-ui的el-input监听不了回车事件解决
- vue基本使用--refs获取组件或元素