checkListData:[];//多选框里要显示的数据
checkListDataStr:"",
checkList:[
{propCode:"01",propName:'测试1'},
{propCode:"02",propName:'测试2'},
]
<el-checkbox-group
v-model="checkListData"
>
<el-checkbox
v-for="item in checkList"
:label="item.propCode"
@change="orgAttributeChange(item,$event)"
:key="item.propCode"
>{{ item.propName }}</el-checkbox >
</el-checkbox-group>
//选中值变化时的方法
orgAttributeChange(item,val) {
if (val) {
// 增加值
let list = this.checkList.filter((i,v)=> {
return i.propCode == item.propCode;
})
this.checkListDataStr += ","+ list[0].propCode;//最后选中的数据
let flag = _.startsWith(this.checkListDataStr, ',');
//有,要去掉字符串前面的,没有则不需要
if (flag) {
this.form.orgAttribute = this.checkListDataStr.substring(1);
}else {
this.form.orgAttribute = this.checkListDataStr;
}
this.checkListData = this.form.orgAttribute.split(',');
}else {
//不勾选当前选中的值
let arr = this.checkListData;
_.remove(arr, function(n) {
return n == item.propCode;
});
this.checkListData = arr;
// 选择后的数据转化为字符串
let str = arr.join(',');
this.form.orgAttribute = str;//要传递给后台的数据
this.checkListDataStr = str;
}
},
设置checobox的回显时,先拿到后台返回的数据,是一个字符串。将字符串转化成数组,进行回显。
this.checkListDataStr = rowData.orgAttribute;
this.checkListData = rowData.orgAttribute.split(’,’);