vue中使用formData传参,复杂list不能解析
发布于:

data结构:

formData:{
    StakeList: [
        {
            pipeName: '',
            stakeNumBefore:'',
            stakeNumAfter:'',
            mainBeforeYear:'',
            mainAfterYear:'',
            stakeNum:'',
            flagType:'',
            smallList:[]
        }
    ],
}

处理:

var form = _this.formData;
var answer = {};
if(form.StakeList.length>0){
    for(var i=0;i<form.StakeList.length;i++){
        answer['StakeList['+i+'].flagType']=form.StakeList[i].flagType;
        answer['StakeList['+i+'].pipeName']=form.StakeList[i].pipeName;
        answer['StakeList['+i+'].stakeNum']=form.StakeList[i].stakeNum;
        if(form.StakeList[i].smallList.length>0){
            for(var j=0;j<form.StakeList[i].smallList.length;j++){
                answer['StakeList['+i+'].smallList['+j+'].stakeNum']=form.StakeList[i].smallList[j].stakeNum;
                answer['StakeList['+i+'].smallList['+j+'].flagType']=form.StakeList[i].smallList[j].flagType;
            }
        }
    }
}


总结:formData处理数据会把复杂list封装成stakeList[smallList]这种,需要转为上述格式,才能被实体类接收。