循环数组 for foreach.....
循环对象 for in
后端返回的数据为num 实际渲染为字符串 需要把num和前端字符串进行匹配,所以利用各种各样的循环 将num 匹配为字符串
import chinaJSON from '@/assets/ChinaCity.json';
if (res.success) {
if (res.data != null && res.data.list.length > 0) {
//遍历返回数据并更改每一项的内容
res.data.list.forEach((item) => {
//匹配省市的循环
for (let i = 0; i < chinaJSON.length; i++) {
if(chinaJSON[i].code==item.provinceNumber){
item.provinceNumber=chinaJSON[i].name;
var city = chinaJSON[i].cityList;
for (let j = 0; j < city.length; j++) {
if(city[j].code==item.cityNumber){
item.cityNumber=city[j].name
} }
}
}
let tempColorArr = ["黄色", "蓝色", "绿色"];
item.carNumColor = tempColorArr[item.carNumColor];
item.ownerSex = item.ownerSex === 0 ? "男" : "女";
item.industryMainType =
item.industryMainType === 0 ? "其他" : "货运";
item.industrySecondaryType =
item.industrySecondaryType === 0 ? "其他" : "货运";
if (item.carMainType === 0) {
item.carMainType = "其他";
}
if (item.carSecondaryType === 0) {
item.carSecondaryType = "其他";
}
if (item.registerType === 0) {
item.registerType = "其他";
}
if (item.terminalType === 0) {
item.terminalType = "其他";
}
item.deviceType = item.deviceType === 0 ? "非视频" : "视频";
});
that.tableData = res.data.list;
that.totalCount = res.data.totalSize;
that.totalPage = res.data.totalPages;
上面是获取到的数据进行渲染 之后进行编辑 并提交时要将文字再次切换为数字进行提交
handleEditSubmit() {
var that = this;
console.log("handleEditSubmit: " + JSON.stringify(that.editFormData));
console.log(that.editFormData);
console.log(Object.keys(that.editFormData));
for (let i = 0; i < chinaJSON.length; i++) {
if (chinaJSON[i].name == that.editFormData.provinceNumber) {
that.editFormData.provinceNumber = parseInt(chinaJSON[i].code);
var city = chinaJSON[i].cityList;
for (let j = 0; j < city.length; j++) {
if (city[j].name == that.editFormData.cityNumber) {
that.editFormData.cityNumber = parseInt(city[j].code);
}
}
}
}
let tempColorArr = ["黄色", "蓝色", "绿色"];
that.editFormData.carNumColor =tempColorArr.map(item => item).indexOf(that.editFormData.carNumColor);
that.editFormData.ownerSex = that.editFormData.ownerSex === "男" ? 0 : 1;
that.editFormData.industryMainType = that.editFormData.industryMainType === "其他" ? 0 : 1;
that.editFormData.industrySecondaryType =
that.editFormData.industrySecondaryType === "其他" ? 0 : 1;
if (that.editFormData.carMainType === "其他") {
that.editFormData.carMainType = 0;
}
if (that.editFormData.carSecondaryType === "其他") {
that.editFormData.carSecondaryType = 0;
}
if (that.editFormData.registerType === "其他") {
that.editFormData.registerType = 0;
}
if (that.editFormData.terminalType === "其他") {
that.editFormData.terminalType = 0;
}
that.editFormData.deviceType = that.editFormData.deviceType === "非视频" ? 0 : 1;
request({
url: "/api-car/car/update",
method: "put",
data: that.editFormData,
})
.then((res) => {
console.log("/api-car/car/update");
console.log(res);
if (res.success) {
this.editDialogVisible.show = false;
if (that.inputKey.length > 0) {
that.handleClickSearch();
} else {
that.handleRequestData();
}
that.$message({
message: "修改成功",
type: "success",
});
} else {
// this.$message({
// message: "获取失败",
// type: "warning",
// });
}
})
.catch((err) => {
// this.$message({
// message: "网络错误",
// type: "warning",
// });
console.log(err);
});
},