index.vue
4.43 KB
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
<template>
<div>
<div style="display: flex; width: 100%; align-items: center">
<a-input
:placeholder="placeholder"
:disabled="disabled"
:value="displayValue"
readonly
style="flex: 1"
/>
<a-button type="primary" @click="openModal" :disabled="disabled">选人</a-button>
</div>
<a-modal
v-model:visible="visible"
:title="title"
width="60vw"
@ok="handleOk"
@cancel="handleCancel"
>
<div class="modal-content">
<OrganizationIdTree
@select="handleOrgSelect"
isOpen="true"
class="org-tree"
/>
<div class="user-list">
<a-radio-group
v-if="mode === 'single'"
v-model:value="selectedValue"
>
<a-radio
v-for="user in userList"
:key="user.id"
:value="user"
:style="radioStyle"
>
{{ user.username }}
</a-radio>
</a-radio-group>
<a-checkbox-group
v-else
v-model:value="selectedValues"
>
<a-checkbox
v-for="user in userList"
:key="user.id"
:value="user"
:style="checkboxStyle"
>
{{ user.username }}
</a-checkbox>
</a-checkbox-group>
</div>
</div>
</a-modal>
</div>
</template>
<script setup lang="ts">
import { ref} from 'vue';
import { useUserStore } from '/@/store/modules/user';
import { OrganizationIdTree } from '/@/views/common/organizationIdTree';
const props = defineProps({
modelValue: { type: [Object, Array] }, // 单选对象/多选数组
mode: { type: String, default: 'single' }, // single/multiple
label: { type: String, default: '报修人' },
placeholder: { type: String, default: '请选择' },
disabled: Boolean,
required: Boolean,
title: { type: String, default: '选择人员' },
showLabel: { type: Boolean, default: true },
// 新增字段映射配置
valueField: { type: String, default: 'id' },
displayField: { type: String, default: 'username' }
});
const emit = defineEmits(['update:modelValue', 'change']);
const userStore = useUserStore();
const visible = ref(false);
const displayValue = ref('');
const userList = ref([]);
const selectedValue = ref(null);
const selectedValues = ref([]);
// 处理多选模式初始化
const openModal = async () => {
visible.value = true;
console.log(userList,'userList')
if (props.mode === 'single') {
selectedValue.value = userList.value.find(u => u.id === props.modelValue);
} else {
// 将父组件传递的 ID 数组转换为用户对象数组
const selectedIds = props.modelValue || [];
selectedValues.value = userList.value.filter(u =>
selectedIds.includes(u[props.valueField])
);
}
};
// 修改确认逻辑
const handleOk = () => {
if (props.mode === 'single') {
if (!selectedValue.value && props.required) {
return Promise.reject('请选择人员');
}
// 发送完整用户对象给父组件
emit('update:modelValue', {
id: selectedValue.value.id,
username: selectedValue.value.username
});
displayValue.value = selectedValue?.value?.username || '';
} else {
// 提取指定字段的值
const output = selectedValues.value.map(u => u[props.valueField]);
// 更新显示值
displayValue.value = selectedValues.value
.map(u => u[props.displayField])
.join(', ');
// 发送给父组件
emit('update:modelValue', output);
emit('change', output);
console.log(selectedValue,'selectedValue?.value')
}
visible.value = false;
};
// 取消选择
const handleCancel = () => {
visible.value = false;
};
// 样式配置
const radioStyle = {
display: 'block',
height: '30px',
lineHeight: '30px'
};
const checkboxStyle = {
display: 'block',
height: '30px',
lineHeight: '30px',
width: '100%',
marginLeft: 0
};
</script>
<style scoped>
.modal-content {
display: flex;
padding: 20px;
height: 50vh;
overflow: hidden;
}
.org-tree {
flex: 0 0 30%;
overflow-y: auto;
padding-right: 20px;
border-right: 1px solid #e8e8e8;
}
.user-list {
flex: 1;
padding-left: 20px;
overflow-y: auto;
}
.inputTitle.required::before {
display: inline-block;
margin-right: 4px;
color: #ff4d4f;
font-size: 14px;
font-family: SimSun, sans-serif;
line-height: 1;
content: '*';
}
</style>