Blame view

ant-design-vue-jeecg/src/components/jeecgbiz/JButtonBizComponent/JSelectBizComponentModal.vue 12.9 KB
李泰瑜 authored
1
2
3
4
5
6
7
8
9
<template>
  <j-modal
    centered
    :title="name + '选择'"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    @cancel="close"
谭毅彬 authored
10
    :cancelText="$t('button.close')"
谭毅彬 authored
11
  >
李泰瑜 authored
12
13
14
15
16
    <a-row :gutter="18">
      <a-col :span="16">
        <!-- 查询区域 -->
        <a-form layout="inline" class="j-inline-form">
          <!-- 固定条件 -->
谭毅彬 authored
17
18
19
20
21
22
          <a-form-item :label="queryParamText || name">
            <a-input
              v-model="queryParam[queryParamCode || valueKey]"
              :placeholder="'请输入' + (queryParamText || name)"
              @pressEnter="searchQuery"
            />
李泰瑜 authored
23
24
          </a-form-item>
          <!-- 动态生成的查询条件 -->
谭毅彬 authored
25
26
27
28
29
30
31
          <j-select-biz-query-item
            v-if="queryConfig.length > 0"
            v-show="showMoreQueryItems"
            :queryParam="queryParam"
            :queryConfig="queryConfig"
            @pressEnter="searchQuery"
          />
李泰瑜 authored
32
          <!-- 按钮 -->
33
34
          <a-button type="primary" @click="searchQuery" icon="search">{{ $t('button.search') }}</a-button>
          <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('button.reset') }}</a-button>
谭毅彬 authored
35
          <a v-if="queryConfig.length > 0" @click="showMoreQueryItems = !showMoreQueryItems" style="margin-left: 8px">
李泰瑜 authored
36
            {{ showMoreQueryItems ? '收起' : '展开' }}
谭毅彬 authored
37
            <a-icon :type="showMoreQueryItems ? 'up' : 'down'" />
李泰瑜 authored
38
39
40
41
42
43
44
45
46
47
48
49
          </a>
        </a-form>

        <a-table
          size="middle"
          bordered
          :rowKey="rowKey"
          :columns="innerColumns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :scroll="{ y: 240 }"
谭毅彬 authored
50
          :rowSelection="{ selectedRowKeys, onChange: onSelectChange, type: multiple ? 'checkbox' : 'radio' }"
李泰瑜 authored
51
          :customRow="customRowFn"
谭毅彬 authored
52
53
54
55
56
57
58
          @change="handleTableChange"
        >
          <span slot="zoneCode" slot-scope="zoneCode">
            <a-tag :key="zoneCode" color="blue">
              {{ solutionZoneCode(zoneCode) }}
            </a-tag>
          </span>
李泰瑜 authored
59
60
61
        </a-table>
      </a-col>
      <a-col :span="8">
谭毅彬 authored
62
        <a-card :title="'已选' + name" :bordered="false" :head-style="{ padding: 0 }" :body-style="{ padding: 0 }">
李泰瑜 authored
63
          <a-table size="middle" :rowKey="rowKey" bordered v-bind="selectedTable">
谭毅彬 authored
64
            <span slot="action" slot-scope="text, record, index">
65
              <a @click="handleDeleteSelected(record, index)">{{$t('button.delete')}}</a>
谭毅彬 authored
66
            </span>
李泰瑜 authored
67
68
69
70
          </a-table>
        </a-card>
      </a-col>
    </a-row>
谭毅彬 authored
71
    <InventoryDetailSelectList />
李泰瑜 authored
72
73
74
75
  </j-modal>
</template>

<script>
谭毅彬 authored
76
import { getAction } from '@/api/manage'
李泰瑜 authored
77
import Ellipsis from '@/components/Ellipsis'
谭毅彬 authored
78
79
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { pushIfNotExist } from '@/utils/util'
李泰瑜 authored
80
import JSelectBizQueryItem from './JSelectBizQueryItem'
谭毅彬 authored
81
82
83
84
import { cloneDeep } from 'lodash'
import { stockTakeTask } from '../../../api/api'
import Utils from './util.js'
import InventoryDetailSelectList from '../../../views/system/inventory/InventoryDetailSelectList'
85
import { translateResultMessage } from '@/api/api'
李泰瑜 authored
86
87
88
89

export default {
  name: 'JSelectBizComponentModal',
  mixins: [JeecgListMixin],
谭毅彬 authored
90
  components: { Ellipsis, JSelectBizQueryItem, InventoryDetailSelectList },
李泰瑜 authored
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
142
143
144
145
146
147
148
149
150
  props: {
    value: {
      type: Array,
      default: () => []
    },
    visible: {
      type: Boolean,
      default: false
    },
    valueKey: {
      type: String,
      required: true
    },
    multiple: {
      type: Boolean,
      default: true
    },
    width: {
      type: Number,
      default: 900
    },

    name: {
      type: String,
      default: ''
    },
    listUrl: {
      type: String,
      required: true,
      default: ''
    },
    // 根据 value 获取显示文本的地址,例如存的是 username,可以通过该地址获取到 realname
    valueUrl: {
      type: String,
      default: ''
    },
    displayKey: {
      type: String,
      default: null
    },
    columns: {
      type: Array,
      required: true,
      default: () => []
    },
    // 查询条件Code
    queryParamCode: {
      type: String,
      default: null
    },
    // 查询条件文字
    queryParamText: {
      type: String,
      default: null
    },
    // 查询配置
    queryConfig: {
      type: Array,
      default: () => []
    },
谭毅彬 authored
151
152
    testConfig: '',
    headerCode: '',
李泰瑜 authored
153
154
155
156
157
158
159
160
    rowKey: {
      type: String,
      default: 'id'
    },
    // 过长裁剪长度,设置为 -1 代表不裁剪
    ellipsisLength: {
      type: Number,
      default: 12
谭毅彬 authored
161
    }
李泰瑜 authored
162
163
164
165
166
167
168
  },
  data() {
    return {
      innerValue: [],
      // 已选择列表
      selectedTable: {
        pagination: false,
谭毅彬 authored
169
        scroll: { y: 240 },
李泰瑜 authored
170
171
        columns: [
          {
172
            ...this.columns[3],
谭毅彬 authored
173
            width: this.columns[0].widthRight || this.columns[0].width
李泰瑜 authored
174
          },
175
          { title: this.$t('system.options'), dataIndex: 'action', align: 'center', width: 60, scopedSlots: { customRender: 'action' } }
李泰瑜 authored
176
        ],
谭毅彬 authored
177
        dataSource: []
李泰瑜 authored
178
      },
谭毅彬 authored
179
180
      renderEllipsis: value => <ellipsis length={this.ellipsisLength}>{value}</ellipsis>,
      url: { list: this.listUrl },
李泰瑜 authored
181
182
183
184
185
186
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ['5', '10', '20', '30'],
        showTotal: (total, range) => {
187
          return range[0] + '-' + range[1] + " " + this.$t('list.showing') + " " + total + " " + this.$t('list.records')
李泰瑜 authored
188
189
190
191
192
193
194
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      options: [],
      dataSourceMap: {},
谭毅彬 authored
195
      showMoreQueryItems: false
李泰瑜 authored
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
    }
  },
  computed: {
    // 表头
    innerColumns() {
      let columns = cloneDeep(this.columns)
      columns.forEach(column => {
        // 给所有的列加上过长裁剪
        if (this.ellipsisLength !== -1) {
          // JSelectBizComponent columns 建议开放customRender等方法类配置
          // https://github.com/jeecgboot/jeecg-boot/issues/3203
          let myCustomRender = column.customRender
          column.customRender = (text, record, index) => {
            let value = text
            if (typeof myCustomRender === 'function') {
              // noinspection JSVoidFunctionReturnValueUsed
              value = myCustomRender(text, record, index)
            }
            if (typeof value === 'string') {
              return this.renderEllipsis(value)
            }
            return value
          }
        }
      })
      return columns
谭毅彬 authored
222
    }
李泰瑜 authored
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  },
  watch: {
    value: {
      deep: true,
      immediate: true,
      handler(val) {
        this.innerValue = cloneDeep(val)
        this.selectedRowKeys = []
        this.valueWatchHandler(val)
        this.queryOptionsByValue(val)
      }
    },
    dataSource: {
      deep: true,
      handler(val) {
        this.emitOptions(val)
        this.valueWatchHandler(this.innerValue)
      }
    },
    selectedRowKeys: {
      immediate: true,
      deep: true,
      handler(val) {
        //update--begin--autor:scott-----date:20200927------for:选取职务名称出现全选 #1753-----
        if (this.innerValue) {
谭毅彬 authored
248
          this.innerValue.length = 0
李泰瑜 authored
249
250
251
252
253
        }
        //update--end--autor:scott-----date:20200927------for:选取职务名称出现全选 #1753-----
        this.selectedTable.dataSource = val.map(key => {
          for (let data of this.dataSource) {
            if (data[this.rowKey] === key) {
谭毅彬 authored
254
              Utils.$emit('methodA', data[this.valueKey])
李泰瑜 authored
255
256
257
258
259
260
261
262
263
264
265
266
267
              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 {}
        })
谭毅彬 authored
268
      }
李泰瑜 authored
269
270
271
272
    }
  },

  methods: {
谭毅彬 authored
273
274
275
276
277
278
279
280
281
282
    solutionZoneCode(value) {
      var actions = []
      Object.keys(this.zoneList).some(key => {
        if (this.zoneList[key].code == '' + value) {
          actions.push(this.zoneList[key].name)
          return true
        }
      })
      return actions.join('')
    },
李泰瑜 authored
283
284
285
    /** 关闭弹窗 */
    close() {
      this.$emit('update:visible', false)
谭毅彬 authored
286
      this.handleDeleteSelected(this.selectedTable.dataSource, 0)
李泰瑜 authored
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    },

    valueWatchHandler(val) {
      val.forEach(item => {
        this.dataSource.concat(this.selectedTable.dataSource).forEach(data => {
          if (data[this.valueKey] === item) {
            pushIfNotExist(this.selectedRowKeys, data[this.rowKey])
          }
        })
      })
    },

    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
        }
      }
谭毅彬 authored
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
      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)
            })
李泰瑜 authored
335
          }
谭毅彬 authored
336
337
        })
      }
李泰瑜 authored
338
339
340
341
342
343
    },

    emitOptions(dataSource, callback) {
      dataSource.forEach(data => {
        let key = data[this.valueKey]
        this.dataSourceMap[key] = data
谭毅彬 authored
344
        pushIfNotExist(this.options, { label: data[this.displayKey || this.valueKey], value: key }, 'value')
李泰瑜 authored
345
346
347
348
349
350
351
352
        typeof callback === 'function' ? callback(data) : ''
      })
      this.$emit('options', this.options, this.dataSourceMap)
    },

    /** 完成选择 */
    handleOk() {
      let value = this.selectedTable.dataSource.map(data => data[this.valueKey])
谭毅彬 authored
353
354
355
356
      if (value.length >= 1) {
        stockTakeTask(value, this.testConfig, this.headerCode)
          .then(res => {
            if (res.success) {
357
              this.$message.success(translateResultMessage(res, res.message))
谭毅彬 authored
358
359
360
361
              this.$emit('ok')
              this.close()
              Utils.$emit('methodB', res.result)
            } else {
362
              this.$message.warning(translateResultMessage(res, res.message))
谭毅彬 authored
363
364
365
366
            }
          })
          .finally(() => {
            this.confirmLoading = false
367
            this.close()
谭毅彬 authored
368
369
370
          })
      } else {
        alert('至少选择一项')
李泰瑜 authored
371
      }
谭毅彬 authored
372
      this.handleDeleteSelected(this.selectedTable.dataSource, 0)
李泰瑜 authored
373
374
375
    },
    /** 删除已选择的 */
    handleDeleteSelected(record, index) {
谭毅彬 authored
376
      Utils.$emit('methodA', 10)
李泰瑜 authored
377
378
379
380
      this.selectedRowKeys.splice(this.selectedRowKeys.indexOf(record[this.rowKey]), 1)
      //update--begin--autor:wangshuai-----date:20200722------for:JSelectBizComponent组件切换页数值问题------
      this.selectedTable.dataSource.splice(this.selectedTable.dataSource.indexOf(record), 1)
      this.innerValue.splice(this.innerValue.indexOf(record[this.valueKey]), 1)
谭毅彬 authored
381
382
      console.log('this.selectedRowKeys:', this.selectedRowKeys)
      console.log('this.selectedTable.dataSource:', this.selectedTable.dataSource)
李泰瑜 authored
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
      //update--begin--autor:wangshuai-----date:20200722------for:JSelectBizComponent组件切换页数值问题------
    },

    customRowFn(record) {
      return {
        on: {
          click: () => {
            let key = record[this.rowKey]
            if (!this.multiple) {
              this.selectedRowKeys = [key]
              this.selectedTable.dataSource = [record]
            } else {
              let index = this.selectedRowKeys.indexOf(key)
              if (index === -1) {
                this.selectedRowKeys.push(key)
                this.selectedTable.dataSource.push(record)
              } else {
                this.handleDeleteSelected(record, index)
              }
            }
          }
        }
      }
谭毅彬 authored
406
    }
李泰瑜 authored
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
  }
}
</script>
<style lang="less" scoped>
.full-form-item {
  display: flex;
  margin-right: 0;

  /deep/ .ant-form-item-control-wrapper {
    flex: 1 1;
    display: inline-block;
  }
}

.j-inline-form {
  /deep/ .ant-form-item {
    margin-bottom: 12px;
  }

  /deep/ .ant-form-item-label {
    line-height: 32px;
    width: auto;
  }

  /deep/ .ant-form-item-control {
    height: 32px;
    line-height: 32px;
  }
}
</style>