@vxe-ui/plugin-render-element 4.3.1 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,729 +1,754 @@
1
- import { h, ComponentOptions } from 'vue'
2
- import { getCurrComponent, hasInputType } from '../util/comp'
3
- import XEUtils from 'xe-utils'
4
- import dayjs from 'dayjs'
5
-
6
- import type { VxeUIExport, VxeGlobalRendererHandles } from 'vxe-pc-ui'
7
- import type { VxeTableDefines, VxeColumnPropTypes } from 'vxe-table'
8
-
9
- /**
10
- * 表格 - 渲染器
11
- */
12
- export function defineTableRender (VxeUI: VxeUIExport) {
13
- function isEmptyValue (cellValue: any) {
14
- return cellValue === null || cellValue === undefined || cellValue === ''
15
- }
16
-
17
- function getOnName (type: string) {
18
- return 'on' + type.substring(0, 1).toLocaleUpperCase() + type.substring(1)
19
- }
20
-
21
- function getModelProp (renderOpts: VxeGlobalRendererHandles.RenderOptions) {
22
- return 'modelValue'
23
- }
24
-
25
- function getModelEvent (renderOpts: VxeGlobalRendererHandles.RenderOptions) {
26
- return 'update:modelValue'
27
- }
28
-
29
- function getChangeEvent (renderOpts: VxeGlobalRendererHandles.RenderOptions) {
30
- let type = 'change'
31
- switch (renderOpts.name) {
32
- case 'ElAutocomplete':
33
- type = 'select'
34
- break
35
- case 'ElInput':
36
- case 'ElInputNumber':
37
- type = 'input'
38
- break
39
- }
40
- return type
41
- }
42
-
43
- function toDayStringDate (value: any, format: string) {
44
- return dayjs(value, format).toDate()
45
- }
46
-
47
- function toDayDateString (date: any, format: string) {
48
- return dayjs(date).format(format)
49
- }
50
-
51
- function parseDate (value: any, props: { [key: string]: any }) {
52
- return value && props.valueFormat ? toDayStringDate(value, props.valueFormat) : value
53
- }
54
-
55
- function getFormatDate (value: any, props: { [key: string]: any }, defaultFormat: string) {
56
- return value ? toDayDateString(parseDate(value, props), props.format || props.valueFormat || defaultFormat) : value
57
- }
58
-
59
- function getFormatDates (values: any[], props: { [key: string]: any }, separator: string, defaultFormat: string) {
60
- return XEUtils.map(values, (date: any) => getFormatDate(date, props, defaultFormat)).join(separator)
61
- }
62
-
63
- function equalDateRange (cellValue: any, data: any, props: { [key: string]: any }, defaultFormat: string) {
64
- cellValue = getFormatDate(cellValue, props, defaultFormat)
65
- return cellValue >= getFormatDate(data[0], props, defaultFormat) && cellValue <= getFormatDate(data[1], props, defaultFormat)
66
- }
67
-
68
- function getCellEditFilterProps (renderOpts: any, params: VxeGlobalRendererHandles.RenderTableEditParams | VxeGlobalRendererHandles.RenderTableFilterParams | VxeGlobalRendererHandles.RenderTableFloatingFilterParams, value: any, defaultProps?: { [prop: string]: any }) {
69
- return XEUtils.assign({}, defaultProps, renderOpts.props, { [getModelProp(renderOpts)]: value })
70
- }
71
-
72
- function formatText (cellValue: any) {
73
- return '' + (isEmptyValue(cellValue) ? '' : cellValue)
74
- }
75
-
76
- function updateFilterChangeOption (params: VxeGlobalRendererHandles.RenderTableFilterParams | VxeGlobalRendererHandles.RenderTableFloatingFilterParams, checked: boolean, option: VxeTableDefines.FilterOption) {
77
- const { $table } = params
78
- $table.updateFilterOptionStatus(option, checked)
79
- }
80
-
81
- function saveFilterEvent (params: VxeGlobalRendererHandles.RenderTableFilterParams | VxeGlobalRendererHandles.RenderTableFloatingFilterParams) {
82
- const { $table, column } = params
83
- $table.saveFilterByEvent(new Event('change'), column)
84
- }
85
-
86
- function getCellLabelVNs (renderOpts: VxeColumnPropTypes.EditRender, params: VxeGlobalRendererHandles.RenderTableCellParams, cellLabel: any) {
87
- const { placeholder } = renderOpts
88
- return [
89
- h('span', {
90
- class: 'vxe-cell--label'
91
- }, placeholder && isEmptyValue(cellLabel)
92
- ? [
93
- h('span', {
94
- class: 'vxe-cell--placeholder'
95
- }, formatText(VxeUI.getI18n(placeholder)))
96
- ]
97
- : formatText(cellLabel))
98
- ]
99
- }
100
-
101
- const blurEvent = 'blur'
102
- const clearEvent = 'clear'
103
-
104
- /**
105
- * 组件事件处理
106
- * @param renderOpts
107
- * @param params
108
- * @param modelFunc
109
- * @param changeFunc
110
- */
111
- function getComponentOns (renderOpts: VxeGlobalRendererHandles.RenderTableCellOptions | VxeGlobalRendererHandles.RenderTableFilterOptions, params: any, eFns?: {
112
- model: (cellValue: any) => void
113
- change?: (...args: any[]) => void
114
- blur?: (...args: any[]) => void
115
- clear?: (...args: any[]) => void
116
- }, eventOns?: Record<string, any>) {
117
- const { events } = renderOpts
118
- const modelEvent = getModelEvent(renderOpts)
119
- const changeEvent = getChangeEvent(renderOpts)
120
- const { model: modelFunc, change: changeFunc, blur: blurFunc, clear: clearFunc } = eFns || {}
121
- const ons: any = {}
122
- XEUtils.objectEach(events, (func, key: any) => {
123
- ons[getOnName(key)] = function (...args: any[]) {
124
- if (!XEUtils.isFunction(func)) {
125
- console.error(`Parameter "${func}" is not a method`)
126
- }
127
- func(params, ...args)
128
- }
129
- })
130
- if (modelFunc) {
131
- ons[getOnName(modelEvent)] = function (targetEvnt: any) {
132
- modelFunc(targetEvnt)
133
- if (events && events[modelEvent]) {
134
- events[modelEvent](params, targetEvnt)
135
- }
136
- }
137
- }
138
- if (changeFunc) {
139
- ons[getOnName(changeEvent)] = function (...args: any[]) {
140
- changeFunc(...args)
141
- if (events && events[changeEvent]) {
142
- events[changeEvent](params, ...args)
143
- }
144
- }
145
- }
146
- if (blurFunc) {
147
- ons[getOnName(blurEvent)] = function (...args: any[]) {
148
- blurFunc(...args)
149
- if (events && events[blurEvent]) {
150
- events[blurEvent](params, ...args)
151
- }
152
- }
153
- }
154
- if (clearFunc) {
155
- ons[getOnName(clearEvent)] = function (...args: any[]) {
156
- clearFunc(...args)
157
- if (events && events[clearEvent]) {
158
- events[clearEvent](params, ...args)
159
- }
160
- }
161
- }
162
- return eventOns ? Object.assign(ons, eventOns) : ons
163
- }
164
-
165
- function getEditOns (renderOpts: VxeGlobalRendererHandles.RenderOptions, params: VxeGlobalRendererHandles.RenderTableEditParams) {
166
- const { $table, row, column } = params
167
- return getComponentOns(renderOpts, params, {
168
- model (value) {
169
- // 处理 model 值双向绑定
170
- XEUtils.set(row, column.field, value)
171
- },
172
- change () {
173
- // 处理 change 事件相关逻辑
174
- $table.updateStatus(params)
175
- }
176
- })
177
- }
178
-
179
- function getFilterOns (renderOpts: any, params: VxeGlobalRendererHandles.RenderTableFilterParams, option: VxeTableDefines.FilterOption, changeFunc: ((...args: any[]) => void)) {
180
- return getComponentOns(renderOpts, params, {
181
- model (value: any) {
182
- // 处理 model 值双向绑定
183
- option.data = value
184
- },
185
- change: changeFunc
186
- })
187
- }
188
-
189
- function getFloatingFilterOns (renderOpts: VxeGlobalRendererHandles.RenderTableFilterOptions, params: VxeGlobalRendererHandles.RenderTableFloatingFilterParams, option: VxeTableDefines.FilterOption) {
190
- const { $table, column } = params
191
- if (hasInputType(renderOpts)) {
192
- return getComponentOns(renderOpts, params, {
193
- model (value: any) {
194
- // 处理 model 值双向绑定
195
- option.data = value
196
- },
197
- change () {
198
- updateFilterChangeOption(params, !isEmptyValue(option.data), option)
199
- },
200
- clear () {
201
- updateFilterChangeOption(params, !isEmptyValue(option.data), option)
202
- saveFilterEvent(params)
203
- },
204
- blur () {
205
- $table.saveFilterByEvent(new Event('change'), column)
206
- }
207
- })
208
- }
209
- return getComponentOns(renderOpts, params, {
210
- model (value: any) {
211
- // 处理 model 值双向绑定
212
- option.data = value
213
- },
214
- clear () {
215
- updateFilterChangeOption(params, !isEmptyValue(option.data), option)
216
- $table.saveFilterByEvent(new Event('change'), column)
217
- },
218
- change () {
219
- updateFilterChangeOption(params, !isEmptyValue(option.data), option)
220
- $table.saveFilterByEvent(new Event('change'), column)
221
- }
222
- })
223
- }
224
-
225
- function matchCascaderData (index: number, list: any[], values: any[], labels: any[]) {
226
- const val = values[index]
227
- if (list && values.length > index) {
228
- XEUtils.each(list, (item) => {
229
- if (item.value === val) {
230
- labels.push(item.label)
231
- matchCascaderData(++index, item.children, values, labels)
232
- }
233
- })
234
- }
235
- }
236
-
237
- function getSelectCellValue (renderOpts: VxeColumnPropTypes.EditRender, params: VxeGlobalRendererHandles.RenderTableCellParams) {
238
- const { options = [], optionGroups, props = {}, optionProps = {}, optionGroupProps = {} } = renderOpts
239
- const { $table, rowid, row, column } = params
240
- const { filterable, multiple } = props
241
- const labelProp = optionProps.label || 'label'
242
- const valueProp = optionProps.value || 'value'
243
- const groupOptions = optionGroupProps.options || 'options'
244
- const cellValue = XEUtils.get(row, column.field)
245
- const colid = column.id
246
- let cellData: any
247
- if (filterable) {
248
- const { internalData } = $table
249
- const { fullAllDataRowIdData } = internalData
250
- const rest: any = fullAllDataRowIdData[rowid]
251
- if (rest) {
252
- cellData = rest.cellData
253
- if (!cellData) {
254
- cellData = rest.cellData = {}
255
- }
256
- }
257
- if (rest && cellData[colid] && cellData[colid].value === cellValue) {
258
- return cellData[colid].label
259
- }
260
- }
261
- if (!isEmptyValue(cellValue)) {
262
- const selectlabel = XEUtils.map(multiple ? cellValue : [cellValue], optionGroups
263
- ? (value) => {
264
- let selectItem: any
265
- for (let index = 0; index < optionGroups.length; index++) {
266
- selectItem = XEUtils.find(optionGroups[index][groupOptions], (item) => item[valueProp] === value)
267
- if (selectItem) {
268
- break
269
- }
270
- }
271
- return selectItem ? selectItem[labelProp] : value
272
- }
273
- : (value) => {
274
- const selectItem = XEUtils.find(options, (item) => item[valueProp] === value)
275
- return selectItem ? selectItem[labelProp] : value
276
- }).join(', ')
277
- if (cellData && options && options.length) {
278
- cellData[colid] = { value: cellValue, label: selectlabel }
279
- }
280
- return selectlabel
281
- }
282
- return ''
283
- }
284
-
285
- function getCascaderCellValue (renderOpts: VxeGlobalRendererHandles.RenderOptions, params: VxeGlobalRendererHandles.RenderTableCellParams) {
286
- const { props = {} } = renderOpts
287
- const { row, column } = params
288
- const cellValue = XEUtils.get(row, column.field)
289
- const values: any[] = cellValue || []
290
- const labels: any[] = []
291
- matchCascaderData(0, props.options, values, labels)
292
- return (props.showAllLevels === false ? labels.slice(labels.length - 1, labels.length) : labels).join(` ${props.separator || '/'} `)
293
- }
294
-
295
- function getDatePickerCellValue (renderOpts: VxeGlobalRendererHandles.RenderOptions, params: VxeGlobalRendererHandles.RenderTableCellParams | VxeGlobalRendererHandles.ExportMethodParams) {
296
- const { props = {} } = renderOpts
297
- const { row, column } = params
298
- const { rangeSeparator = '-' } = props
299
- let cellValue = XEUtils.get(row, column.field)
300
- switch (props.type) {
301
- case 'week':
302
- cellValue = getFormatDate(cellValue, props, 'YYYYwWW')
303
- break
304
- case 'month':
305
- cellValue = getFormatDate(cellValue, props, 'YYYY-MM')
306
- break
307
- case 'year':
308
- cellValue = getFormatDate(cellValue, props, 'YYYY')
309
- break
310
- case 'dates':
311
- cellValue = getFormatDates(cellValue, props, ', ', 'YYYY-MM-DD')
312
- break
313
- case 'daterange':
314
- cellValue = getFormatDates(cellValue, props, ` ${rangeSeparator} `, 'YYYY-MM-DD')
315
- break
316
- case 'datetimerange':
317
- cellValue = getFormatDates(cellValue, props, ` ${rangeSeparator} `, 'YYYY-MM-DD HH:ss:mm')
318
- break
319
- case 'monthrange':
320
- cellValue = getFormatDates(cellValue, props, ` ${rangeSeparator} `, 'YYYY-MM')
321
- break
322
- default:
323
- cellValue = getFormatDate(cellValue, props, 'YYYY-MM-DD')
324
- }
325
- return cellValue
326
- }
327
-
328
- function getTimePickerCellValue (renderOpts: VxeGlobalRendererHandles.RenderOptions, params: VxeGlobalRendererHandles.RenderTableCellParams | VxeGlobalRendererHandles.RenderTableEditParams) {
329
- const { props = {} } = renderOpts
330
- const { row, column } = params
331
- const { isRange, format = 'HH:mm:ss', rangeSeparator = '-' } = props
332
- let cellValue = XEUtils.get(row, column.field)
333
- if (cellValue && isRange) {
334
- cellValue = XEUtils.map(cellValue, (date) => toDayDateString(parseDate(date, props), format)).join(` ${rangeSeparator} `)
335
- }
336
- return toDayDateString(parseDate(cellValue, props), format)
337
- }
338
-
339
- function createEditRender (defaultProps?: { [key: string]: any }) {
340
- return function (renderOpts: VxeColumnPropTypes.EditRender & { name: string }, params: VxeGlobalRendererHandles.RenderTableEditParams) {
341
- const { row, column } = params
342
- const { name, attrs, props } = renderOpts
343
- const cellValue = XEUtils.get(row, column.field)
344
- return [
345
- h(getCurrComponent(name), {
346
- ...attrs,
347
- ...props,
348
- ...getCellEditFilterProps(renderOpts, params, cellValue, defaultProps),
349
- ...getEditOns(renderOpts, params)
350
- })
351
- ]
352
- }
353
- }
354
-
355
- function defaultButtonEditRender (renderOpts: VxeColumnPropTypes.EditRender, params: VxeGlobalRendererHandles.RenderTableEditParams) {
356
- const { attrs, props } = renderOpts
357
- return [
358
- h(getCurrComponent('el-button'), {
359
- ...attrs,
360
- ...props,
361
- ...getCellEditFilterProps(renderOpts, params, null),
362
- ...getComponentOns(renderOpts, params)
363
- }, cellText(renderOpts.content))
364
- ]
365
- }
366
-
367
- function defaultButtonsEditRender (renderOpts: VxeColumnPropTypes.EditRender, params: VxeGlobalRendererHandles.RenderTableEditParams) {
368
- const { children } = renderOpts
369
- if (children) {
370
- return children.map((childRenderOpts: VxeColumnPropTypes.EditRender) => defaultButtonEditRender(childRenderOpts, params)[0])
371
- }
372
- return []
373
- }
374
-
375
- function createFilterRender (defaultProps?: { [key: string]: any }) {
376
- return function (renderOpts: VxeColumnPropTypes.FilterRender & { name: string }, params: VxeGlobalRendererHandles.RenderTableFilterParams) {
377
- const { column } = params
378
- const { name, attrs, props } = renderOpts
379
- return [
380
- h('div', {
381
- class: 'vxe-table--filter-element-wrapper'
382
- }, column.filters.map((option, oIndex) => {
383
- const optionValue = option.data
384
- return h(getCurrComponent(name), {
385
- key: oIndex,
386
- ...attrs,
387
- ...props,
388
- ...getCellEditFilterProps(renderOpts, params, optionValue, defaultProps),
389
- ...getFilterOns(renderOpts, params, option, () => {
390
- // 处理 change 事件相关逻辑
391
- handleConfirmFilter(params, !!option.data, option)
392
- })
393
- })
394
- }))
395
- ]
396
- }
397
- }
398
-
399
- function createFloatingFilterRender (defaultProps?: { [key: string]: any }) {
400
- return function (renderOpts: VxeGlobalRendererHandles.RenderTableFloatingFilterOptions & { name: string }, params: VxeGlobalRendererHandles.RenderTableFloatingFilterParams) {
401
- const { option } = params
402
- const { name, attrs, props } = renderOpts
403
- const optionValue = option.data
404
- return [
405
- h(getCurrComponent(name), {
406
- ...attrs,
407
- ...props,
408
- ...getCellEditFilterProps(renderOpts, params, optionValue, defaultProps),
409
- ...getFloatingFilterOns(renderOpts, params, option)
410
- })
411
- ]
412
- }
413
- }
414
-
415
- function handleConfirmFilter (params: VxeGlobalRendererHandles.RenderTableFilterParams, checked: boolean, option: VxeTableDefines.FilterOption) {
416
- const { $panel } = params
417
- $panel.changeOption(null, checked, option)
418
- }
419
-
420
- /**
421
- * 模糊匹配
422
- * @param params
423
- */
424
- function defaultFuzzyFilterMethod (params: VxeGlobalRendererHandles.FilterMethodParams) {
425
- const { option, row, column } = params
426
- const { data } = option
427
- const cellValue = XEUtils.get(row, column.field)
428
- return XEUtils.toValueString(cellValue).indexOf(data) > -1
429
- }
430
-
431
- /**
432
- * 精确匹配
433
- * @param params
434
- */
435
- function defaultExactFilterMethod (params: VxeGlobalRendererHandles.FilterMethodParams) {
436
- const { option, row, column } = params
437
- const { data } = option
438
- const cellValue = XEUtils.get(row, column.field)
439
- /* eslint-disable eqeqeq */
440
- return cellValue === data
441
- }
442
-
443
- function renderOptions (options: any[], optionProps: VxeGlobalRendererHandles.RenderOptionProps) {
444
- const labelProp = optionProps.label || 'label'
445
- const valueProp = optionProps.value || 'value'
446
- return XEUtils.map(options, (item, oIndex) => {
447
- return h(getCurrComponent('el-option'), {
448
- key: oIndex,
449
- value: item[valueProp],
450
- label: item[labelProp],
451
- disabled: item.disabled
452
- })
453
- })
454
- }
455
-
456
- function cellText (cellValue: any): string[] {
457
- return [formatText(cellValue)]
458
- }
459
-
460
- function createExportMethod (getExportCellValue: Function) {
461
- return function (params: VxeGlobalRendererHandles.ExportMethodParams) {
462
- const { row, column, options } = params
463
- return options && options.original ? XEUtils.get(row, column.field) : getExportCellValue(column.editRender || column.cellRender, params)
464
- }
465
- }
466
-
467
- function defaultFilterOptions () {
468
- return [
469
- { data: null }
470
- ]
471
- }
472
-
473
- VxeUI.renderer.mixin({
474
- ElAutocomplete: {
475
- tableAutoFocus: 'input',
476
- renderTableDefault: createEditRender(),
477
- renderTableEdit: createEditRender(),
478
- createTableFilterOptions: defaultFilterOptions,
479
- renderTableFilter: createFilterRender(),
480
- tableFilterDefaultMethod: defaultExactFilterMethod
481
- },
482
- ElInput: {
483
- tableAutoFocus: 'input',
484
- renderTableDefault: createEditRender(),
485
- renderTableEdit: createEditRender(),
486
- createTableFilterOptions: defaultFilterOptions,
487
- renderTableFilter: createFilterRender(),
488
- renderTableFloatingFilter: createFloatingFilterRender(),
489
- tableFilterDefaultMethod: defaultFuzzyFilterMethod
490
- },
491
- ElInputNumber: {
492
- tableAutoFocus: 'input',
493
- renderTableDefault: createEditRender(),
494
- renderTableEdit: createEditRender(),
495
- createTableFilterOptions: defaultFilterOptions,
496
- renderTableFilter: createFilterRender(),
497
- renderTableFloatingFilter: createFloatingFilterRender(),
498
- tableFilterDefaultMethod: defaultFuzzyFilterMethod
499
- },
500
- ElSelect: {
501
- tableAutoFocus: 'input',
502
- renderTableEdit (renderOpts, params) {
503
- const { options = [], optionGroups, optionProps = {}, optionGroupProps = {} } = renderOpts
504
- const { row, column } = params
505
- const { attrs } = renderOpts
506
- const cellValue = XEUtils.get(row, column.field)
507
- const props = getCellEditFilterProps(renderOpts, params, cellValue)
508
- const ons = getEditOns(renderOpts, params)
509
- if (optionGroups) {
510
- const groupOptions = optionGroupProps.options || 'options'
511
- const groupLabel = optionGroupProps.label || 'label'
512
- return [
513
- h(getCurrComponent('el-select') as ComponentOptions, {
514
- ...attrs,
515
- ...props,
516
- ...ons
517
- }, {
518
- default: () => {
519
- return XEUtils.map(optionGroups, (group, gIndex) => {
520
- return h(getCurrComponent('el-option-group') as ComponentOptions, {
521
- key: gIndex,
522
- label: group[groupLabel]
523
- }, {
524
- default: () => renderOptions(group[groupOptions], optionProps)
525
- })
526
- })
527
- }
528
- })
529
- ]
530
- }
531
- return [
532
- h(getCurrComponent('el-select') as ComponentOptions, {
533
- ...props,
534
- ...attrs,
535
- ...ons
536
- }, {
537
- default: () => renderOptions(options, optionProps)
538
- })
539
- ]
540
- },
541
- renderTableCell (renderOpts, params) {
542
- return getCellLabelVNs(renderOpts, params, getSelectCellValue(renderOpts, params))
543
- },
544
- createTableFilterOptions: defaultFilterOptions,
545
- renderTableFilter (renderOpts, params) {
546
- const { options = [], optionGroups, optionProps = {}, optionGroupProps = {} } = renderOpts
547
- const groupOptions = optionGroupProps.options || 'options'
548
- const groupLabel = optionGroupProps.label || 'label'
549
- const { column } = params
550
- const { attrs } = renderOpts
551
- return [
552
- h('div', {
553
- class: 'vxe-table--filter-element-wrapper'
554
- }, optionGroups
555
- ? column.filters.map((option, oIndex) => {
556
- const optionValue = option.data
557
- const props = getCellEditFilterProps(renderOpts, params, optionValue)
558
- return h(getCurrComponent('el-select') as ComponentOptions, {
559
- key: oIndex,
560
- ...attrs,
561
- ...props,
562
- ...getFilterOns(renderOpts, params, option, () => {
563
- // 处理 change 事件相关逻辑
564
- handleConfirmFilter(params, props.multiple ? (option.data && option.data.length > 0) : !XEUtils.eqNull(option.data), option)
565
- })
566
- }, {
567
- default: () => {
568
- return XEUtils.map(optionGroups, (group, gIndex) => {
569
- return h(getCurrComponent('el-option-group') as ComponentOptions, {
570
- key: gIndex,
571
- label: group[groupLabel]
572
- }, {
573
- default: () => renderOptions(group[groupOptions], optionProps)
574
- })
575
- })
576
- }
577
- })
578
- })
579
- : column.filters.map((option, oIndex) => {
580
- const optionValue = option.data
581
- const props = getCellEditFilterProps(renderOpts, params, optionValue)
582
- return h(getCurrComponent('el-select') as ComponentOptions, {
583
- key: oIndex,
584
- ...attrs,
585
- ...props,
586
- ...getFilterOns(renderOpts, params, option, () => {
587
- // 处理 change 事件相关逻辑
588
- handleConfirmFilter(params, props.multiple ? (option.data && option.data.length > 0) : !XEUtils.eqNull(option.data), option)
589
- })
590
- }, {
591
- default: () => renderOptions(options, optionProps)
592
- })
593
- }))
594
- ]
595
- },
596
- tableFilterDefaultMethod (params) {
597
- const { option, row, column } = params
598
- const { data } = option
599
- const { field, filterRender: renderOpts } = column
600
- const { props = {} } = renderOpts
601
- const cellValue = XEUtils.get(row, field)
602
- if (props.multiple) {
603
- if (XEUtils.isArray(cellValue)) {
604
- return XEUtils.includeArrays(cellValue, data)
605
- }
606
- return data.indexOf(cellValue) > -1
607
- }
608
- /* eslint-disable eqeqeq */
609
- return cellValue == data
610
- },
611
- tableExportMethod: createExportMethod(getSelectCellValue)
612
- },
613
- ElCascader: {
614
- tableAutoFocus: 'input',
615
- renderTableEdit: createEditRender(),
616
- renderTableCell (renderOpts, params) {
617
- return getCellLabelVNs(renderOpts, params, getCascaderCellValue(renderOpts, params))
618
- },
619
- tableExportMethod: createExportMethod(getCascaderCellValue)
620
- },
621
- ElDatePicker: {
622
- tableAutoFocus: 'input',
623
- renderTableEdit: createEditRender(),
624
- renderTableCell (renderOpts, params) {
625
- return getCellLabelVNs(renderOpts, params, getDatePickerCellValue(renderOpts, params))
626
- },
627
- createTableFilterOptions: defaultFilterOptions,
628
- renderTableFilter (renderOpts, params) {
629
- const { column } = params
630
- const { name, attrs } = renderOpts
631
- return [
632
- h('div', {
633
- class: 'vxe-table--filter-element-wrapper'
634
- }, column.filters.map((option, oIndex) => {
635
- const optionValue = option.data
636
- return h(getCurrComponent(name as string), {
637
- key: oIndex,
638
- ...attrs,
639
- ...getCellEditFilterProps(renderOpts, params, optionValue),
640
- ...getFilterOns(renderOpts, params, option, () => {
641
- // 处理 change 事件相关逻辑
642
- handleConfirmFilter(params, !!option.data, option)
643
- })
644
- })
645
- }))
646
- ]
647
- },
648
- renderTableFloatingFilter: createFloatingFilterRender(),
649
- tableFilterDefaultMethod (params) {
650
- const { option, row, column } = params
651
- const { data } = option
652
- const { filterRender: renderOpts } = column
653
- const { props = {} } = renderOpts
654
- const cellValue = XEUtils.get(row, column.field)
655
- if (data) {
656
- switch (props.type) {
657
- case 'daterange':
658
- return equalDateRange(cellValue, data, props, 'YYYY-MM-DD')
659
- case 'datetimerange':
660
- return equalDateRange(cellValue, data, props, 'YYYY-MM-DD HH:ss:mm')
661
- case 'monthrange':
662
- return equalDateRange(cellValue, data, props, 'YYYY-MM')
663
- default:
664
- return cellValue === data
665
- }
666
- }
667
- return false
668
- },
669
- tableExportMethod: createExportMethod(getDatePickerCellValue)
670
- },
671
- ElTimePicker: {
672
- tableAutoFocus: 'input',
673
- renderTableEdit: createEditRender(),
674
- renderTableCell (renderOpts, params) {
675
- return getCellLabelVNs(renderOpts, params, getTimePickerCellValue(renderOpts, params))
676
- },
677
- tableExportMethod: createExportMethod(getTimePickerCellValue)
678
- },
679
- ElTimeSelect: {
680
- tableAutoFocus: 'input',
681
- renderTableEdit: createEditRender()
682
- },
683
- ElRate: {
684
- renderTableDefault: createEditRender(),
685
- renderTableEdit: createEditRender(),
686
- createTableFilterOptions: defaultFilterOptions,
687
- renderTableFilter: createFilterRender(),
688
- tableFilterDefaultMethod: defaultExactFilterMethod
689
- },
690
- ElSwitch: {
691
- renderTableDefault: createEditRender(),
692
- renderTableEdit: createEditRender(),
693
- renderTableFilter (renderOpts, params) {
694
- const { column } = params
695
- const { name, attrs } = renderOpts
696
- return [
697
- h('div', {
698
- class: 'vxe-table--filter-element-wrapper'
699
- }, column.filters.map((option, oIndex) => {
700
- const optionValue = option.data
701
- return h(getCurrComponent(name as string), {
702
- key: oIndex,
703
- ...attrs,
704
- ...getCellEditFilterProps(renderOpts, params, optionValue),
705
- ...getFilterOns(renderOpts, params, option, () => {
706
- // 处理 change 事件相关逻辑
707
- handleConfirmFilter(params, XEUtils.isBoolean(option.data), option)
708
- })
709
- })
710
- }))
711
- ]
712
- },
713
- tableFilterDefaultMethod: defaultExactFilterMethod
714
- },
715
- ElSlider: {
716
- renderTableDefault: createEditRender(),
717
- renderTableEdit: createEditRender(),
718
- createTableFilterOptions: defaultFilterOptions,
719
- renderTableFilter: createFilterRender(),
720
- tableFilterDefaultMethod: defaultExactFilterMethod
721
- },
722
- ElButton: {
723
- renderTableDefault: defaultButtonEditRender
724
- },
725
- ElButtons: {
726
- renderTableDefault: defaultButtonsEditRender
727
- }
728
- })
729
- }
1
+ import { h, ComponentOptions } from 'vue'
2
+ import { getCurrComponent, hasInputType } from '../util/comp'
3
+ import XEUtils from 'xe-utils'
4
+ import dayjs from 'dayjs'
5
+
6
+ import type { VxeUIExport, VxeGlobalRendererHandles } from 'vxe-pc-ui'
7
+ import type { VxeTableDefines, VxeColumnPropTypes } from 'vxe-table'
8
+
9
+ /**
10
+ * 表格 - 渲染器
11
+ */
12
+ export function defineTableRender (VxeUI: VxeUIExport) {
13
+ function isEmptyValue (cellValue: any) {
14
+ return cellValue === null || cellValue === undefined || cellValue === ''
15
+ }
16
+
17
+ function getOnName (type: string) {
18
+ return 'on' + type.substring(0, 1).toLocaleUpperCase() + type.substring(1)
19
+ }
20
+
21
+ function getModelProp (renderOpts: VxeGlobalRendererHandles.RenderOptions) {
22
+ return 'modelValue'
23
+ }
24
+
25
+ function getModelEvent (renderOpts: VxeGlobalRendererHandles.RenderOptions) {
26
+ return 'update:modelValue'
27
+ }
28
+
29
+ function getChangeEvent (renderOpts: VxeGlobalRendererHandles.RenderOptions) {
30
+ let type = 'change'
31
+ switch (renderOpts.name) {
32
+ case 'ElAutocomplete':
33
+ type = 'select'
34
+ break
35
+ case 'ElInput':
36
+ case 'ElInputNumber':
37
+ type = 'input'
38
+ break
39
+ }
40
+ return type
41
+ }
42
+
43
+ function toDayStringDate (value: any, format: string) {
44
+ return dayjs(value, format).toDate()
45
+ }
46
+
47
+ function toDayDateString (date: any, format: string) {
48
+ return dayjs(date).format(format)
49
+ }
50
+
51
+ function parseDate (value: any, props: { [key: string]: any }) {
52
+ return value && props.valueFormat ? toDayStringDate(value, props.valueFormat) : value
53
+ }
54
+
55
+ function getFormatDate (value: any, props: { [key: string]: any }, defaultFormat: string) {
56
+ return value ? toDayDateString(parseDate(value, props), props.format || props.valueFormat || defaultFormat) : value
57
+ }
58
+
59
+ function getFormatDates (values: any[], props: { [key: string]: any }, separator: string, defaultFormat: string) {
60
+ return XEUtils.map(values, (date: any) => getFormatDate(date, props, defaultFormat)).join(separator)
61
+ }
62
+
63
+ function equalDateRange (cellValue: any, data: any, props: { [key: string]: any }, defaultFormat: string) {
64
+ cellValue = getFormatDate(cellValue, props, defaultFormat)
65
+ return cellValue >= getFormatDate(data[0], props, defaultFormat) && cellValue <= getFormatDate(data[1], props, defaultFormat)
66
+ }
67
+
68
+ function getCellEditFilterProps (renderOpts: any, params: VxeGlobalRendererHandles.RenderTableEditParams | VxeGlobalRendererHandles.RenderTableFilterParams | VxeGlobalRendererHandles.RenderTableFloatingFilterParams, value: any, defaultProps?: { [prop: string]: any }) {
69
+ return XEUtils.assign({}, defaultProps, renderOpts.props, { [getModelProp(renderOpts)]: value })
70
+ }
71
+
72
+ function formatText (cellValue: any) {
73
+ return '' + (isEmptyValue(cellValue) ? '' : cellValue)
74
+ }
75
+
76
+ function updateFilterChangeOption (params: VxeGlobalRendererHandles.RenderTableFilterParams | VxeGlobalRendererHandles.RenderTableFloatingFilterParams, checked: boolean, option: VxeTableDefines.FilterOption) {
77
+ const { $table } = params
78
+ $table.updateFilterOptionStatus(option, checked)
79
+ }
80
+
81
+ function saveFilterEvent (params: VxeGlobalRendererHandles.RenderTableFilterParams | VxeGlobalRendererHandles.RenderTableFloatingFilterParams) {
82
+ const { $table, column } = params
83
+ $table.saveFilterByEvent(new Event('change'), column)
84
+ }
85
+
86
+ function getCellLabelVNs (renderOpts: VxeColumnPropTypes.EditRender, params: VxeGlobalRendererHandles.RenderTableCellParams, cellLabel: any) {
87
+ const { placeholder } = renderOpts
88
+ return [
89
+ h('span', {
90
+ class: 'vxe-cell--label'
91
+ }, placeholder && isEmptyValue(cellLabel)
92
+ ? [
93
+ h('span', {
94
+ class: 'vxe-cell--placeholder'
95
+ }, formatText(VxeUI.getI18n(placeholder)))
96
+ ]
97
+ : formatText(cellLabel))
98
+ ]
99
+ }
100
+
101
+ const blurEvent = 'blur'
102
+ const clearEvent = 'clear'
103
+
104
+ /**
105
+ * 组件事件处理
106
+ * @param renderOpts
107
+ * @param params
108
+ * @param modelFunc
109
+ * @param changeFunc
110
+ */
111
+ function getComponentOns (renderOpts: VxeGlobalRendererHandles.RenderTableCellOptions | VxeGlobalRendererHandles.RenderTableFilterOptions, params: any, eFns?: {
112
+ model: (cellValue: any) => void
113
+ change?: (...args: any[]) => void
114
+ blur?: (...args: any[]) => void
115
+ clear?: (...args: any[]) => void
116
+ }, eventOns?: Record<string, any>) {
117
+ const { events } = renderOpts
118
+ const modelEvent = getModelEvent(renderOpts)
119
+ const changeEvent = getChangeEvent(renderOpts)
120
+ const { model: modelFunc, change: changeFunc, blur: blurFunc, clear: clearFunc } = eFns || {}
121
+ const ons: any = {}
122
+ XEUtils.objectEach(events, (func, key: any) => {
123
+ ons[getOnName(key)] = function (...args: any[]) {
124
+ if (!XEUtils.isFunction(func)) {
125
+ console.error(`Parameter "${func}" is not a method`)
126
+ }
127
+ func(params, ...args)
128
+ }
129
+ })
130
+ if (modelFunc) {
131
+ ons[getOnName(modelEvent)] = function (targetEvnt: any) {
132
+ modelFunc(targetEvnt)
133
+ if (events && events[modelEvent]) {
134
+ events[modelEvent](params, targetEvnt)
135
+ }
136
+ }
137
+ }
138
+ if (changeFunc) {
139
+ ons[getOnName(changeEvent)] = function (...args: any[]) {
140
+ changeFunc(...args)
141
+ if (events && events[changeEvent]) {
142
+ events[changeEvent](params, ...args)
143
+ }
144
+ }
145
+ }
146
+ if (blurFunc) {
147
+ ons[getOnName(blurEvent)] = function (...args: any[]) {
148
+ blurFunc(...args)
149
+ if (events && events[blurEvent]) {
150
+ events[blurEvent](params, ...args)
151
+ }
152
+ }
153
+ }
154
+ if (clearFunc) {
155
+ ons[getOnName(clearEvent)] = function (...args: any[]) {
156
+ clearFunc(...args)
157
+ if (events && events[clearEvent]) {
158
+ events[clearEvent](params, ...args)
159
+ }
160
+ }
161
+ }
162
+ return eventOns ? Object.assign(ons, eventOns) : ons
163
+ }
164
+
165
+ function getEditOns (renderOpts: VxeGlobalRendererHandles.RenderOptions, params: VxeGlobalRendererHandles.RenderTableEditParams) {
166
+ const { $table, row, column } = params
167
+ return getComponentOns(renderOpts, params, {
168
+ model (value) {
169
+ // 处理 model 值双向绑定
170
+ XEUtils.set(row, column.field, value)
171
+ },
172
+ change () {
173
+ // 处理 change 事件相关逻辑
174
+ $table.updateStatus(params)
175
+ }
176
+ })
177
+ }
178
+
179
+ function getFilterOns (renderOpts: any, params: VxeGlobalRendererHandles.RenderTableFilterParams, option: VxeTableDefines.FilterOption, changeFunc: ((...args: any[]) => void)) {
180
+ return getComponentOns(renderOpts, params, {
181
+ model (value: any) {
182
+ // 处理 model 值双向绑定
183
+ option.data = value
184
+ },
185
+ change: changeFunc
186
+ })
187
+ }
188
+
189
+ function getFloatingFilterOns (renderOpts: VxeGlobalRendererHandles.RenderTableFilterOptions, params: VxeGlobalRendererHandles.RenderTableFloatingFilterParams, option: VxeTableDefines.FilterOption) {
190
+ const { $table, column } = params
191
+ if (hasInputType(renderOpts)) {
192
+ return getComponentOns(renderOpts, params, {
193
+ model (value: any) {
194
+ // 处理 model 值双向绑定
195
+ option.data = value
196
+ },
197
+ change () {
198
+ updateFilterChangeOption(params, !isEmptyValue(option.data), option)
199
+ },
200
+ clear () {
201
+ updateFilterChangeOption(params, !isEmptyValue(option.data), option)
202
+ saveFilterEvent(params)
203
+ },
204
+ blur () {
205
+ $table.saveFilterByEvent(new Event('change'), column)
206
+ }
207
+ })
208
+ }
209
+ return getComponentOns(renderOpts, params, {
210
+ model (value: any) {
211
+ // 处理 model 值双向绑定
212
+ option.data = value
213
+ },
214
+ clear () {
215
+ updateFilterChangeOption(params, !isEmptyValue(option.data), option)
216
+ $table.saveFilterByEvent(new Event('change'), column)
217
+ },
218
+ change () {
219
+ updateFilterChangeOption(params, !isEmptyValue(option.data), option)
220
+ $table.saveFilterByEvent(new Event('change'), column)
221
+ }
222
+ })
223
+ }
224
+
225
+ function matchCascaderData (index: number, list: any[], values: any[], labels: any[]) {
226
+ const val = values[index]
227
+ if (list && values.length > index) {
228
+ XEUtils.each(list, (item) => {
229
+ if (item.value === val) {
230
+ labels.push(item.label)
231
+ matchCascaderData(++index, item.children, values, labels)
232
+ }
233
+ })
234
+ }
235
+ }
236
+
237
+ function handleSelectCellValue (cellValue: any, renderOpts: VxeGlobalRendererHandles.RenderTableCellOptions, renderParams: VxeGlobalRendererHandles.RenderCellParams) {
238
+ const { options = [], optionGroups, props = {}, optionProps = {}, optionGroupProps = {} } = renderOpts
239
+ const { $table, rowid, column } = renderParams
240
+ const { filterable, multiple } = props
241
+ const labelProp = optionProps.label || 'label'
242
+ const valueProp = optionProps.value || 'value'
243
+ const groupOptions = optionGroupProps.options || 'options'
244
+ const colid = column.id
245
+ let cellData: any
246
+ if (filterable) {
247
+ const { internalData } = $table
248
+ const { fullAllDataRowIdData } = internalData
249
+ const rest: any = fullAllDataRowIdData[rowid]
250
+ if (rest) {
251
+ cellData = rest.cellData
252
+ if (!cellData) {
253
+ cellData = rest.cellData = {}
254
+ }
255
+ }
256
+ if (rest && cellData[colid] && cellData[colid].value === cellValue) {
257
+ return cellData[colid].label
258
+ }
259
+ }
260
+ if (!isEmptyValue(cellValue)) {
261
+ const selectlabel = XEUtils.map(multiple ? cellValue : [cellValue], optionGroups
262
+ ? (value) => {
263
+ let selectItem: any
264
+ for (let index = 0; index < optionGroups.length; index++) {
265
+ selectItem = XEUtils.find(optionGroups[index][groupOptions], (item) => item[valueProp] === value)
266
+ if (selectItem) {
267
+ break
268
+ }
269
+ }
270
+ return selectItem ? selectItem[labelProp] : value
271
+ }
272
+ : (value) => {
273
+ const selectItem = XEUtils.find(options, (item) => item[valueProp] === value)
274
+ return selectItem ? selectItem[labelProp] : value
275
+ }).join(', ')
276
+ if (cellData && options && options.length) {
277
+ cellData[colid] = { value: cellValue, label: selectlabel }
278
+ }
279
+ return selectlabel
280
+ }
281
+ return ''
282
+ }
283
+
284
+ function getSelectCellValue (renderOpts: VxeColumnPropTypes.EditRender, renderParams: VxeGlobalRendererHandles.RenderTableCellParams) {
285
+ const { row, column } = renderParams
286
+ const cellValue = XEUtils.get(row, column.field)
287
+ return handleSelectCellValue(cellValue, renderOpts, renderParams)
288
+ }
289
+
290
+ function getCascaderCellValue (renderOpts: VxeGlobalRendererHandles.RenderOptions, params: VxeGlobalRendererHandles.RenderTableCellParams) {
291
+ const { props = {} } = renderOpts
292
+ const { row, column } = params
293
+ const cellValue = XEUtils.get(row, column.field)
294
+ const values: any[] = cellValue || []
295
+ const labels: any[] = []
296
+ matchCascaderData(0, props.options, values, labels)
297
+ return (props.showAllLevels === false ? labels.slice(labels.length - 1, labels.length) : labels).join(` ${props.separator || '/'} `)
298
+ }
299
+
300
+ function getDatePickerCellValue (renderOpts: VxeGlobalRendererHandles.RenderOptions, params: VxeGlobalRendererHandles.RenderTableCellParams | VxeGlobalRendererHandles.ExportMethodParams) {
301
+ const { props = {} } = renderOpts
302
+ const { row, column } = params
303
+ const { rangeSeparator = '-' } = props
304
+ let cellValue = XEUtils.get(row, column.field)
305
+ switch (props.type) {
306
+ case 'week':
307
+ cellValue = getFormatDate(cellValue, props, 'YYYYwWW')
308
+ break
309
+ case 'month':
310
+ cellValue = getFormatDate(cellValue, props, 'YYYY-MM')
311
+ break
312
+ case 'year':
313
+ cellValue = getFormatDate(cellValue, props, 'YYYY')
314
+ break
315
+ case 'dates':
316
+ cellValue = getFormatDates(cellValue, props, ', ', 'YYYY-MM-DD')
317
+ break
318
+ case 'daterange':
319
+ cellValue = getFormatDates(cellValue, props, ` ${rangeSeparator} `, 'YYYY-MM-DD')
320
+ break
321
+ case 'datetimerange':
322
+ cellValue = getFormatDates(cellValue, props, ` ${rangeSeparator} `, 'YYYY-MM-DD HH:ss:mm')
323
+ break
324
+ case 'monthrange':
325
+ cellValue = getFormatDates(cellValue, props, ` ${rangeSeparator} `, 'YYYY-MM')
326
+ break
327
+ default:
328
+ cellValue = getFormatDate(cellValue, props, 'YYYY-MM-DD')
329
+ }
330
+ return cellValue
331
+ }
332
+
333
+ function getTimePickerCellValue (renderOpts: VxeGlobalRendererHandles.RenderOptions, params: VxeGlobalRendererHandles.RenderTableCellParams | VxeGlobalRendererHandles.RenderTableEditParams) {
334
+ const { props = {} } = renderOpts
335
+ const { row, column } = params
336
+ const { isRange, format = 'HH:mm:ss', rangeSeparator = '-' } = props
337
+ let cellValue = XEUtils.get(row, column.field)
338
+ if (cellValue && isRange) {
339
+ cellValue = XEUtils.map(cellValue, (date) => toDayDateString(parseDate(date, props), format)).join(` ${rangeSeparator} `)
340
+ }
341
+ return toDayDateString(parseDate(cellValue, props), format)
342
+ }
343
+
344
+ function createEditRender (defaultProps?: { [key: string]: any }) {
345
+ return function (renderOpts: VxeColumnPropTypes.EditRender & { name: string }, params: VxeGlobalRendererHandles.RenderTableEditParams) {
346
+ const { row, column } = params
347
+ const { name, attrs, props } = renderOpts
348
+ const cellValue = XEUtils.get(row, column.field)
349
+ return [
350
+ h(getCurrComponent(name), {
351
+ ...attrs,
352
+ ...props,
353
+ ...getCellEditFilterProps(renderOpts, params, cellValue, defaultProps),
354
+ ...getEditOns(renderOpts, params)
355
+ })
356
+ ]
357
+ }
358
+ }
359
+
360
+ function defaultButtonEditRender (renderOpts: VxeColumnPropTypes.EditRender, params: VxeGlobalRendererHandles.RenderTableEditParams) {
361
+ const { attrs, props } = renderOpts
362
+ return [
363
+ h(getCurrComponent('el-button'), {
364
+ ...attrs,
365
+ ...props,
366
+ ...getCellEditFilterProps(renderOpts, params, null),
367
+ ...getComponentOns(renderOpts, params)
368
+ }, cellText(renderOpts.content))
369
+ ]
370
+ }
371
+
372
+ function defaultButtonsEditRender (renderOpts: VxeColumnPropTypes.EditRender, params: VxeGlobalRendererHandles.RenderTableEditParams) {
373
+ const { children } = renderOpts
374
+ if (children) {
375
+ return children.map((childRenderOpts: VxeColumnPropTypes.EditRender) => defaultButtonEditRender(childRenderOpts, params)[0])
376
+ }
377
+ return []
378
+ }
379
+
380
+ function createFilterRender (defaultProps?: { [key: string]: any }) {
381
+ return function (renderOpts: VxeColumnPropTypes.FilterRender & { name: string }, params: VxeGlobalRendererHandles.RenderTableFilterParams) {
382
+ const { column } = params
383
+ const { name, attrs, props } = renderOpts
384
+ return [
385
+ h('div', {
386
+ class: 'vxe-table--filter-element-wrapper'
387
+ }, column.filters.map((option, oIndex) => {
388
+ const optionValue = option.data
389
+ return h(getCurrComponent(name), {
390
+ key: oIndex,
391
+ ...attrs,
392
+ ...props,
393
+ ...getCellEditFilterProps(renderOpts, params, optionValue, defaultProps),
394
+ ...getFilterOns(renderOpts, params, option, () => {
395
+ // 处理 change 事件相关逻辑
396
+ handleConfirmFilter(params, !!option.data, option)
397
+ })
398
+ })
399
+ }))
400
+ ]
401
+ }
402
+ }
403
+
404
+ function createFloatingFilterRender (defaultProps?: { [key: string]: any }) {
405
+ return function (renderOpts: VxeGlobalRendererHandles.RenderTableFloatingFilterOptions & { name: string }, params: VxeGlobalRendererHandles.RenderTableFloatingFilterParams) {
406
+ const { option } = params
407
+ const { name, attrs, props } = renderOpts
408
+ const optionValue = option.data
409
+ return [
410
+ h(getCurrComponent(name), {
411
+ ...attrs,
412
+ ...props,
413
+ ...getCellEditFilterProps(renderOpts, params, optionValue, defaultProps),
414
+ ...getFloatingFilterOns(renderOpts, params, option)
415
+ })
416
+ ]
417
+ }
418
+ }
419
+
420
+ function handleConfirmFilter (params: VxeGlobalRendererHandles.RenderTableFilterParams, checked: boolean, option: VxeTableDefines.FilterOption) {
421
+ const { $panel } = params
422
+ $panel.changeOption(null, checked, option)
423
+ }
424
+
425
+ /**
426
+ * 模糊匹配
427
+ * @param params
428
+ */
429
+ function defaultFuzzyFilterMethod (params: VxeGlobalRendererHandles.FilterMethodParams) {
430
+ const { option, row, column } = params
431
+ const { data } = option
432
+ const cellValue = XEUtils.get(row, column.field)
433
+ return XEUtils.toValueString(cellValue).indexOf(data) > -1
434
+ }
435
+
436
+ /**
437
+ * 精确匹配
438
+ * @param params
439
+ */
440
+ function defaultExactFilterMethod (params: VxeGlobalRendererHandles.FilterMethodParams) {
441
+ const { option, row, column } = params
442
+ const { data } = option
443
+ const cellValue = XEUtils.get(row, column.field)
444
+ /* eslint-disable eqeqeq */
445
+ return cellValue === data
446
+ }
447
+
448
+ function renderOptions (options: any[], optionProps: VxeGlobalRendererHandles.RenderOptionProps) {
449
+ const labelProp = optionProps.label || 'label'
450
+ const valueProp = optionProps.value || 'value'
451
+ return XEUtils.map(options, (item, oIndex) => {
452
+ return h(getCurrComponent('el-option'), {
453
+ key: oIndex,
454
+ value: item[valueProp],
455
+ label: item[labelProp],
456
+ disabled: item.disabled
457
+ })
458
+ })
459
+ }
460
+
461
+ function cellText (cellValue: any): string[] {
462
+ return [formatText(cellValue)]
463
+ }
464
+
465
+ function createExportMethod (getExportCellValue: Function) {
466
+ return function (params: VxeGlobalRendererHandles.ExportMethodParams) {
467
+ const { row, column, options } = params
468
+ return options && options.original ? XEUtils.get(row, column.field) : getExportCellValue(column.editRender || column.cellRender, params)
469
+ }
470
+ }
471
+
472
+ function defaultFilterOptions () {
473
+ return [
474
+ { data: null }
475
+ ]
476
+ }
477
+
478
+ VxeUI.renderer.mixin({
479
+ ElAutocomplete: {
480
+ tableAutoFocus: 'input',
481
+ renderTableDefault: createEditRender(),
482
+ renderTableEdit: createEditRender(),
483
+ createTableFilterOptions: defaultFilterOptions,
484
+ renderTableFilter: createFilterRender(),
485
+ tableFilterDefaultMethod: defaultExactFilterMethod
486
+ },
487
+ ElInput: {
488
+ tableAutoFocus: 'input',
489
+ renderTableDefault: createEditRender(),
490
+ renderTableEdit: createEditRender(),
491
+ createTableFilterOptions: defaultFilterOptions,
492
+ renderTableFilter: createFilterRender(),
493
+ renderTableFloatingFilter: createFloatingFilterRender(),
494
+ tableFilterDefaultMethod: defaultFuzzyFilterMethod
495
+ },
496
+ ElInputNumber: {
497
+ tableAutoFocus: 'input',
498
+ renderTableDefault: createEditRender(),
499
+ renderTableEdit: createEditRender(),
500
+ createTableFilterOptions: defaultFilterOptions,
501
+ renderTableFilter: createFilterRender(),
502
+ renderTableFloatingFilter: createFloatingFilterRender(),
503
+ tableFilterDefaultMethod: defaultFuzzyFilterMethod
504
+ },
505
+ ElSelect: {
506
+ tableAutoFocus: 'input',
507
+ renderTableEdit (renderOpts, params) {
508
+ const { options = [], optionGroups, optionProps = {}, optionGroupProps = {} } = renderOpts
509
+ const { row, column } = params
510
+ const { attrs } = renderOpts
511
+ const cellValue = XEUtils.get(row, column.field)
512
+ const props = getCellEditFilterProps(renderOpts, params, cellValue)
513
+ const ons = getEditOns(renderOpts, params)
514
+ if (optionGroups) {
515
+ const groupOptions = optionGroupProps.options || 'options'
516
+ const groupLabel = optionGroupProps.label || 'label'
517
+ return [
518
+ h(getCurrComponent('el-select') as ComponentOptions, {
519
+ ...attrs,
520
+ ...props,
521
+ ...ons
522
+ }, {
523
+ default: () => {
524
+ return XEUtils.map(optionGroups, (group, gIndex) => {
525
+ return h(getCurrComponent('el-option-group') as ComponentOptions, {
526
+ key: gIndex,
527
+ label: group[groupLabel]
528
+ }, {
529
+ default: () => renderOptions(group[groupOptions], optionProps)
530
+ })
531
+ })
532
+ }
533
+ })
534
+ ]
535
+ }
536
+ return [
537
+ h(getCurrComponent('el-select') as ComponentOptions, {
538
+ ...props,
539
+ ...attrs,
540
+ ...ons
541
+ }, {
542
+ default: () => renderOptions(options, optionProps)
543
+ })
544
+ ]
545
+ },
546
+ renderTableCell (renderOpts, renderParams) {
547
+ const { options, optionGroups } = renderOpts
548
+ const { $table, row, column } = renderParams
549
+ const opSize = options ? options.length : null
550
+ const ogSize = optionGroups ? optionGroups.length : null
551
+ if ($table.effectCellData) {
552
+ const { cellResult } = $table.effectCellData(row, column, {
553
+ key: 'render_table_cell',
554
+ isChanged ({ oldValue, cellValue }) {
555
+ return oldValue && oldValue[0] === cellValue && oldValue[1] === opSize && oldValue[2] === ogSize
556
+ },
557
+ setValue ({ cellValue }) {
558
+ return [cellValue, opSize, ogSize]
559
+ },
560
+ getResult ({ cellValue }) {
561
+ return handleSelectCellValue(cellValue, renderOpts, renderParams)
562
+ }
563
+ })
564
+ return getCellLabelVNs(renderOpts, renderParams, cellResult)
565
+ }
566
+ const cellResult = getSelectCellValue(renderOpts, renderParams)
567
+ return getCellLabelVNs(renderOpts, renderParams, cellResult)
568
+ },
569
+ createTableFilterOptions: defaultFilterOptions,
570
+ renderTableFilter (renderOpts, params) {
571
+ const { options = [], optionGroups, optionProps = {}, optionGroupProps = {} } = renderOpts
572
+ const groupOptions = optionGroupProps.options || 'options'
573
+ const groupLabel = optionGroupProps.label || 'label'
574
+ const { column } = params
575
+ const { attrs } = renderOpts
576
+ return [
577
+ h('div', {
578
+ class: 'vxe-table--filter-element-wrapper'
579
+ }, optionGroups
580
+ ? column.filters.map((option, oIndex) => {
581
+ const optionValue = option.data
582
+ const props = getCellEditFilterProps(renderOpts, params, optionValue)
583
+ return h(getCurrComponent('el-select') as ComponentOptions, {
584
+ key: oIndex,
585
+ ...attrs,
586
+ ...props,
587
+ ...getFilterOns(renderOpts, params, option, () => {
588
+ // 处理 change 事件相关逻辑
589
+ handleConfirmFilter(params, props.multiple ? (option.data && option.data.length > 0) : !XEUtils.eqNull(option.data), option)
590
+ })
591
+ }, {
592
+ default: () => {
593
+ return XEUtils.map(optionGroups, (group, gIndex) => {
594
+ return h(getCurrComponent('el-option-group') as ComponentOptions, {
595
+ key: gIndex,
596
+ label: group[groupLabel]
597
+ }, {
598
+ default: () => renderOptions(group[groupOptions], optionProps)
599
+ })
600
+ })
601
+ }
602
+ })
603
+ })
604
+ : column.filters.map((option, oIndex) => {
605
+ const optionValue = option.data
606
+ const props = getCellEditFilterProps(renderOpts, params, optionValue)
607
+ return h(getCurrComponent('el-select') as ComponentOptions, {
608
+ key: oIndex,
609
+ ...attrs,
610
+ ...props,
611
+ ...getFilterOns(renderOpts, params, option, () => {
612
+ // 处理 change 事件相关逻辑
613
+ handleConfirmFilter(params, props.multiple ? (option.data && option.data.length > 0) : !XEUtils.eqNull(option.data), option)
614
+ })
615
+ }, {
616
+ default: () => renderOptions(options, optionProps)
617
+ })
618
+ }))
619
+ ]
620
+ },
621
+ tableFilterDefaultMethod (params) {
622
+ const { option, row, column } = params
623
+ const { data } = option
624
+ const { field, filterRender: renderOpts } = column
625
+ const { props = {} } = renderOpts
626
+ const cellValue = XEUtils.get(row, field)
627
+ if (props.multiple) {
628
+ if (XEUtils.isArray(cellValue)) {
629
+ return XEUtils.includeArrays(cellValue, data)
630
+ }
631
+ return data.indexOf(cellValue) > -1
632
+ }
633
+ /* eslint-disable eqeqeq */
634
+ return cellValue == data
635
+ },
636
+ tableExportMethod: createExportMethod(getSelectCellValue)
637
+ },
638
+ ElCascader: {
639
+ tableAutoFocus: 'input',
640
+ renderTableEdit: createEditRender(),
641
+ renderTableCell (renderOpts, params) {
642
+ return getCellLabelVNs(renderOpts, params, getCascaderCellValue(renderOpts, params))
643
+ },
644
+ tableExportMethod: createExportMethod(getCascaderCellValue)
645
+ },
646
+ ElDatePicker: {
647
+ tableAutoFocus: 'input',
648
+ renderTableEdit: createEditRender(),
649
+ renderTableCell (renderOpts, params) {
650
+ return getCellLabelVNs(renderOpts, params, getDatePickerCellValue(renderOpts, params))
651
+ },
652
+ createTableFilterOptions: defaultFilterOptions,
653
+ renderTableFilter (renderOpts, params) {
654
+ const { column } = params
655
+ const { name, attrs } = renderOpts
656
+ return [
657
+ h('div', {
658
+ class: 'vxe-table--filter-element-wrapper'
659
+ }, column.filters.map((option, oIndex) => {
660
+ const optionValue = option.data
661
+ return h(getCurrComponent(name as string), {
662
+ key: oIndex,
663
+ ...attrs,
664
+ ...getCellEditFilterProps(renderOpts, params, optionValue),
665
+ ...getFilterOns(renderOpts, params, option, () => {
666
+ // 处理 change 事件相关逻辑
667
+ handleConfirmFilter(params, !!option.data, option)
668
+ })
669
+ })
670
+ }))
671
+ ]
672
+ },
673
+ renderTableFloatingFilter: createFloatingFilterRender(),
674
+ tableFilterDefaultMethod (params) {
675
+ const { option, row, column } = params
676
+ const { data } = option
677
+ const { filterRender: renderOpts } = column
678
+ const { props = {} } = renderOpts
679
+ const cellValue = XEUtils.get(row, column.field)
680
+ if (data) {
681
+ switch (props.type) {
682
+ case 'daterange':
683
+ return equalDateRange(cellValue, data, props, 'YYYY-MM-DD')
684
+ case 'datetimerange':
685
+ return equalDateRange(cellValue, data, props, 'YYYY-MM-DD HH:ss:mm')
686
+ case 'monthrange':
687
+ return equalDateRange(cellValue, data, props, 'YYYY-MM')
688
+ default:
689
+ return cellValue === data
690
+ }
691
+ }
692
+ return false
693
+ },
694
+ tableExportMethod: createExportMethod(getDatePickerCellValue)
695
+ },
696
+ ElTimePicker: {
697
+ tableAutoFocus: 'input',
698
+ renderTableEdit: createEditRender(),
699
+ renderTableCell (renderOpts, params) {
700
+ return getCellLabelVNs(renderOpts, params, getTimePickerCellValue(renderOpts, params))
701
+ },
702
+ tableExportMethod: createExportMethod(getTimePickerCellValue)
703
+ },
704
+ ElTimeSelect: {
705
+ tableAutoFocus: 'input',
706
+ renderTableEdit: createEditRender()
707
+ },
708
+ ElRate: {
709
+ renderTableDefault: createEditRender(),
710
+ renderTableEdit: createEditRender(),
711
+ createTableFilterOptions: defaultFilterOptions,
712
+ renderTableFilter: createFilterRender(),
713
+ tableFilterDefaultMethod: defaultExactFilterMethod
714
+ },
715
+ ElSwitch: {
716
+ renderTableDefault: createEditRender(),
717
+ renderTableEdit: createEditRender(),
718
+ renderTableFilter (renderOpts, params) {
719
+ const { column } = params
720
+ const { name, attrs } = renderOpts
721
+ return [
722
+ h('div', {
723
+ class: 'vxe-table--filter-element-wrapper'
724
+ }, column.filters.map((option, oIndex) => {
725
+ const optionValue = option.data
726
+ return h(getCurrComponent(name as string), {
727
+ key: oIndex,
728
+ ...attrs,
729
+ ...getCellEditFilterProps(renderOpts, params, optionValue),
730
+ ...getFilterOns(renderOpts, params, option, () => {
731
+ // 处理 change 事件相关逻辑
732
+ handleConfirmFilter(params, XEUtils.isBoolean(option.data), option)
733
+ })
734
+ })
735
+ }))
736
+ ]
737
+ },
738
+ tableFilterDefaultMethod: defaultExactFilterMethod
739
+ },
740
+ ElSlider: {
741
+ renderTableDefault: createEditRender(),
742
+ renderTableEdit: createEditRender(),
743
+ createTableFilterOptions: defaultFilterOptions,
744
+ renderTableFilter: createFilterRender(),
745
+ tableFilterDefaultMethod: defaultExactFilterMethod
746
+ },
747
+ ElButton: {
748
+ renderTableDefault: defaultButtonEditRender
749
+ },
750
+ ElButtons: {
751
+ renderTableDefault: defaultButtonsEditRender
752
+ }
753
+ })
754
+ }