动态表格如下:
/**
* 设置表格数据 该方法在新增规格、新增规格值、规格和规格值的文本改变都要调用
*/
setTableList(){
let newList=[];
this.getRowList(null,0,newList)
this.tableList=newList;
},
/**
* 获取行数据 specList规格集合 tableList表格集合
* @param specValues 多规格值通过逗号拼接组成每一行唯一值
* @param specIndex 规格索引
* @param newList 新表格数据
*/
getRowList(specValues,specIndex,newList){
if(specIndex<this.specList.length){
this.specList[specIndex].values.forEach((t)=>{
let newValues=(specValues===null?t.name:(specValues+","+t.name))
//当前规格值,一直往下规格值循环完
this.getRowList(newValues,specIndex+1,newList)
})
//当添加新规格(第二个规格的时候)没有规格值时,默认一个空值
if(this.specList[specIndex].values.length===0 && specValues!==null){
let newValues=specValues+","
this.getRowList(newValues,specIndex+1,newList)
}
}
//当最后规格的规格值循环到最后后添加行数据
else{
//判断表格行数据是否已经存在,存在直接添加不创建新行
let row = this.tableList.find((r)=>{return r.specValues===specValues});
if(row){
newList.push({...row})
}
else{
//默认的字段
row = {
specValues:specValues,
amount:0,
code:"",
code_name:""
}
//通过规格值循环出规格值列数
specValues.split(',').forEach((c,index)=>{
row["sku-"+index]=c
})
newList.push(row);
}
}
},