index.vue 3.8 KB
Newer Older
1 2
<template>
  <a-row class="j-select-biz-component-box" type="flex" :gutter="8">
3
    <a-col class="left" :class="{'full': !buttons}">
JEECG低代码平台's avatar
JEECG低代码平台 已提交
4 5 6 7 8 9 10 11
      <slot name="left">
        <a-select
          mode="multiple"
          :placeholder="placeholder"
          v-model="selectValue"
          :options="selectOptions"
          allowClear
          :disabled="disabled"
12
          :open="selectOpen"
JEECG低代码平台's avatar
JEECG低代码平台 已提交
13
          style="width: 100%;"
14
          @dropdownVisibleChange="handleDropdownVisibleChange"
JEECG低代码平台's avatar
JEECG低代码平台 已提交
15 16 17
          @click.native="visible=(buttons?visible:true)"
        />
      </slot>
18 19
    </a-col>

20
    <a-col v-if="buttons" class="right">
21 22 23 24 25 26
      <a-button type="primary" icon="search" :disabled="disabled" @click="visible=true">{{selectButtonText}}</a-button>
    </a-col>

    <j-select-biz-component-modal
      v-model="selectValue"
      :visible.sync="visible"
JEECG低代码平台's avatar
JEECG低代码平台 已提交
27 28
      v-bind="modalProps"
      @options="handleOptions"
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
    />
  </a-row>
</template>

<script>
  import JSelectBizComponentModal from './JSelectBizComponentModal'

  export default {
    name: 'JSelectBizComponent',
    components: { JSelectBizComponentModal },
    props: {
      value: {
        type: String,
        default: ''
      },
      /** 是否返回 id,默认 false,返回 code */
      returnId: {
        type: Boolean,
        default: false
      },
      placeholder: {
        type: String,
        default: '请选择'
      },
      disabled: {
        type: Boolean,
        default: false
      },
      // 是否支持多选,默认 true
      multiple: {
        type: Boolean,
        default: true
      },
62 63 64 65 66
      // 是否显示按钮,默认 true
      buttons: {
        type: Boolean,
        default: true
      },
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      // 显示的 Key
      displayKey: {
        type: String,
        default: null
      },
      // 返回的 key
      returnKeys: {
        type: Array,
        default: () => ['id', 'id']
      },
      // 选择按钮文字
      selectButtonText: {
        type: String,
        default: '选择'
      },

    },
    data() {
      return {
        selectValue: [],
        selectOptions: [],
JEECG低代码平台's avatar
JEECG低代码平台 已提交
88
        dataSourceMap: {},
89 90
        visible: false,
        selectOpen: false,
91 92 93 94 95
      }
    },
    computed: {
      valueKey() {
        return this.returnId ? this.returnKeys[0] : this.returnKeys[1]
JEECG低代码平台's avatar
JEECG低代码平台 已提交
96 97 98 99 100 101 102 103 104
      },
      modalProps() {
        return Object.assign({
          valueKey: this.valueKey,
          multiple: this.multiple,
          returnKeys: this.returnKeys,
          displayKey: this.displayKey || this.valueKey
        }, this.$attrs)
      },
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    },
    watch: {
      value: {
        immediate: true,
        handler(val) {
          if (val) {
            this.selectValue = val.split(',')
          } else {
            this.selectValue = []
          }
        }
      },
      selectValue: {
        deep: true,
        handler(val) {
JEECG低代码平台's avatar
JEECG低代码平台 已提交
120 121 122
          let rows = val.map(key => this.dataSourceMap[key])
          this.$emit('select', rows)
          let data = val.join(',')
123 124 125 126 127
          this.$emit('input', data)
          this.$emit('change', data)
        }
      }
    },
JEECG低代码平台's avatar
JEECG低代码平台 已提交
128 129 130 131 132
    methods: {
      handleOptions(options, dataSourceMap) {
        this.selectOptions = options
        this.dataSourceMap = dataSourceMap
      },
133 134 135 136 137 138 139
      handleDropdownVisibleChange() {
        // 解决antdv自己的bug —— open 设置为 false 了,点击后还是添加了 open 样式,导致点击事件失效
        this.selectOpen = true
        this.$nextTick(() => {
          this.selectOpen = false
        })
      },
JEECG低代码平台's avatar
JEECG低代码平台 已提交
140
    }
141 142 143
  }
</script>

144
<style lang="less" scoped>
145 146
  .j-select-biz-component-box {

147
    @width: 82px;
148 149

    .left {
150
      width: calc(100% - @width - 8px);
151 152 153
    }

    .right {
154
      width: @width;
155
    }
156 157 158 159

    .full {
      width: 100%;
    }
JEECG低代码平台's avatar
JEECG低代码平台 已提交
160

161
    /deep/ .ant-select-search__field {
162 163
      display: none !important;
    }
164 165
  }
</style>