xyj-check
父组件中使用
<xyj-check :config="config" :list="list" :echo-list="echoList" @getList="fn"></xyj-check>
:config 配置选择框 已下均为默认值可以自行修改
config: {
is:false,
isAll: true,
max: 100,
min: 0,
size: 'small',
type: 'button',
textColor: '#fff',
fill: '#123',
isGroup:false
}
:list 可选择的选项
业务场景1:固定选项 写死参数
list:[
{
name: '长沙',
id: 1,
disabled: true
},
{
name: '武汉',
id: 2
},
{
name: '泉州',
id: 3
}
],
业务场景2:不固定选项 从后端api获取
1.假设后端返回的JSON为
this.cityList=[
{
label: '长沙',
cityId: 1,
},
{
label: '武汉',
cityId: 2
},
{
label: '泉州',
cityId: 3
}
]
this.list = this.cityList.map(item => {
return {
name:item.label,
id:item.cityId
};
});
@getList="fn" 子组件会告诉父组件勾选了哪些
1.比如我们勾选了 长沙
methods:{
fn(res){
console.log(res)
this.cityList=res
}
save(){
const data={
cityList:this.cityList
}
...发起请求给后端传参
}
}
:echoList 回显数据
业务场景:请求后端api保存后 一般查看详情进行之前保存的修改 需要回显告诉我们之前选择了哪几个
1.假设后端返回[{label:'长沙',cityId:1}]这种格式
this.config.isGroup=true
2.假设后端返回[1,2,3]这种格式
this.config.isGroup=false
3.假设我们之前选的是长沙,后端详情api返回的JSON为
this.cityList=[
{
label: '长沙',
cityId: 1,
checkd:true
},
{
label: '武汉',
cityId: 2,
checkd:false
},
{
label: '泉州',
cityId: 3,
checkd:false
}
]
this.echoList=[]
this.echoList = this.cityList.map(item => {
if(item.checkd){
this.echoList.push(item.cityId)
}
});
console.log(this.echoList)
this.echoList=[]
this.echoList = this.cityList.map(item => {
if(item.checkd){
this.echoList.push({name:item.label,id:item.cityId})
}
});
console.log(this.echoList)