优化这段代码 if ( this.queryParam.tonnageOne == '' || this.queryParam.tonnageTwo == '' || this.queryParam.tonnageThree == '' || this.dataSource.some((item) => !item.feiGangName) || this.dataSource.some((item) => !item.feiGangPrice) || this.dataSourceFeWater.some((item) => !item.tscfhcbName) || this.dataSourceFeWater.some((item) => !item.temp) || this.dataSourceFeWater.some((item) => !item.tscfhcb_C) || this.dataSourceFeWater.some((item) => !item.tscfhcb_Si) || this.dataSourceFeWater.some((item) => !item.tscfhcb_Mn) || this.dataSourceFeWater.some((item) => !item.tscfhcb_P) || this.dataSourceFeWater.some((item) => !item.tscfhcb_S) || this.dataSourceFeWater.some((item) => !item.tscfhcb_Price) ) { this.$message.error('请检查值,或者名称不能为空!!!') } else { this.dataSource.forEach((item) => { item['acoursewareId'] = 3343772731908188 const editableItem = this.editableData[item.id] if (editableItem) { Object.assign(item, editableItem) } }) this.editableData = [] this.dataSourceFeWater.forEach((item) => { item['acoursewareId'] = 3343772731908188 const editableItem = this.editableDataFeWater[item.id] if (editableItem) { Object.assign(item, editableItem) } }) this.editableDataFeWater = [] console.log(this.dataSourceFeWater) // 清空可编辑数据 this.conceal = false this.concealFeWater = false var addZldwInfo = { AcoursewareId: 3343772731908188, tons1: this.queryParam.tonnageOne, tons2: this.queryParam.tonnageTwo, tons3: this.queryParam.tonnageThree, } let param = { addZldwInfo: addZldwInfo, addFgList: this.dataSource, b_TscfhcbListAdd: this.dataSourceFeWater, } FeAdd(param) .then((res) => {}) .catch((error) => {}) }
时间: 2024-02-14 07:10:52 浏览: 143
JAXRS.:REST式Web服务API中新增的和值得关注的功能.ppt
你可以先将一些重复的代码提取成函数,以减少代码冗余。同时,你可以将这些验证条件提取出来,方便进行管理和修改。下面是一个简单的示例:
```javascript
function validateData(dataSource, dataSourceFeWater, queryParam) {
// 验证条件
const conditions = [
queryParam.tonnageOne === '',
queryParam.tonnageTwo === '',
queryParam.tonnageThree === '',
dataSource.some((item) => !item.feiGangName),
dataSource.some((item) => !item.feiGangPrice),
dataSourceFeWater.some((item) => !item.tscfhcbName),
dataSourceFeWater.some((item) => !item.temp),
dataSourceFeWater.some((item) => !item.tscfhcb_C),
dataSourceFeWater.some((item) => !item.tscfhcb_Si),
dataSourceFeWater.some((item) => !item.tscfhcb_Mn),
dataSourceFeWater.some((item) => !item.tscfhcb_P),
dataSourceFeWater.some((item) => !item.tscfhcb_S),
dataSourceFeWater.some((item) => !item.tscfhcb_Price),
];
if (conditions.some((condition) => condition)) {
// 验证失败
return false;
}
// 验证成功
return true;
}
function updateData(dataSource, editableData) {
dataSource.forEach((item) => {
item['acoursewareId'] = 3343772731908188;
const editableItem = editableData[item.id];
if (editableItem) {
Object.assign(item, editableItem);
}
});
editableData = [];
}
// 在原来的代码中调用新的函数
if (validateData(this.dataSource, this.dataSourceFeWater, this.queryParam)) {
updateData(this.dataSource, this.editableData);
updateData(this.dataSourceFeWater, this.editableDataFeWater);
console.log(this.dataSourceFeWater);
// 清空可编辑数据
this.conceal = false;
this.concealFeWater = false;
var addZldwInfo = {
AcoursewareId: 3343772731908188,
tons1: this.queryParam.tonnageOne,
tons2: this.queryParam.tonnageTwo,
tons3: this.queryParam.tonnageThree,
};
let param = {
addZldwInfo: addZldwInfo,
addFgList: this.dataSource,
b_TscfhcbListAdd: this.dataSourceFeWater,
};
FeAdd(param)
.then((res) => {})
.catch((error) => {});
} else {
this.$message.error('请检查值,或者名称不能为空!!!');
}
```
上面的代码,我们将验证条件和更新数据的操作分别提取成了两个函数:`validateData` 和 `updateData`。这样可以让代码更加清晰和易于维护。
阅读全文