selectDeptUser.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <script setup lang="ts">
  2. import { provide, onMounted, ref } from 'vue'
  3. import { ElTree } from 'element-plus'
  4. import { post } from "@/utils/request";
  5. const departmentData = ref<any[]>([])
  6. const filterText = ref('')
  7. const treeRef = ref<InstanceType<typeof ElTree>>()
  8. const defaultProps = {
  9. children: 'children',
  10. label: 'label',
  11. value: 'id'
  12. }
  13. function getDepartmentData() {
  14. post(`/department/listAllMemb`, { keyword: '' }).then(res => {
  15. departmentData.value = setUserToDept(res.data || [])
  16. })
  17. }
  18. function setUserToDept(list: any): any[] {
  19. let newList = JSON.parse(JSON.stringify(list))
  20. for (let item of newList) {
  21. if (item.children != null) {
  22. item.children = setUserToDept(item.children);
  23. }
  24. if (item.userList != null) {
  25. if (item.children == null) {
  26. item.children = [];
  27. }
  28. item.userList.forEach((element: any) => {
  29. var obj = { id: element.id, label: element.name, parentId: element.departmentId, isUser: 1 };
  30. item.children.push(obj);
  31. });
  32. }
  33. }
  34. return newList;
  35. }
  36. onMounted(() => {
  37. getDepartmentData()
  38. })
  39. function getSelectData() {
  40. return treeRef.value!.getCheckedNodes()
  41. }
  42. // 向外暴露方法
  43. defineExpose({
  44. getSelectData,
  45. });
  46. </script>
  47. <template>
  48. <div class="w-full h-[60vh] flex flex-col">
  49. <!-- <el-input v-model="filterText" placeholder="请输入" class="mb-4" /> -->
  50. <div class="flex-1 overflow-auto">
  51. <el-tree ref="treeRef" :data="departmentData" :props="defaultProps" :check-on-click-node="true" :expand-on-click-node="false" :highlight-current="true" node-key="id" :show-checkbox="true" :check-strictly="true">
  52. <template #default="{ node, data }">
  53. <template v-if="node.data.isUser">
  54. <div class="flex items-center">
  55. <span class="pr-2"><el-icon color="#e6a23c"><Avatar /></el-icon></span>
  56. <TextTranslation translationTypes="userName" :translationValue="node.label"></TextTranslation>
  57. </div>
  58. </template>
  59. <template v-if="!node.data.isUser">
  60. <div class="flex items-center">
  61. <span class="pr-2"><el-icon color="#075985"><Grid /></el-icon></span>
  62. <TextTranslation translationTypes="departmentName" :translationValue="node.label"></TextTranslation>
  63. </div>
  64. </template>
  65. </template>
  66. </el-tree>
  67. </div>
  68. </div>
  69. </template>
  70. <style lang="scss" scoped>
  71. :deep(.el-tree-node) {
  72. padding-top: 8px;
  73. font-size: 14px;
  74. }
  75. </style>