在后台管理系统里面,最常见的是就是编辑弹框,赋值,当值很多的时候,我们可以简写,而不是一长串的写出来
来来来,错误示范
在data里面去定义
formSearch: {
page: 1,
size: 10,
entName: '',
brandIds: [],
seriesId: '',
modelId: '',
status: [],
timeRange: '',
beginSearchTime: '',
endSearchTime: '',
needLoad: '',
bocStatus: [],
searchNo: '',
buyerName: '',
endUserProvince: '',
endUserCity: '',
endUserDistrict: '',
minOfferNum: '',
maxOfferNum: '',
belongName: '',
addFrom: [],
deptName: '',
bigDeptName: '',
buyerDeptCode: '',
defeatReason: ''
},
清空时去赋值
clearFilter () {
this.formSearch.page = 1
this.formSearch.entName = ''
this.formSearch.belongName = ''
this.formSearch.brandIds = []
this.formSearch.seriesId = ''
this.formSearch.modelId = ''
this.formSearch.timeRange = ''
this.formSearch.beginSearchTime = ''
this.formSearch.endSearchTime = ''
this.formSearch.needLoad = ''
this.formSearch.searchNo = ''
this.formSearch.buyerName = ''
this.formSearch.endUserProvince = ''
this.formSearch.endUserCity = ''
this.formSearch.endUserDistrict = ''
this.formSearch.minOfferNum = ''
this.formSearch.maxOfferNum = ''
this.formSearch.addFrom = []
this.formSearch.deptName = ''
this.formSearch.bigDeptName = ''
this.formSearch.buyerDeptCode = ''
this.formSearch.defeatReason = ''
this.getTargetCarList()
localStorage.removeItem('initTime')
},
是不是看不下去?
下面示范正确的
1.首先我们可以在data的return外面去定义一个常量,然后赋值
let defaultAddParams = {
planName: '',
expressionName: '',
effectiveTime: '',
exclusivePlan: [],
relyPlan: [],
expiryTime: '',
expressionId: '',
expressionDescribe: '',
planDescribe: '',
planCode: '',
templateType: '',
lycTopTips: '',
lycOrderTips: '',
isLycTips: '',
czgTopTips: '',
czgOrderTips: '',
orderRemark: '',
marketingType: '',
isCzgTips: '',
planSort: '',
planPdfUrl: '',
putInType: '',
putInDesc: '',
putInPoints: '',
putInNotPoints: '',
putInRemark: '',
putInImg: '',
putInText: '',
putInIsShow: '',
expressionCn: '',
expression: '',
expressionId: ''
}
return {
defaultAddParams,
planAddParams: defaultAddParams,
}
编辑时赋值
editPlan (item) {
for (var key in this.defaultAddParams) {
this.defaultAddParams[key] = ''
}
this.defaultAddParams.exclusivePlan = []
this.defaultAddParams.relyPlan = []
Object.keys(this.defaultAddParams).forEach(key => {
item[key] ? this.planAddParams[key] = item[key] : this.planAddParams[key] = this.defaultAddParams[key]
})
this.planAddParams.expressionDescribe = ''
this.planAddParams.expressionCn = ''
}
ps:先对原始的数据可以做一个清空,再去判断这个item里面有没有返回这个值,如果有就赋值,没有用原来的值
清空
直接对数据进行forEach清空就好