| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <template>
- <el-select
- v-if="multiple"
- v-model="accountId"
- style="width: 100%"
- multiple
- filterable
- placeholder="请选择"
- @change="handleSelectUserS"
- >
- <el-option-group v-for="(user, idx) in userList" :key="idx" :label="user.name">
- <el-option
- v-for="item in user.children"
- :key="item.accountId"
- :label="item.name"
- :value="item.accountId"
- />
- </el-option-group>
- </el-select>
- <el-select
- v-else
- v-model="accountId"
- style="width: 100%"
- filterable
- placeholder="请选择人员"
- @change="handleSelectUser"
- >
- <el-option-group v-for="(user, idx) in userList" :key="idx" :label="user.name">
- <el-option
- v-for="item in user.children"
- :key="item.accountId"
- :label="item.name"
- :value="item.accountId"
- />
- </el-option-group>
- </el-select>
- </template>
- <script>
- import { getUserList } from '@/api/system/userApi'
- import { isArray } from '@/utils/validate'
- export default {
- name: 'UserSelector',
- props: {
- defaultVal: {
- type: [Number, String, Array],
- default: ''
- },
- multiple: {
- type: [Boolean, String, Array],
- default: false
- }
- },
- data() {
- return {
- accountIdS: [],
- accountId: undefined,
- userList: [],
- userSelObj: {}
- }
- },
- computed: {},
- watch: {
- defaultVal(val) {
- this.accountId = val
- }
- },
- created() {
- this.initUserListData()
- },
- mounted() {
- this.initUserListData()
- },
- methods: {
- // 初始化用户列表
- initUserListData() {
- getUserList().then((resp) => {
- const { code, data, msg } = resp
- if (code === 200) {
- this.userList = data
- } else {
- this.$message.error(msg)
- }
- }).catch((error) => {
- console.log(error)
- })
- },
- // 执行人选择
- handleSelectUser(data) {
- this.userList.forEach((node) => {
- if (node.children) {
- node.children.forEach((a) => {
- if (a.accountId === data) {
- this.setVal(a)
- return
- }
- })
- }
- })
- }, // 执行人选择
- handleSelectUserS(data) {
- this.setVal(data)
- },
- // 返回值
- setVal(obj) {
- const userList = this.userList.reduce((total, current) => total.concat(current.children), [])
- const userItems = userList.filter(item => isArray(obj) ? obj.includes(item.accountId) : obj.accountId === item.accountId)
- this.$emit('setUserInfo', obj, userItems)
- }
- }
- }
- </script>
|