jinlin
2024-02-23 1772fc5e211f9e9e0ab4cdc6c29b436aac178c2a
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<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>
      <div v-if="showType === 'radio'">
        <el-radio-group :value="dictValue" @input="$emit('input', $event)" :disabled="disabled">
          <el-radio :label="data.dictValue" v-for="data in dicts" :key="data.dictValue">{{data.dictLabel}}</el-radio>
          <el-input class="radio-input" v-if="isOtherOptionSelected" v-model="inputName"></el-input>
        </el-radio-group>
      </div>
      <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: {
      inputName:{},
      origin:{}, // 字典字段
      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 + ''),
        isName:false,
      }
    },
    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
      },
      isOtherOptionSelected() {
        const selectedOption = this.dicts.find(data => data.dictValue === this.dictValue);
        return selectedOption && selectedOption.dictLabel.includes('其他');
      },
      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 + '')
      },
      inputName(){
        this.$emit('getChangeInputData',this.inputName,this.origin)
      }
    },
    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;
  }
  .radio-input.el-input{
    position: absolute;
    bottom: 5px;
    left: 68px;
  }
  .radio-input.el-input>.el-input__inner{
    height: 25px;
  }
</style>