Tree.vue 2.88 KB
<template>
  <section class="rc-tree">
    <el-tree
      :data="treeOptions"
      :show-checkbox="showCheckbox"
      :props="defaultProps"
      @check-change="handleCheckChange"
    />
  </section>
</template>
<script>
import { apiMap } from '../../fieldConfig/apis'
export default {
  name: 'RcTree',
  components: {},
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: Array,
      default: () => []
    },
    options: {
      type: Array,
      default() {
        return [
          {
            value: '1',
            label: '选项一'
          }
        ]
      }
    },
    defaultProps: {
      type: Object,
      default() {
        return {
          children: 'children',
          label: 'label'
        }
      }
    },
    showCheckbox: {
      type: Boolean,
      default: true
    },
    optionMode: {
      type: String,
      default() {
        return 'static'
      }
    },
    optionApi: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      treeOptions: []
    }
  },
  computed: {},
  watch: {
    optionMode(val) {
      this.value = []
      switch (val) {
        case 'static': this.treeOptions = this.options; return
        case 'origin': this.originOptions(this.optionApi); return
      }
    },
    optionApi: {
      deep: true,
      handler(val) {
        this.value = []
        this.originOptions(val)
      }
    }
  },
  created() {},
  beforeMount() {
    if (this.optionMode === 'origin') {
      this.originOptions(this.optionApi)
    } else {
      this.treeOptions = this.options
    }
  },
  mounted() {
  },
  methods: {
    handleCheckChange(data, checked, indeterminate) {
    },
    originOptions(config) {
      if (config.mode === 'builtIn') {
        if (config.builtIn === '') {
          this.$message('请配置内置api接口')
        } else {
          apiMap[config.builtIn](this, '', (res) => {
            if (res.data instanceof Array) {
              const optionArray = []
              const idMapper = {}
              res.data.forEach(item => {
                idMapper[item.businessId] = {
                  id: item.businessId,
                  label: item.name,
                  children: []
                }
              })
              res.data.forEach(item => {
                if (item.parentId === undefined) {
                  optionArray.push(idMapper[item.businessId])
                } else {
                  if (idMapper[item.parentId]) {
                    idMapper[item.parentId].children.push(idMapper[item.businessId])
                  }
                }
              })
              this.treeOptions = optionArray
            } else {
              this.treeOptions = []
            }
          })
        }
        // eslint-disable-next-line no-empty
      } else {
      }
    }
  }
}
</script>
<style lang="stylus">
.rc-tree {

}
</style>