orgDropDown.vue 3.3 KB

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