Blame view

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

<script>
  //option {label:,value:}
15
16
  import { getAction } from '@api/manage'
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  export default {
    name: 'JSelectMultiple',
    props: {
      placeholder:{
        type: String,
        default:'',
        required: false
      },
      value:{
        type: String,
        required: false
      },
      readOnly:{
        type: Boolean,
        required: false,
        default: false
      },
      options:{
        type: Array,
36
37
        default:()=>[],
        required: false
38
39
40
41
42
      },
      triggerChange:{
        type: Boolean,
        required: false,
        default: false
43
44
45
46
47
48
49
50
51
52
53
      },
      spliter:{
        type: String,
        required: false,
        default: ','
      },
      popContainer:{
        type:String,
        default:'',
        required:false
      },
54
55
56
57
      dictCode:{
        type:String,
        required:false
      },
58
59
60
    },
    data(){
      return {
61
62
        arrayValue:!this.value?[]:this.value.split(this.spliter),
        dictOptions: [],
63
64
      }
    },
65
66
67
68
69
    computed:{
      selectOptions(){
        return this.dictOptions.length > 0 ? this.dictOptions : this.options
      },
    },
70
71
72
73
74
    watch:{
      value (val) {
        if(!val){
          this.arrayValue = []
        }else{
75
          this.arrayValue = this.value.split(this.spliter)
76
77
78
        }
      }
    },
79
80
81
82
83
    mounted(){
      if (this.dictCode) {
        this.loadDictOptions()
      }
    },
84
85
86
    methods:{
      onChange (selectedValue) {
        if(this.triggerChange){
87
          this.$emit('change', selectedValue.join(this.spliter));
88
        }else{
89
          this.$emit('input', selectedValue.join(this.spliter));
90
91
        }
      },
92
93
94
95
96
97
      getParentContainer(node){
        if(!this.popContainer){
          return node.parentNode
        }else{
          return document.querySelector(this.popContainer)
        }
98
99
100
101
102
103
104
105
106
107
108
109
      },
      // 根据字典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 = []
          }
        })
      },
110
111
    },
112
113
  }
</script>