orgDropDown.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <treeselect v-model="val" :options="deptOptions" :normalizer="normalizer" v-bind="$attrs" @select="select" ref="tree"
  3. noResultsText="暂无符合条件的数据" clearValueText="清除" />
  4. </template>
  5. <script>
  6. import Treeselect from "@riophae/vue-treeselect";
  7. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  8. import { deptTreeSelect } from "@/api/system/public";
  9. import sync from "@/components/computed.sync.js";
  10. export default {
  11. name: "orgDropTree",
  12. data() {
  13. return {
  14. deptOptions: [],
  15. currentNode: null,
  16. };
  17. },
  18. props: {
  19. value: {
  20. type: String,
  21. },
  22. label: {
  23. type: String,
  24. default: "shortName",
  25. },
  26. },
  27. watch: {
  28. value(v) {
  29. if(!v){
  30. return;
  31. }
  32. if (!this.currentNode || this.currentNode.id != v) {
  33. this.currentNode = this.findNodeInOptions(v);
  34. }
  35. if (this.currentNode) {
  36. console.info(this.currentNode)
  37. this.$emit("select", this.currentNode);
  38. }
  39. }
  40. },
  41. computed: {
  42. val: sync("value"),
  43. },
  44. components: { Treeselect },
  45. methods: {
  46. /** 查询机构下拉树结构 */
  47. getDeptTree() {
  48. this.deptOptions = this.$store.getters.businessTree;
  49. if (this.deptOptions && this.deptOptions.length > 0) {
  50. if (this.val) {
  51. this.currentNode = this.findNodeInOptions(this.val);
  52. if (this.currentNode) {
  53. this.$emit("select", this.currentNode);
  54. }
  55. }
  56. }
  57. },
  58. findNodeInOptions(id) {
  59. if (!id) {
  60. return;
  61. }
  62. let func = (nodes) => {
  63. if (!nodes || nodes.length == 0) {
  64. return null;
  65. }
  66. let n = nodes.find(n => n.id == id);
  67. if (n) {
  68. return n;
  69. }
  70. for (let index in nodes) {
  71. let node = nodes[index]
  72. n = func(node.children);
  73. if (n) {
  74. return n;
  75. }
  76. }
  77. }
  78. return func(this.deptOptions)
  79. },
  80. /** 转换机构数据结构 */
  81. normalizer(node) {
  82. if (node.children == null || node.children.length == 0) {
  83. delete node.children;
  84. }
  85. return {
  86. id: node.id,
  87. label: node[this.label],
  88. children: node.children,
  89. };
  90. },
  91. select(node) {
  92. this.currentNode = node;
  93. },
  94. },
  95. mounted() {
  96. this.getDeptTree();
  97. },
  98. };
  99. </script>
  100. <style scoped lang="scss">
  101. ::v-deep {
  102. .vue-treeselect__menu {
  103. overflow-x: auto !important;
  104. width: 250px;
  105. max-height: 300px !important;
  106. }
  107. .vue-treeselect__label {
  108. overflow: unset;
  109. text-overflow: unset;
  110. }
  111. .vue-treeselect div,
  112. .vue-treeselect span {
  113. box-sizing: content-box;
  114. // white-space: nowrap;
  115. // text-overflow: ellipsis;
  116. }
  117. // 选中后的溢出隐藏
  118. .vue-treeselect__multi-value-label {
  119. display: block;
  120. width: 140px;
  121. overflow: hidden;
  122. white-space: nowrap;
  123. text-overflow: ellipsis;
  124. }
  125. }
  126. </style>