index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <el-select
  3. v-if="multiple"
  4. v-model="accountId"
  5. style="width: 100%"
  6. multiple
  7. filterable
  8. placeholder="请选择"
  9. @change="handleSelectUserS"
  10. >
  11. <el-option-group v-for="(user, idx) in userList" :key="idx" :label="user.name">
  12. <el-option
  13. v-for="item in user.children"
  14. :key="item.accountId"
  15. :label="item.name"
  16. :value="item.accountId"
  17. />
  18. </el-option-group>
  19. </el-select>
  20. <el-select
  21. v-else
  22. v-model="accountId"
  23. style="width: 100%"
  24. filterable
  25. placeholder="请选择人员"
  26. @change="handleSelectUser"
  27. >
  28. <el-option-group v-for="(user, idx) in userList" :key="idx" :label="user.name">
  29. <el-option
  30. v-for="item in user.children"
  31. :key="item.accountId"
  32. :label="item.name"
  33. :value="item.accountId"
  34. />
  35. </el-option-group>
  36. </el-select>
  37. </template>
  38. <script>
  39. import { getUserList } from '@/api/system/userApi'
  40. import { isArray } from '@/utils/validate'
  41. export default {
  42. name: 'UserSelector',
  43. props: {
  44. defaultVal: {
  45. type: [Number, String, Array],
  46. default: ''
  47. },
  48. multiple: {
  49. type: [Boolean, String, Array],
  50. default: false
  51. }
  52. },
  53. data() {
  54. return {
  55. accountIdS: [],
  56. accountId: undefined,
  57. userList: [],
  58. userSelObj: {}
  59. }
  60. },
  61. computed: {},
  62. watch: {
  63. defaultVal(val) {
  64. this.accountId = val
  65. }
  66. },
  67. created() {
  68. this.initUserListData()
  69. },
  70. mounted() {
  71. this.initUserListData()
  72. },
  73. methods: {
  74. // 初始化用户列表
  75. initUserListData() {
  76. getUserList().then((resp) => {
  77. const { code, data, msg } = resp
  78. if (code === 200) {
  79. this.userList = data
  80. } else {
  81. this.$message.error(msg)
  82. }
  83. }).catch((error) => {
  84. console.log(error)
  85. })
  86. },
  87. // 执行人选择
  88. handleSelectUser(data) {
  89. this.userList.forEach((node) => {
  90. if (node.children) {
  91. node.children.forEach((a) => {
  92. if (a.accountId === data) {
  93. this.setVal(a)
  94. return
  95. }
  96. })
  97. }
  98. })
  99. }, // 执行人选择
  100. handleSelectUserS(data) {
  101. this.setVal(data)
  102. },
  103. // 返回值
  104. setVal(obj) {
  105. const userList = this.userList.reduce((total, current) => total.concat(current.children), [])
  106. const userItems = userList.filter(item => isArray(obj) ? obj.includes(item.accountId) : obj.accountId === item.accountId)
  107. this.$emit('setUserInfo', obj, userItems)
  108. }
  109. }
  110. }
  111. </script>