雪忆
2024-07-29 ecfe66a0199606241c73a52519bbe800c9aa31f8
提交 | 用户 | age
6753e2 1 import { ComponentOptions, h, resolveComponent } from 'vue';
64a499 2 import { VxeColumnPropTypes, VxeGlobalRendererHandles, VxeGlobalRendererOptions } from 'vxe-table';
6753e2 3 import XEUtils from 'xe-utils';
L 4 import {
5   cellText,
6   createCellRender,
7   createEvents,
8   createProps,
9   isEmptyValue,
10   createExportMethod,
11   createFormItemRender,
12 } from './common';
13
14 function renderOptions(options: any[], optionProps: VxeGlobalRendererHandles.RenderOptionProps) {
15   const labelProp = optionProps.label || 'label';
16   const valueProp = optionProps.value || 'value';
17   return XEUtils.map(options, (item, oIndex) => {
18     return h(
19       resolveComponent('a-select-option') as ComponentOptions,
20       {
21         key: oIndex,
22         value: item[valueProp],
23         disabled: item.disabled,
24       },
25       {
26         default: () => cellText(item[labelProp]),
27       },
28     );
29   });
30 }
31
32 function createEditRender() {
33   return function (
34     renderOpts: VxeColumnPropTypes.EditRender,
64a499 35     params: VxeGlobalRendererHandles.RenderTableEditParams,
6753e2 36   ) {
L 37     const { options = [], optionGroups, optionProps = {}, optionGroupProps = {} } = renderOpts;
38     const { row, column, $table } = params;
39     const { attrs } = renderOpts;
40     const cellValue = XEUtils.get(row, column.field as string);
41     const props = createProps(renderOpts, cellValue);
42     const ons = createEvents(
43       renderOpts,
44       params,
45       (value: any) => {
46         // 处理 model 值双向绑定
47         XEUtils.set(row, column.field as string, value);
48       },
49       () => {
50         // 处理 change 事件相关逻辑
51         $table.updateStatus(params);
52       },
53     );
54     if (optionGroups) {
55       const groupOptions = optionGroupProps.options || 'options';
56       const groupLabel = optionGroupProps.label || 'label';
57       return [
58         h(
59           resolveComponent('a-select') as ComponentOptions,
60           {
61             ...attrs,
62             ...props,
63             ...ons,
64           },
65           {
66             default: () => {
67               return XEUtils.map(optionGroups, (group, gIndex) => {
68                 return h(
69                   resolveComponent('a-select-opt-group') as ComponentOptions,
70                   {
71                     key: gIndex,
72                   },
73                   {
74                     label: () => {
75                       return h('span', {}, group[groupLabel]);
76                     },
77                     default: () => renderOptions(group[groupOptions], optionProps),
78                   },
79                 );
80               });
81             },
82           },
83         ),
84       ];
85     }
86     return [
87       h(
88         resolveComponent('a-select') as ComponentOptions,
89         {
90           ...props,
91           ...attrs,
92           ...ons,
93         },
94         {
95           default: () => renderOptions(options, optionProps),
96         },
97       ),
98     ];
99   };
100 }
101
102 function getSelectCellValue(
64a499 103   renderOpts: VxeGlobalRendererHandles.RenderTableCellOptions,
F 104   params: VxeGlobalRendererHandles.RenderTableCellParams,
6753e2 105 ) {
L 106   const {
107     options = [],
108     optionGroups,
109     props = {},
110     optionProps = {},
111     optionGroupProps = {},
112   } = renderOpts;
113   const { row, column } = params;
114   const labelProp = optionProps.label || 'label';
115   const valueProp = optionProps.value || 'value';
116   const groupOptions = optionGroupProps.options || 'options';
117   const cellValue = XEUtils.get(row, column.field as string);
118   if (!isEmptyValue(cellValue)) {
119     return XEUtils.map(
120       props.mode === 'multiple' ? cellValue : [cellValue],
121       optionGroups
122         ? (value) => {
123             let selectItem;
124             for (let index = 0; index < optionGroups.length; index++) {
125               selectItem = XEUtils.find(
126                 optionGroups[index][groupOptions],
127                 (item) => item[valueProp] === value,
128               );
129               if (selectItem) {
130                 break;
131               }
132             }
133             return selectItem ? selectItem[labelProp] : value;
134           }
135         : (value) => {
136             const selectItem = XEUtils.find(options, (item) => item[valueProp] === value);
137             return selectItem ? selectItem[labelProp] : value;
138           },
139     ).join(', ');
140   }
141   return '';
142 }
143
144 function createFilterRender() {
145   return function (
146     renderOpts: VxeColumnPropTypes.FilterRender,
64a499 147     params: VxeGlobalRendererHandles.RenderTableFilterParams,
6753e2 148   ) {
L 149     const { options = [], optionGroups, optionProps = {}, optionGroupProps = {} } = renderOpts;
150     const groupOptions = optionGroupProps.options || 'options';
151     const groupLabel = optionGroupProps.label || 'label';
152     const { column } = params;
153     const { attrs } = renderOpts;
154
155     return [
156       h(
157         'div',
158         {
159           class: 'vxe-table--filter-antd-wrapper',
160         },
161         optionGroups
162           ? column.filters.map((option, oIndex) => {
163               const optionValue = option.data;
164               const props = createProps(renderOpts, optionValue);
165
166               return h(
167                 resolveComponent('a-select') as ComponentOptions,
168                 {
169                   key: oIndex,
170                   ...attrs,
171                   ...props,
172                   ...createEvents(
173                     renderOpts,
174                     params,
175                     (value: any) => {
176                       // 处理 model 值双向绑定
177                       option.data = value;
178                     },
179                     () => {
180                       // 处理 change 事件相关逻辑
181                       const { $panel } = params;
182                       $panel.changeOption(
183                         null,
184                         props.mode === 'multiple'
185                           ? option.data && option.data.length > 0
186                           : !XEUtils.eqNull(option.data),
187                         option,
188                       );
189                     },
190                   ),
191                 },
192                 {
193                   default: () => {
194                     return XEUtils.map(optionGroups, (group, gIndex) => {
195                       return h(
196                         resolveComponent('a-select-opt-group') as ComponentOptions,
197                         {
198                           key: gIndex,
199                         },
200                         {
201                           label: () => {
202                             return h('span', {}, group[groupLabel]);
203                           },
204                           default: () => renderOptions(group[groupOptions], optionProps),
205                         },
206                       );
207                     });
208                   },
209                 },
210               );
211             })
212           : column.filters.map((option, oIndex) => {
213               const optionValue = option.data;
214               const props = createProps(renderOpts, optionValue);
215               return h(
216                 resolveComponent('a-select') as ComponentOptions,
217                 {
218                   key: oIndex,
219                   ...attrs,
220                   ...props,
221                   ...createEvents(
222                     renderOpts,
223                     params,
224                     (value: any) => {
225                       // 处理 model 值双向绑定
226                       option.data = value;
227                     },
228                     () => {
229                       // 处理 change 事件相关逻辑
230                       const { $panel } = params;
231                       $panel.changeOption(
232                         null,
233                         props.mode === 'multiple'
234                           ? option.data && option.data.length > 0
235                           : !XEUtils.eqNull(option.data),
236                         option,
237                       );
238                     },
239                   ),
240                 },
241                 {
242                   default: () => renderOptions(options, optionProps),
243                 },
244               );
245             }),
246       ),
247     ];
248   };
249 }
250
251 export default {
64a499 252   renderTableEdit: createEditRender(),
F 253   renderTableCell: createCellRender(getSelectCellValue),
254   renderTableFilter: createFilterRender(),
255   tableFilterDefaultMethod(params) {
6753e2 256     const { option, row, column } = params;
L 257     const { data } = option;
258     const { field, filterRender: renderOpts } = column;
259     const { props = {} } = renderOpts;
260     const cellValue = XEUtils.get(row, field);
261     if (props.mode === 'multiple') {
262       if (XEUtils.isArray(cellValue)) {
263         return XEUtils.includeArrays(cellValue, data);
264       }
265       return data.indexOf(cellValue) > -1;
266     }
267     return cellValue == data;
268   },
64a499 269   renderFormItemContent: createFormItemRender(),
F 270   tableExportMethod: createExportMethod(getSelectCellValue),
271 } as VxeGlobalRendererOptions;