Vue+ElementUI 弹窗的表格中(第一列是选择框),在修改弹窗时,需要回显并勾选中在新增时已选择的表格数据。

1、效果图

2、一些思路+碰到的报错

(1)row:表格的行数据,选中/取消选中哪行,就是哪行的数据

注意:row只能是表格的data属性指定的数组中的数据,放其它数组中数据是无效的

(2)selected:true-选中;false-取消选中

this.$refs.table.toggleRowSelection(row, selected)

(3)在打开弹窗时,需要先让弹窗的visible变为true,再调获取表格数据的接口。

明明this.$refs里面有东西,但是打印this.$refs.xxx的时候却是undefined。

 ref 只有等页面加载完成好之后你才能调用 this.$refs ,如果你使用v-if 、v-for渲染页面的话,那么在刚开始页面没没渲染之前你是拿不到this.$refs的,所以要等到页面渲染之后拿才可以。

解决方法:

如果写在method中,那么可以使用 this.$nextTick(() => {})等页面渲染好再调用this.$refs.xxx,这样就可以了。

(4)控制台报错:“Error: row is required when get row identity”

参考1(主要的):“Error: row is required when get row identity“-CSDN博客

参考2:element-ui之“row is required when get row identity”报错 - 知乎

(5)设置表格勾选

 参考1: 

【解决】Element UI表格Table组件,点击翻页后之前选中的数据仍保留,勾选中的样式也会回显_@selection-change="" 已选中的数据,从其他地方移除,表格的数据还是选中的-CSDN博客

参考2: ElementUI的表格设置勾选toggleRowSelection_element table togglerowselection-CSDN博客

注意:选中表格数据的数组要赋值1个创建的新数组

参考3:

基于el-table二次封装的表格组件,toggleRowSelection 默认选中事件被清空的问题_toggleselect 对应的清空-CSDN博客

注意:

有的时候会提示toggleRowSelection is not a function 

此时打印this.$refs.table。看看它是什么,有时候他是个数组,所以需要改成

this.$refs.table[0].toggleRowSelection(item, true);

有时候他是个对象,在对象里找,可能会找到$children,这个$children是个数组,里面的第一项里有toggleRowSelection属性,所以这种情况下可以改成(这种情况一般都是因为各个公司基于el-table进行了二次封装导致的,所以得在里面慢慢找)

this.$refs.table.$children[0].toggleRowSelection(item, true);

3、代码

<el-table ref="multipleClusterTable" v-loading="dialogClusterLoading" :data="dialogClusterDataList" :row-key="getDialogClusterRowId" @selection-change="selectedDialogClusterDataCurrentRow">
  <el-table-column type="selection" width="55"></el-table-column>
  <el-table-column label="集群名称" prop="name" min-width="120" show-overflow-tooltip></el-table-column>
  <el-table-column label="URL" prop="accessUrl" min-width="120" show-overflow-tooltip></el-table-column>
</el-table>
getDialogClusterRowId(row){
  return row.id;  // row-key绑定的必须为唯一值
},
selectedDialogClusterDataCurrentRow(val){
  this.currentDialogClusterData = val;
},
if(this.isModify == false){
  //新增的时候,表格当前选中数据为空
  this.currentDialogClusterData= [];
}else{
  //修改的时候,判断是否有已经选择过的数据
  var selectionClusterArr = [];
  selectionClusterArr = this.currentDialogClusterData;
  if (selectionClusterArr.length > 0) { // 判断是否存在勾选过的数据
    this.$nextTick(() => {
      selectionClusterArr.forEach( selectItem => { // 勾选到的数据
        let row = this.dialogClusterDataList.filter(item => item.id == selectItem.id)[0];
        if (row) {
          this.$refs.multipleClusterTable.toggleRowSelection(row,true);
        }
      });
    })
  }
}

可以使用 Element UI 的 Upload 组件来上传图片,然后在 Select 组件选择已上传的图片。具体步骤如下: 1. 引入 Element UI 和 Vue.js: ```html <!-- 引入 Element UI 样式 --> <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"> <!-- 引入 Vue.js --> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <!-- 引入 Element UI 组件库 --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> ``` 2. 创建一个 Vue 实例,并在模板添加 Select 组件和 Upload 组件: ```html <div id="app"> <el-select v-model="imageUrl" placeholder="请选择图片"> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> <img :src="item.value" alt="" style="width: 50px; height: 50px;"> </el-option> </el-select> <el-upload class="upload-demo" action="/upload" :on-success="handleSuccess" :show-file-list="false" :before-upload="beforeUpload"> <el-button size="small" type="primary">点击上传</el-button> </el-upload> </div> ``` 3. 在 Vue 实例定义数据和方法: ```js new Vue({ el: '#app', data() { return { options: [], // Select 组件的选项 imageUrl: '' // Select 组件选中的图片地址 } }, methods: { // 上传成功后将图片地址添加到 Select 组件的选项 handleSuccess(response, file, fileList) { this.options.push({ label: file.name, value: response.url }) }, // 上传前校验文件类型和大小 beforeUpload(file) { const isJPG = file.type === 'image/jpeg' const isPNG = file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 2 if (!(isJPG || isPNG)) { this.$message.error('上传图片只能是 JPG 或 PNG 格式!') } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!') } return isJPG || isPNG && isLt2M } } }) ``` 4. 在 handleSuccess 方法将上传成功的图片地址添加到 Select 组件的选项,然后在模板使用 img 标签显示图片即可。 这样就实现了使用 Select 组件选择已上传的图片并在 Select 回显图片的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值