zzw
2023-11-20 c1314fc66be416fb4e7f8ef69a4734493fd802ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<template>
    <div>
      <el-select :value="dictValue" @change="changeProjectMajor" @input="$emit('input', $event)" :placeholder="placeholder" clearable style="width: 100%;" v-if="showType === 'select'" :disabled="disabled">
          <el-option :label="data.dictLabel" v-for="data in dicts" :key="data.dictValue" :value ="data.dictValue">{{data.dictLabel}}</el-option>
      </el-select>
      <el-radio-group :value="dictValue" @input="$emit('input', $event)" v-if="showType === 'radio'" :disabled="disabled">
        <el-radio :label="data.dictValue" v-for="data in dicts" :key="data.dictValue">{{data.dictLabel}}</el-radio>
      </el-radio-group>
      <zt-combo-tree v-model="dictValue" :datas="dictTrees" :disabled-filter="disabledFilter" @input="$emit('input', $event)" v-if="showType === 'tree'" :placeholder="placeholder"/>
    </div>
</template>
<script>
  import cloneDeep from 'lodash/cloneDeep'
 
  export default {
    name: 'ZtDict',
    props: {
      value: [Number, String, Boolean],
      dict: { // 字典类型
        type: String,
        required: true
      },
      excluded: {// 排除的
        type: Array,
        default: function () {
          return []
        }
      },
      radio: {
        type: Boolean,
        default: false
      },
      dataOnly: { // 对于字典树时,是否只能选择字典值
        type: Boolean,
        default: false
      },
      disabled: {
        type: Boolean,
        default: false
      },
      placeholder: String
    },
    data() {
      return {
        dictValue: typeof this.value === 'undefined' ? '' : (this.value + '')
      }
    },
    computed: {
      showType() {
        if (this.radio) {
          return 'radio'
        } else {
          return this.dicts.length > 0 && this.dicts[0].children ? 'tree' : 'select'
        }
      },
      dictTrees() {
        let datas = cloneDeep(this.dicts)
        this.wrapTreeId(datas)
        return datas
      },
      dicts: function () {
        let dicts = this.$store.getters.getDict(this.dict)
        if (this.excluded.length > 0) {
          let excludedArray = []
          this.excluded.forEach(value => excludedArray.push(value + ''))
          return dicts.filter(option => excludedArray.indexOf(option.dictValue + '') < 0)
        } else {
          return dicts
        }
      }
    },
    watch: {
      value(val, oldval) {
        this.dictValue = typeof val === 'undefined' ? '' : (val + '')
      }
    },
    methods: {
      wrapTreeId(dictTypes) {
        dictTypes.forEach(item => {
          if ((item.dictType || '').length > 0) { // 字典类型
            item.id = item.dictType
            item.name = item.dictName
            if (item.dataList && item.dataList.length > 0) {
              item.children = item.dataList
            }
            if (item.children && item.children.length > 0) {
              this.wrapTreeId(item.children)
            }
          } else { // 字典值
            item.id = item.dictValue
            item.name = item.dictLabel
          }
        })
      },
      changeProjectMajor(){
        this.$emit('changeProjectMajor')
      },
      disabledFilter(dict) {
        return this.dataOnly && (dict.dictType || '').length > 0
      },
      onSelected() {
 
      }
    }
  }
</script>
<style>
  .el-radio__input.is-disabled + .el-radio__label {
    color: #808080 !important;
  }
 
  .el-radio__input.is-disabled.is-checked + .el-radio__label {
    color: #00a06e !important;
  }
 
  .el-radio__input.is-disabled.is-checked .el-radio__inner {
    background-color: #00a06e !important;
    border-color: #1890ff !important;
  }
</style>