el-Cascader二次封装,不影响原来组件的属性

7 篇文章 0 订阅
<template>
  <div id="expense-type-id" :style="`width:${width}`">
    <el-cascader
      v-model="selectData"
      ref="expenseCascader"
      v-bind="_newAttrs"
      size="mini"
      v-on="_newListeners"
      :props="_newAttrs.props"
      :options="reimbursementCategoryAll"
      >
    </el-cascader>
  </div>
</template>

<script>
let ROW_ID_MAP = {} // rowId对应名称的集合
export default {
  name: 'expense-type',
  model: {
    prop: 'values',
    event: 'change'
  },
  props: {
    values: {
      type: Array,
      default: () => {
        return []
      }
    },
    width: {
      type: String,
      default: '300px'
    },
    urlParams: {
      type: Object,
      default: () => {
        return {
          disconnected: false // true停用,false未停用
        }
      }
    }
  },
  data() {
    return {
      reimbursementCategoryAll: [],
      selectData: []
    }
  },
  computed: {
    _newAttrs() {
      const newAttrs = { ...this.$attrs }
      return newAttrs
    },
    _newListeners() {
      if (Object.keys(this.$listeners).length > 0) {
        return Object.assign(this.$listeners, {
         //在这里覆盖原有的change事件
          change: this.handleChange
        })
      }
      return {}
    }
  },
  mounted() {
    this.selectData = this.values
    this.getReimbursementType()
  },
  destroyed() {
    ROW_ID_MAP = {}
  },
  methods: {
    getCascader(){
      return this.$refs.expenseCascader
    },
    /**
     * @description: 数据选中/取消监听的事件
     * @param {Array} val 选中的二维数组,[['父级ID', '子级ID']]
     */
    handleChange(val) {
      const objName = this.getDataName(val)
      this.$emit('change', val, objName)
    },
    // 请求费用类型数据
    getReimbursementType() {
      let url = ''
      this.$http.post(url, this.urlParams).then(res => {
        if (res.data.success) {
          const newResData = []
          this.handleReorganizingData(res?.data?.data || [], newResData)
          this.reimbursementCategoryAll = newResData
        } else {
          this.$message({ type:'error', message:res.data.msg })
        }
      }).catch(error => {
        console.log(error)
      })
    },
    /**
     * @description: 重新组合数据格式
     * @param {Array} data 需要处理的数据
     * @param {Array} newData 最终处理的数据格式
     */
    handleReorganizingData(data, newData) {
      if (data && data.length > 0) {
        data.forEach((item) => {
          const { rowId, title, childern } = item,
                newItem = {
                  value: rowId,
                  label: title,
                  children: null
                }
          ROW_ID_MAP[rowId] = title
          
          newData.push(newItem)
          if (childern && childern.length > 0) {
            newItem.children = []
            this.handleReorganizingData(childern, newItem.children)
          }
        })
      }
    },
    /**
     * @description: 根据rowId查找对应的名称,显示用
     * @param {Array} data
     * @return {Object} 参数1:返回二维数组,名称跟ID数组格式匹配,参数2:所有名称的拼接
     */
    getDataName(data) {
      const { props } = this._newAttrs;
      const typeNames = [],
            twoDimensionalName = []
      if (data && data.length > 0) {
        if (props.multiple) {
          data.forEach(items => {
            const parentName = ROW_ID_MAP[items[0]],
                  childName = ROW_ID_MAP[items[1]]
            typeNames.push(`${parentName}/${childName}`)
            twoDimensionalName.push([parentName, childName])
          })
        } else {
          const parentName = ROW_ID_MAP[data[0]],
                childName = ROW_ID_MAP[data[1]]
          typeNames.push(`${parentName}/${childName}`)
          twoDimensionalName.push([parentName, childName])
        }
      }
      return {
        typeNameStr: typeNames.join(','),
        twoDimensionalName
      }
    }
  }
}
</script>

<style lang="stylus">
#expense-type-id{
  .el-cascader{
    width: 100%;
  }
}
</style>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值