orgDropDown.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. 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. },
  28. watch: {
  29. value(v) {
  30. if(!v){
  31. return;
  32. }
  33. if (!this.currentNode || this.currentNode.id != v) {
  34. this.currentNode = this.findNodeInOptions(v);
  35. }
  36. if (this.currentNode) {
  37. console.info(this.currentNode)
  38. this.$emit("select", this.currentNode);
  39. }
  40. },orgTree(val)
  41. {
  42. this.getDeptTree();
  43. },
  44. },
  45. computed: {
  46. ...mapGetters(["orgTree"]),
  47. val: sync("value"),
  48. },
  49. components: { Treeselect },
  50. methods: {
  51. /** 查询机构下拉树结构 */
  52. getDeptTree() {
  53. this.deptOptions = this.$store.getters.businessTree;
  54. if (this.deptOptions && this.deptOptions.length > 0) {
  55. if (this.val) {
  56. this.currentNode = this.findNodeInOptions(this.val);
  57. if (this.currentNode) {
  58. this.$emit("select", this.currentNode);
  59. }
  60. }
  61. }
  62. },
  63. findNodeInOptions(id) {
  64. if (!id) {
  65. return;
  66. }
  67. let func = (nodes) => {
  68. if (!nodes || nodes.length == 0) {
  69. return null;
  70. }
  71. let n = nodes.find(n => n.id == id);
  72. if (n) {
  73. return n;
  74. }
  75. for (let index in nodes) {
  76. let node = nodes[index]
  77. n = func(node.children);
  78. if (n) {
  79. return n;
  80. }
  81. }
  82. }
  83. return func(this.deptOptions)
  84. },
  85. /** 转换机构数据结构 */
  86. normalizer(node) {
  87. if (node.children == null || node.children.length == 0) {
  88. delete node.children;
  89. }
  90. return {
  91. id: node.id,
  92. label: node[this.label],
  93. children: node.children,
  94. };
  95. },
  96. select(node) {
  97. this.currentNode = node;
  98. },
  99. },
  100. mounted() {
  101. this.getDeptTree();
  102. },
  103. };
  104. </script>
  105. <style scoped lang="scss">
  106. ::v-deep {
  107. .vue-treeselect__menu {
  108. overflow-x: auto !important;
  109. width: 250px;
  110. max-height: 300px !important;
  111. }
  112. .vue-treeselect__label {
  113. overflow: unset;
  114. text-overflow: unset;
  115. }
  116. .vue-treeselect div,
  117. .vue-treeselect span {
  118. box-sizing: content-box;
  119. // white-space: nowrap;
  120. // text-overflow: ellipsis;
  121. }
  122. // 选中后的溢出隐藏
  123. .vue-treeselect__multi-value-label {
  124. display: block;
  125. width: 140px;
  126. overflow: hidden;
  127. white-space: nowrap;
  128. text-overflow: ellipsis;
  129. }
  130. }
  131. </style>