dept-select.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <!-- 部门树形选择下拉框 -->
  2. <template>
  3. <ele-tree-select
  4. clearable
  5. :value="value || ''"
  6. :data="treeData"
  7. label-key="name"
  8. value-key="id"
  9. default-expand-all
  10. :placeholder="placeholder"
  11. @input="updateValue"
  12. @change="changeChoose"
  13. ref="treeSelect"
  14. v-bind="$attrs"
  15. />
  16. </template>
  17. <script>
  18. import { listOrganizations } from '@/api/system/organization';
  19. export default {
  20. props: {
  21. // 选中的数据(v-model)
  22. value: [Number, String],
  23. // 提示信息
  24. placeholder: {
  25. type: String,
  26. default: '请选择'
  27. }
  28. },
  29. data() {
  30. return {
  31. treeData: []
  32. };
  33. },
  34. created() {
  35. this.getData();
  36. },
  37. methods: {
  38. async getData(parmas = {}) {
  39. const data = await listOrganizations(parmas);
  40. this.treeData = this.$util.toTreeData({
  41. data: data || [],
  42. idField: 'id',
  43. parentIdField: 'parentId'
  44. });
  45. },
  46. /* 更新选中数据 */
  47. updateValue(value) {
  48. this.$emit('input', value);
  49. },
  50. changeChoose(val) {
  51. this.$emit(
  52. 'changeGroup',
  53. val,
  54. this.$refs.treeSelect.getNodeByValue(val)
  55. );
  56. },
  57. async locateByName(code) {
  58. await this.getData();
  59. const res = this.findNodesByNameWithPath(this.treeData, code);
  60. console.log(res);
  61. if (!res.length) return;
  62. const { node, path } = res[0];
  63. this.$nextTick(() => {
  64. // 设置选中
  65. this.updateValue(node.id);
  66. this.changeChoose(node.id);
  67. // 展开树路径
  68. const tree = this.$refs.treeSelect?.$refs?.tree;
  69. if (!tree) return;
  70. path.forEach((p) => {
  71. const treeNode = tree.store.nodesMap[p.id];
  72. treeNode && treeNode.expand();
  73. });
  74. });
  75. },
  76. findNodesByNameWithPath(tree, code) {
  77. const result = [];
  78. function dfs(list, path = []) {
  79. list.forEach((node) => {
  80. const currentPath = [...path, node];
  81. if (node.groupCode == code) {
  82. result.push({
  83. node,
  84. path: currentPath
  85. });
  86. }
  87. if (node.children?.length) {
  88. dfs(node.children, currentPath);
  89. }
  90. });
  91. }
  92. dfs(tree);
  93. return result;
  94. }
  95. }
  96. };
  97. </script>