JSelectBizComponentModal.vue 9.5 KB
Newer Older
1 2 3 4
<template>
  <a-modal
    centered
    :title="name + '选择'"
JEECG低代码平台's avatar
JEECG低代码平台 已提交
5
    :width="width"
6 7 8 9 10 11 12 13 14 15 16 17 18 19
    :visible="visible"
    @ok="handleOk"
    @cancel="close"
    cancelText="关闭">

    <a-row :gutter="18">
      <a-col :span="16">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="24">

              <a-col :span="14">
                <a-form-item :label="(queryParamText||name)">
JEECG低代码平台's avatar
JEECG低代码平台 已提交
20
                  <a-input v-model="queryParam[queryParamCode||valueKey]" :placeholder="'请输入' + (queryParamText||name)" @pressEnter="searchQuery"/>
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
                </a-form-item>
              </a-col>
              <a-col :span="8">
                  <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                    <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                    <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                  </span>
              </a-col>

            </a-row>
          </a-form>
        </div>

        <a-table
          size="small"
          bordered
JEECG低代码平台's avatar
JEECG低代码平台 已提交
37 38
          :rowKey="rowKey"
          :columns="innerColumns"
39 40 41 42 43 44 45 46 47 48 49 50 51
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :scroll="{ y: 240 }"
          :rowSelection="{selectedRowKeys, onChange: onSelectChange, type: multiple ? 'checkbox':'radio'}"
          :customRow="customRowFn"
          @change="handleTableChange">
        </a-table>

      </a-col>
      <a-col :span="8">
        <a-card :title="'已选' + name" :bordered="false" :head-style="{padding:0}" :body-style="{padding:0}">

JEECG低代码平台's avatar
JEECG低代码平台 已提交
52
          <a-table size="small" :rowKey="rowKey" bordered v-bind="selectedTable">
53 54 55 56 57 58 59 60 61 62 63 64
              <span slot="action" slot-scope="text, record, index">
                <a @click="handleDeleteSelected(record, index)">删除</a>
              </span>
          </a-table>

        </a-card>
      </a-col>
    </a-row>
  </a-modal>
</template>

<script>
JEECG低代码平台's avatar
JEECG低代码平台 已提交
65
  import { getAction } from '@/api/manage'
66
  import Ellipsis from '@/components/Ellipsis'
67
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
JEECG低代码平台's avatar
JEECG低代码平台 已提交
68
  import { cloneObject, pushIfNotExist } from '@/utils/util'
69 70 71 72

  export default {
    name: 'JSelectBizComponentModal',
    mixins: [JeecgListMixin],
73
    components: { Ellipsis },
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    props: {
      value: {
        type: Array,
        default: () => []
      },
      visible: {
        type: Boolean,
        default: false
      },
      valueKey: {
        type: String,
        required: true
      },
      multiple: {
        type: Boolean,
        default: true
      },
JEECG低代码平台's avatar
JEECG低代码平台 已提交
91 92 93 94
      width: {
        type: Number,
        default: 900
      },
95 96 97 98 99 100 101 102 103 104

      name: {
        type: String,
        default: ''
      },
      listUrl: {
        type: String,
        required: true,
        default: ''
      },
JEECG低代码平台's avatar
JEECG低代码平台 已提交
105 106 107 108 109
      // 根据 value 获取显示文本的地址,例如存的是 username,可以通过该地址获取到 realname
      valueUrl: {
        type: String,
        default: ''
      },
110 111 112 113
      displayKey: {
        type: String,
        default: null
      },
JEECG低代码平台's avatar
JEECG低代码平台 已提交
114
      columns: {
115
        type: Array,
JEECG低代码平台's avatar
JEECG低代码平台 已提交
116
        required: true,
117 118
        default: () => []
      },
JEECG低代码平台's avatar
JEECG低代码平台 已提交
119 120 121 122 123
      // 查询条件Code
      queryParamCode: {
        type: String,
        default: null
      },
124 125 126 127 128
      // 查询条件文字
      queryParamText: {
        type: String,
        default: null
      },
JEECG低代码平台's avatar
JEECG低代码平台 已提交
129 130 131 132
      rowKey: {
        type: String,
        default: 'id'
      },
133 134 135 136 137
      // 过长裁剪长度,设置为 -1 代表不裁剪
      ellipsisLength: {
        type: Number,
        default: 12
      },
138 139 140
    },
    data() {
      return {
JEECG低代码平台's avatar
JEECG低代码平台 已提交
141
        innerValue: [],
142 143 144 145 146
        // 已选择列表
        selectedTable: {
          pagination: false,
          scroll: { y: 240 },
          columns: [
JEECG低代码平台's avatar
JEECG低代码平台 已提交
147 148 149 150
            {
              ...this.columns[0],
              width: this.columns[0].widthRight || this.columns[0].width,
            },
151 152 153 154
            { title: '操作', dataIndex: 'action', align: 'center', width: 60, scopedSlots: { customRender: 'action' }, }
          ],
          dataSource: [],
        },
155
        renderEllipsis: (value) => (<ellipsis length={this.ellipsisLength}>{value}</ellipsis>),
JEECG低代码平台's avatar
JEECG低代码平台 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        url: { list: this.listUrl },
        /* 分页参数 */
        ipagination: {
          current: 1,
          pageSize: 5,
          pageSizeOptions: ['5', '10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + '' + total + ''
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        options: [],
        dataSourceMap: {},
171 172
      }
    },
173 174 175 176 177 178 179 180 181 182 183 184 185
    computed: {
      // 表头
      innerColumns() {
        let columns = cloneObject(this.columns)
        columns.forEach(column => {
          // 给所有的列加上过长裁剪
          if (this.ellipsisLength !== -1) {
            column.customRender = (text) => this.renderEllipsis(text)
          }
        })
        return columns
      },
    },
186 187
    watch: {
      value: {
JEECG低代码平台's avatar
JEECG低代码平台 已提交
188
        deep: true,
189 190
        immediate: true,
        handler(val) {
JEECG低代码平台's avatar
JEECG低代码平台 已提交
191 192
          this.innerValue = cloneObject(val)
          this.selectedRowKeys = []
193
          this.valueWatchHandler(val)
JEECG低代码平台's avatar
JEECG低代码平台 已提交
194
          this.queryOptionsByValue(val)
195 196 197 198 199
        }
      },
      dataSource: {
        deep: true,
        handler(val) {
JEECG低代码平台's avatar
JEECG低代码平台 已提交
200 201
          this.emitOptions(val)
          this.valueWatchHandler(this.innerValue)
202 203
        }
      },
JEECG低代码平台's avatar
JEECG低代码平台 已提交
204
      selectedRowKeys: {
205 206 207
        immediate: true,
        deep: true,
        handler(val) {
JEECG低代码平台's avatar
JEECG低代码平台 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
          this.selectedTable.dataSource = val.map(key => {
            for (let data of this.dataSource) {
              if (data[this.rowKey] === key) {
                pushIfNotExist(this.innerValue, data[this.valueKey])
                return data
              }
            }
            for (let data of this.selectedTable.dataSource) {
              if (data[this.rowKey] === key) {
                pushIfNotExist(this.innerValue, data[this.valueKey])
                return data
              }
            }
            console.warn('未找到选择的行信息,key:' + key)
            return {}
          })
224
        },
JEECG低代码平台's avatar
JEECG低代码平台 已提交
225
      }
226 227 228 229 230 231 232 233 234 235 236
    },

    methods: {

      /** 关闭弹窗 */
      close() {
        this.$emit('update:visible', false)
      },

      valueWatchHandler(val) {
        val.forEach(item => {
JEECG低代码平台's avatar
JEECG低代码平台 已提交
237
          this.dataSource.concat(this.selectedTable.dataSource).forEach(data => {
238
            if (data[this.valueKey] === item) {
JEECG低代码平台's avatar
JEECG低代码平台 已提交
239
              pushIfNotExist(this.selectedRowKeys, data[this.rowKey])
240 241 242
            }
          })
        })
JEECG低代码平台's avatar
JEECG低代码平台 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
      },

      queryOptionsByValue(value) {
        if (!value || value.length === 0) {
          return
        }
        // 判断options是否存在value,如果已存在数据就不再请求后台了
        let notExist = false
        for (let val of value) {
          let find = false
          for (let option of this.options) {
            if (val === option.value) {
              find = true
              break
            }
          }
          if (!find) {
            notExist = true
            break
          }
        }
        if (!notExist) return
        getAction(this.valueUrl || this.listUrl, {
          // 这里最后加一个 , 的原因是因为无论如何都要使用 in 查询,防止后台进行了模糊匹配,导致查询结果不准确
          [this.valueKey]: value.join(',') + ',',
          pageNo: 1,
          pageSize: value.length
        }).then((res) => {
          if (res.success) {
            let dataSource = res.result
            if (!(dataSource instanceof Array)) {
              dataSource = res.result.records
            }
            this.emitOptions(dataSource, (data) => {
              pushIfNotExist(this.innerValue, data[this.valueKey])
              pushIfNotExist(this.selectedRowKeys, data[this.rowKey])
              pushIfNotExist(this.selectedTable.dataSource, data, this.rowKey)
            })
          }
        })
      },

      emitOptions(dataSource, callback) {
        dataSource.forEach(data => {
          let key = data[this.valueKey]
          this.dataSourceMap[key] = data
          pushIfNotExist(this.options, { label: data[this.displayKey || this.valueKey], value: key }, 'value')
          typeof callback === 'function' ? callback(data) : ''
        })
        this.$emit('options', this.options, this.dataSourceMap)
293 294 295 296 297 298 299 300 301 302 303
      },

      /** 完成选择 */
      handleOk() {
        let value = this.selectedTable.dataSource.map(data => data[this.valueKey])
        this.$emit('input', value)
        this.close()
      },

      /** 删除已选择的 */
      handleDeleteSelected(record, index) {
JEECG低代码平台's avatar
JEECG低代码平台 已提交
304
        this.selectedRowKeys.splice(this.selectedRowKeys.indexOf(record[this.rowKey]), 1)
305 306 307 308
        this.selectedTable.dataSource.splice(index, 1)
      },

      customRowFn(record) {
JEECG低代码平台's avatar
JEECG低代码平台 已提交
309 310 311 312 313 314
        return {
          on: {
            click: () => {
              let key = record[this.rowKey]
              if (!this.multiple) {
                this.selectedRowKeys = [key]
315
                this.selectedTable.dataSource = [record]
JEECG低代码平台's avatar
JEECG低代码平台 已提交
316 317 318 319 320 321 322 323
              } else {
                let index = this.selectedRowKeys.indexOf(key)
                if (index === -1) {
                  this.selectedRowKeys.push(key)
                  this.selectedTable.dataSource.push(record)
                } else {
                  this.handleDeleteSelected(record, index)
                }
324 325 326 327 328 329 330 331 332 333 334
              }
            }
          }
        }
      },

    }
  }
</script>
<style lang="less" scoped>
</style>