orgQuerySelector.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <div class="org_query_selector">
  3. <treeselect
  4. v-model="val"
  5. :options="treeList"
  6. :normalizer="normalizer"
  7. :default-expand-level="0"
  8. :clearable="clearable"
  9. v-bind="$attrs"
  10. @select="select"
  11. ref="tree"
  12. noResultsText="暂无符合条件的数据"
  13. noOptionsText="无数据"
  14. clearValueText="清除"
  15. :placeholder="placeholder"
  16. />
  17. <el-checkbox v-if="showCheckSub" v-model="checkSub" @change="changeCheckBox"
  18. >关联所有下级数据</el-checkbox
  19. >
  20. </div>
  21. </template>
  22. <script>
  23. import Treeselect from "@riophae/vue-treeselect";
  24. import { deptTreeSelect } from "@/api/system/public";
  25. import sync from "@/components/computed.sync.js";
  26. import { mapGetters, mapMutations } from "vuex";
  27. export default {
  28. name: "orgDropTree",
  29. data() {
  30. return {
  31. treeList: [],
  32. checkSub: this.defaultCheckSub,
  33. };
  34. },
  35. props: {
  36. value: {
  37. type: String,
  38. },
  39. multiple:{
  40. type:Boolean,
  41. default:true,
  42. },
  43. label: {
  44. type: String,
  45. default: "shortName",
  46. },
  47. orgTreeType: {
  48. //取值范围:business,org
  49. type: String,
  50. default: "business",
  51. },
  52. defaultCheckSub: {
  53. type: Boolean,
  54. default: true,
  55. },
  56. hangsheTree: {
  57. type: Boolean,
  58. default: false,
  59. },
  60. businessTree: {
  61. type: Boolean,
  62. default: false,
  63. },
  64. wholeTree: {
  65. type: Boolean,
  66. default: false,
  67. },
  68. customRequest: {
  69. type: Function,
  70. },
  71. showLowerCheck: {
  72. type: Boolean,
  73. default: true,
  74. },
  75. clearable: {
  76. type: Boolean,
  77. default: false,
  78. },
  79. placeholder:{
  80. type:String,
  81. default:'请选择机构'
  82. },
  83. showCheckSub:{
  84. type: Boolean,
  85. default: true,
  86. }
  87. },
  88. watch: {
  89. value(v) {
  90. //补充clear时不触发select事件
  91. if (!v) {
  92. this.$emit("click", null);
  93. return;
  94. }
  95. },
  96. orgTree(val) {
  97. this.getDeptTree();
  98. },
  99. },
  100. computed: {
  101. ...mapGetters(["orgTree"]),
  102. val: sync("value"),
  103. },
  104. components: { Treeselect },
  105. methods: {
  106. /** 查询机构下拉树结构 */
  107. getDeptTree() {
  108. if (this.customRequest) {
  109. this.customRequest().then((response) => {
  110. let treeList = response.data;
  111. this.dataFn(treeList);
  112. return;
  113. });
  114. } else {
  115. console.log(
  116. "getDeptTree",
  117. this.hangsheTree,
  118. this.businessTree,
  119. this.wholeTree
  120. );
  121. let treeList=null;
  122. if (this.hangsheTree) {
  123. treeList = this.$store.getters.depTree;
  124. } else if (this.businessTree) {
  125. treeList = this.$store.getters.businessTree;
  126. } else if (this.wholeTree) {
  127. treeList = this.$store.getters.wholeTree;
  128. } else {
  129. treeList = this.$store.getters.orgTree;
  130. }
  131. this.dataFn(treeList);
  132. }
  133. },
  134. dataFn(arr) {
  135. if (!arr || arr.length === 0) return;
  136. // console.log(arr, "arrr");
  137. if(arr==this.treeList){
  138. return;
  139. }
  140. if (arr && arr.length > 0) {
  141. if (arr.length == 1) {
  142. arr[0].isDefaultExpanded = true;
  143. }
  144. let defaultSelectedNode;
  145. if (!this.val) {
  146. this.val = arr[0].id;
  147. defaultSelectedNode = arr[0];
  148. // let v=this.val
  149. // debugger
  150. } else {
  151. if (this.value) {
  152. defaultSelectedNode = this.findNodeInOptions(this.value);
  153. }
  154. }
  155. if (defaultSelectedNode) {
  156. // console.trace();
  157. this.$emit("defaultKey", defaultSelectedNode.id);
  158. this.$emit("defaultOrg", { ...defaultSelectedNode });
  159. }
  160. this.treeList = arr;
  161. }
  162. // setTimeout(() => {
  163. // this.$refs.tree.setCurrentKey(arr[0].id);
  164. // }, 100);
  165. },
  166. findNodeInOptions(id) {
  167. if (!id) {
  168. return;
  169. }
  170. let func = (nodes) => {
  171. if (!nodes || nodes.length == 0) {
  172. return null;
  173. }
  174. let n = nodes.find((n) => n.id == id);
  175. if (n) {
  176. return n;
  177. }
  178. for (let index in nodes) {
  179. let node = nodes[index];
  180. n = func(node.children);
  181. if (n) {
  182. return n;
  183. }
  184. }
  185. };
  186. return func(this.treeList);
  187. },
  188. /** 转换机构数据结构 */
  189. normalizer(node) {
  190. if (node.children == null || node.children.length == 0) {
  191. delete node.children;
  192. }
  193. return {
  194. id: node.id,
  195. label: node[this.label],
  196. children: node.children,
  197. isDefaultExpanded: node.isDefaultExpanded,
  198. };
  199. },
  200. select(node) {
  201. this.$emit("click", node);
  202. },
  203. changeCheckBox(state) {
  204. this.$emit("checkChange", state);
  205. },
  206. setCheckSub(checked){
  207. this.checkSub=checked;
  208. },
  209. /**
  210. * 设置选中顶级节点
  211. */
  212. setSelectTop(){
  213. if(!this.treeList || this.treeList.length===0){
  214. return null;
  215. }
  216. let node= this.treeList[0];
  217. this.val=node.id
  218. return node;
  219. }
  220. },
  221. mounted() {
  222. this.getDeptTree();
  223. },
  224. };
  225. </script>
  226. <style lang="scss" scoped>
  227. .org_query_selector {
  228. display: flex;
  229. ::v-deep .vue-treeselect {
  230. width: 205px !important;
  231. }
  232. }
  233. .org_query_selector div:first-child {
  234. margin-right: 20px;
  235. }
  236. ::v-deep {
  237. .vue-treeselect__single-value {
  238. line-height: 30px;
  239. color: #666;
  240. }
  241. .vue-treeselect__control {
  242. height: 30px;
  243. }
  244. .vue-treeselect__input {
  245. height: 30px;
  246. line-height: 30px;
  247. }
  248. .el-checkbox__inner::after {
  249. left: 5px;
  250. top: 2px;
  251. }
  252. .vue-treeselect__menu {
  253. overflow-x: auto !important;
  254. width: 215px;
  255. max-height: 400px !important;
  256. }
  257. .vue-treeselect__label {
  258. overflow: unset;
  259. text-overflow: unset;
  260. }
  261. .vue-treeselect div,
  262. .vue-treeselect span {
  263. box-sizing: content-box;
  264. // white-space: nowrap;
  265. // text-overflow: ellipsis;
  266. }
  267. // 选中后的溢出隐藏
  268. .vue-treeselect__multi-value-label {
  269. display: block;
  270. width: 130px;
  271. overflow: hidden;
  272. white-space: nowrap;
  273. text-overflow: ellipsis;
  274. }
  275. }
  276. </style>