目录
- 【示例1】
- 【示例2】
- 【示例3】
- 总结
【示例1】
<templete slot-scope="scope"> <el-form-item :prop="'list'. + scope.$index + '.goodModularId'"> <!-- change事件中,会获取到当前选中的值(因为默认会将event参数传递过去; 如果想要传递多个值,change($event, "传递的其他值"),将“选中的当前元素” 和 “传递的其他值” 一起传递过去 --> <el-select v-model="ruleForm.goodModularId" @change="getModularValue($event, scope.$index)" @clear="delModularValue(scope.$index)"> <el-option v-for="(item,index) in modularData" :key="index" :value="item.id" :label="item.name"></el-option> </el-select> </el-form-item> </templete> <script> data() { return { ruleForm: { list: [{ goodModularId: '', goodModular: '' }] } } } methods: { // 获取value值给goodModular getModularValue(val,index) { let obj = this.modularData.find(item => item.id === val) // 判断的时候可以直接写obj而不需要以判断对象是否为空的方式是因为:如果找不到,find方法返回的是undefined而不是空对象 if(obj) { this.ruleForm.list[index].goodModular = obj.name } else { this.ruleForm.list[index].goodModular = '' } } // 清空选项事件 delModularValue(index) { this.ruleForm.list[index].goodModular = '' } } </script>
【示例2】
<templete slot-scope="scope"> <el-form-item :prop="'list'. + scope.$index + '.goodModularId'"> <el-select v-model="ruleForm.goodModularId" @clear="delModularValue(scope.$index)"> <el-option v-for="(item,index) in modularData" :key="index" :value="item.id" :label="item.name" @click.native="getModularValue(item.id, scope.$index)"></el-option> </el-select> </el-form-item> </templete> <script> data() { return { ruleForm: { list: [{ goodModularId: '', goodModular: '' }] } } } methods: { getModularValue(val,index) { let obj = this.modularData.find(item => item.id === val) if(obj) { this.ruleForm.list[index].goodModular = obj.name } else { this.ruleForm.list[index].goodModular = '' } }, delModularValue(index) { this.ruleForm.list[index].goodModular = '' } } </script>
【示例3】
<el-form-item label="类别" prop="categoryId"> <el-select v-model="ruleForm.categoryId" @clear="clearCategory"> <el-option v-for="(item,index) in categoryOptions" :key="item.id" :value="item.id" :label="item.name" @click.native="getValue(item.name, categoryName)"></el-option> </el-select> </el-form-item>
getValue(val, type) { this.ruleForm[type] = val } clearCategory() { this.ruleForm.categoryName = '' }
总结
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)