zoukankan      html  css  js  c++  java
  • 赋值清空的简写

    在后台管理系统里面,最常见的是就是编辑弹框,赋值,当值很多的时候,我们可以简写,而不是一长串的写出来
    来来来,错误示范
    在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清空就好

  • 相关阅读:
    转载:从git仓库中删除不必要的文件
    问题:Swiper父容器隐藏时实例化组件,组件滑动失效
    图片预加载
    移动端苹果手机:图片没有加载完成前,白色边框线是怎么来的
    bower 安装依赖提示 EINVRES Request to https://bower.herokuapp.com/packages/xxx failed with 502
    H5序列帧播放
    盟军敢死队
    二维游戏开发的点滴
    用c语言开发游戏 快乐的痛 笑着哭
    ibatis
  • 原文地址:https://www.cnblogs.com/antyhouse/p/10931453.html
Copyright © 2011-2022 走看看