1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
222
223
224
225
226
|
export default defineComponent({
name: 'JVxeSelectCell',
components: { LoadingOutlined },
props: useJVxeCompProps(),
setup(props: JVxeComponent.Props) {
const { innerValue, cellProps, row, column, originColumn, scrolling, handleChangeCommon, handleBlurCommon } = useJVxeComponent(props);
const loading = ref(false);
// 异步加载的options(用于多级联动)
const asyncOptions = ref<any[] | null>(null);
let finalColumn = originColumn;
const columnKey = column.value.key || column.value.field;
if (columnKey !== originColumn.value.key && originColumn.value.children) {
const { options, allowSearch, allowInput } = originColumn.value.children.find((child) => child.key === columnKey) ?? {};
finalColumn = column;
finalColumn.value.options = options;
finalColumn.value.allowSearch = allowSearch;
finalColumn.value.allowInput = allowInput;
}
// 下拉框 props
const selectProps = computed(() => {
let selProps = {
...cellProps.value,
allowClear: true,
autofocus: true,
defaultOpen: !scrolling.value,
style: { width: '100%' },
filterOption: handleSelectFilterOption,
onBlur: handleBlur,
onChange: handleChange,
};
// 判断select是否允许输入
let { allowSearch, allowInput } = finalColumn.value;
if (allowInput === true || allowSearch === true) {
selProps['showSearch'] = true;
selProps['onSearch'] = handleSearchSelect;
}
return selProps;
});
// 下拉选项
const selectOptions = computed(() => {
if (asyncOptions.value) {
return asyncOptions.value;
}
let { linkage } = props.renderOptions;
if (linkage) {
let { getLinkageOptionsSibling, config } = linkage;
let res = getLinkageOptionsSibling(row.value, finalColumn.value, config, true);
// 当返回Promise时,说明是多级联动
if (res instanceof Promise) {
loading.value = true;
res
.then((opt) => {
asyncOptions.value = opt;
loading.value = false;
})
.catch((e) => {
console.error(e);
loading.value = false;
});
} else {
asyncOptions.value = null;
return res;
}
}
return finalColumn.value.options;
});
// --------- created ---------
// 多选、搜索type
let multipleTypes = [JVxeTypes.selectMultiple, 'list_multi'];
let searchTypes = [JVxeTypes.selectSearch, 'sel_search'];
if (multipleTypes.includes(props.type)) {
// 处理多选
let props = finalColumn.value.props || {};
props['mode'] = 'multiple';
props['maxTagCount'] = 1;
finalColumn.value.allowSearch = true;
finalColumn.value.props = props;
} else if (searchTypes.includes(props.type)) {
// 处理搜索
finalColumn.value.allowSearch = true;
}
/** 处理 change 事件 */
function handleChange(value) {
// 处理下级联动
let linkage = props.renderOptions.linkage;
if (linkage) {
linkage.handleLinkageSelectChange(row.value, finalColumn.value, linkage.config, value);
}
handleChangeCommon(value);
}
/** 处理blur失去焦点事件 */
function handleBlur(value) {
let { allowInput, options } = finalColumn.value;
if (allowInput === true) {
// 删除无用的因搜索(用户输入)而创建的项
if (typeof value === 'string') {
let indexes: number[] = [];
options.forEach((option, index) => {
if (option.value.toLocaleString() === value.toLocaleString()) {
delete option.searchAdd;
} else if (option.searchAdd === true) {
indexes.push(index);
}
});
// 翻转删除数组中的项
for (let index of indexes.reverse()) {
options.splice(index, 1);
}
}
}
handleBlurCommon(value);
}
/** 用于搜索下拉框中的内容 */
function handleSelectFilterOption(input, option) {
let { allowSearch, allowInput } = finalColumn.value;
if (allowSearch === true || allowInput === true) {
if (option.title == null) return false;
return option.title.toLowerCase().indexOf(input.toLowerCase()) >= 0;
}
return true;
}
/** select 搜索时的事件,用于动态添加options */
function handleSearchSelect(value) {
let { allowSearch, allowInput, options } = finalColumn.value;
if (allowSearch !== true && allowInput === true) {
// 是否找到了对应的项,找不到则添加这一项
let flag = false;
for (let option of options) {
if (option.value.toLocaleString() === value.toLocaleString()) {
flag = true;
break;
}
}
if (!flag && !!value) {
// searchAdd 是否是通过搜索添加的
options.push({ title: value, value: value, searchAdd: true });
}
}
}
return {
loading,
innerValue,
selectProps,
selectOptions,
handleChange,
handleBlur,
};
},
// 【组件增强】注释详见:JVxeComponent.Enhanced
enhanced: {
aopEvents: {
editActived({ $event, row, column }) {
dispatchEvent({
$event,
row,
column,
props: this.props,
instance: this,
className: '.ant-select .ant-select-selection-search-input',
isClick: false,
handler: (el) => el.focus(),
});
},
},
translate: {
enabled: true,
async handler(value, ctx) {
let { props, context } = ctx!;
let { row, column, originColumn } = context;
let finalColumn = originColumn;
if (column.value.key != originColumn.value.key && originColumn.value.children) {
finalColumn = column;
finalColumn.value.options = originColumn.value.children.find((child) => child.key == column.value.key)?.options;
}
let options;
let linkage = props?.renderOptions.linkage;
// 判断是否是多级联动,如果是就通过接口异步翻译
if (linkage) {
let { getLinkageOptionsSibling, config } = linkage;
let linkageOptions = getLinkageOptionsSibling(row.value, finalColumn.value, config, true);
options = isPromise(linkageOptions) ? await linkageOptions : linkageOptions;
} else if (isPromise(finalColumn.value.optionsPromise)) {
options = await finalColumn.value.optionsPromise;
} else {
options = finalColumn.value.options;
}
return filterDictText(options, value);
},
},
getValue(value) {
if (Array.isArray(value)) {
return value.join(',');
} else {
return value;
}
},
setValue(value, ctx) {
let { context } = ctx!;
let { originColumn, column } = context;
let finalColumn = originColumn;
if (column.value.key != originColumn.value.key && originColumn.value.children) {
finalColumn = column;
finalColumn.value.options = originColumn.value.children.find((child) => child.key == column.value.key)?.options;
}
// 判断是否是多选
if ((finalColumn.value.props || {})['mode'] === 'multiple') {
finalColumn.value.props['maxTagCount'] = 1;
}
if (value != null && value !== '') {
if (typeof value === 'string') {
return value === '' ? [] : value.split(',');
}
return value;
} else {
return undefined;
}
},
} as JVxeComponent.EnhancedPartial,
});
|