存在两种情况:新增和编辑
- 编辑时,重要的一步是要把传过来的index放到config中,也就是更新
- 新增时,要判断config.index是否存在,不能直接使用if(this.config.index){}因为当数据里面的第一个数据的index为0时就成了新增了,要使用if(this.config.index!==”)来使用
- 更重要的一点,数据添加完之后,要恢复数据的默认值,把this.config.index=”,不过有的写成this.$set(this.config, “storeAndPosIndex”, “”);
saveStorePost() {
let { store, position } = this.form;
let storeAndPosindex = this.config.storeAndPosIndex;
this.config.showDialog = false;
let midObj = {};
for (var k in this.form.position) {
if (this.form.position[k].selected) {
midObj.postId = this.form.position[k].postId;
midObj.postName = this.form.position[k].postName;
}
}
for (var l in this.form.store) {
if (this.form.store[l].selected) {
midObj.storeId = this.form.store[l].storeId;
midObj.storeName = this.form.store[l].storeName;
}
}
if (storeAndPosindex !== "") {
this.$set(this.form.positionList, storeAndPosindex, midObj);
} else {
this.form.positionList.push(midObj);
}
this.$set(this.config, "storeAndPosIndex", "");
for (var p in this.form.position) {
this.form.position[p].selected = false;
}
for (var s in this.form.store) {
this.form.store[s].selected = false;
}
},