Blame view

ant-design-vue-jeecg/src/components/jeecg/JSelectMultiple.vue 2.41 KB
肖超群 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
<template>
  <a-select :value="arrayValue" @change="onChange" mode="multiple" :placeholder="placeholder" allowClear>
    <a-select-option
      v-for="(item,index) in selectOptions"
      :key="index"
      :getPopupContainer="getParentContainer"
      :value="item.value">
      {{ item.text || item.label }}
    </a-select-option>
  </a-select>
</template>

<script>
肖超群 authored
14
15
//option {label:,value:}
import {getAction} from '@api/manage'
肖超群 authored
16
肖超群 authored
17
18
19
20
21
22
23
export default {
  name: 'JSelectMultiple',
  props: {
    placeholder: {
      type: String,
      default: '',
      required: false
肖超群 authored
24
    },
肖超群 authored
25
26
27
    value: {
      type: String,
      required: false
肖超群 authored
28
    },
肖超群 authored
29
30
31
32
    readOnly: {
      type: Boolean,
      required: false,
      default: false
肖超群 authored
33
    },
肖超群 authored
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
    options: {
      type: Array,
      default: () => [],
      required: false
    },
    triggerChange: {
      type: Boolean,
      required: false,
      default: false
    },
    spliter: {
      type: String,
      required: false,
      default: ','
    },
    popContainer: {
      type: String,
      default: '',
      required: false
    },
    dictCode: {
      type: String,
      required: false
    },
  },
  data() {
    return {
      arrayValue: !this.value ? [] : this.value.split(this.spliter),
      dictOptions: [],
    }
  },
  computed: {
    selectOptions() {
      return this.dictOptions.length > 0 ? this.dictOptions : this.options
    },
  },
  watch: {
    value(val) {
      if (!val) {
        this.arrayValue = []
      } else {
        this.arrayValue = this.value.split(this.spliter)
      }
    }
  },
  mounted() {
    if (this.dictCode) {
      this.loadDictOptions()
    }
  },
  methods: {
    onChange(selectedValue) {
      if (this.triggerChange) {
        this.$emit('change', selectedValue.join(this.spliter));
      } else {
        this.$emit('input', selectedValue.join(this.spliter));
肖超群 authored
90
91
      }
    },
肖超群 authored
92
93
94
95
96
    getParentContainer(node) {
      if (!this.popContainer) {
        return node.parentNode
      } else {
        return document.querySelector(this.popContainer)
肖超群 authored
97
98
      }
    },
肖超群 authored
99
100
101
102
103
104
105
106
    // 根据字典code查询字典项
    loadDictOptions() {
      getAction(`/sys/dict/getDictItems/${this.dictCode}`, {}).then(res => {
        if (res.success) {
          this.dictOptions = res.result.map(item => ({value: item.value, label: item.text}))
        } else {
          console.error('getDictItems error: : ', res)
          this.dictOptions = []
肖超群 authored
107
        }
肖超群 authored
108
      })
肖超群 authored
109
    },
肖超群 authored
110
  },
肖超群 authored
111
肖超群 authored
112
}
肖超群 authored
113
</script>