index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <div class="org-tree">
  3. <div class="head-container">
  4. <el-input
  5. v-model="deptName"
  6. :placeholder="searchPlaceHolder"
  7. clearable
  8. size="small"
  9. prefix-icon="el-icon-search"
  10. maxlength="50"
  11. style="margin-bottom: 20px"
  12. />
  13. </div>
  14. <div class="tree-container">
  15. <div style="margin-bottom: 10px" v-show="showLowerCheck">
  16. <el-checkbox v-model="checkSub" @change="changeCheckBox"
  17. >关联所有下级数据</el-checkbox>
  18. </div>
  19. <el-collapse v-model="activeNames">
  20. <el-collapse-item title="福建农信" name="1">
  21. <el-tree
  22. :data="treeList"
  23. :props="defaultProps"
  24. :expand-on-click-node="false"
  25. :filter-node-method="filterNode"
  26. ref="tree"
  27. node-key="id"
  28. class="el-tree-ex"
  29. :default-expanded-keys="defaultKeys"
  30. :default-checked-keys="defaultKeys"
  31. @node-click="handleNodeClick"
  32. v-bind="$attrs"
  33. >
  34. <span class="custom-tree-node" slot-scope="{ node, data }">
  35. <span>{{ node.label }}</span>
  36. </span>
  37. </el-tree>
  38. </el-collapse-item>
  39. </el-collapse>
  40. </div>
  41. </div>
  42. </template>
  43. <script>
  44. import { mapGetters, mapMutations } from "vuex";
  45. export default {
  46. name: "orgTree",
  47. data() {
  48. return {
  49. //过滤信息
  50. activeNames: ["1"],
  51. deptName: "",
  52. // 机构树列表
  53. treeList: [],
  54. //默认选中节点
  55. defaultKeys: [],
  56. checkSub: this.defaultCheckSub,
  57. defaultKey: null,
  58. };
  59. },
  60. props: {
  61. hangsheTree: {
  62. type: Boolean,
  63. default: false,
  64. },
  65. businessTree: {
  66. type: Boolean,
  67. default: false,
  68. },
  69. wholeTree: {
  70. type: Boolean,
  71. default: false,
  72. },
  73. defaultProps: {
  74. type: Object,
  75. default: () => {
  76. return {
  77. children: "children",
  78. label: "shortName",
  79. };
  80. },
  81. },
  82. customRequest: {
  83. type: Function,
  84. },
  85. showLowerCheck: {
  86. type: Boolean,
  87. default: true,
  88. },
  89. searchPlaceHolder: {
  90. type: String,
  91. default: "请输入机构名称",
  92. },
  93. defaultCheckSub:{
  94. type:Boolean,
  95. default:true
  96. }
  97. },
  98. watch: {
  99. // 根据名称筛选机构树
  100. deptName(val) {
  101. this.$refs.tree.filter(val);
  102. },
  103. defaultProps(val) {
  104. this.defaultProps = { ...val };
  105. },
  106. orgTree(val)
  107. {
  108. this.getDeptTree();
  109. },
  110. },
  111. created() {
  112. this.getDeptTree();
  113. },
  114. computed:{
  115. ...mapGetters(["orgTree"]),
  116. },
  117. mounted() {
  118. this.getHeight();
  119. },
  120. methods: {
  121. getHeight(){
  122. let orgTree = document.querySelector('.org-tree');
  123. const resizeObserver = new ResizeObserver(entries => {
  124. for (let entry of entries) {
  125. orgTree.style.height = entry.contentRect.height + 'px';
  126. }
  127. });
  128. resizeObserver.observe(document.querySelector('.main-right-box'));
  129. },
  130. /** 下穿状态改变*/
  131. changeCheckBox(state) {
  132. this.$emit("checkChange", state);
  133. },
  134. /** 查询机构下拉树结构 */
  135. getDeptTree() {
  136. if (this.customRequest) {
  137. this.customRequest().then((response) => {
  138. this.treeList = response.data;
  139. this.dataFn(response.data);
  140. return;
  141. });
  142. } else {
  143. console.log("getDeptTree",this.hangsheTree,this.businessTree,this.wholeTree)
  144. if (this.hangsheTree) {
  145. this.treeList = this.$store.getters.depTree;
  146. }
  147. else if(this.businessTree)
  148. {
  149. this.treeList = this.$store.getters.businessTree;
  150. }
  151. else if(this.wholeTree)
  152. {
  153. console.log("wholeTree",this.$store.getters.wholeTree)
  154. this.treeList = this.$store.getters.wholeTree;
  155. }
  156. else {
  157. this.treeList = this.$store.getters.orgTree;
  158. }
  159. this.dataFn(this.treeList);
  160. }
  161. },
  162. dataFn(arr){
  163. if(!arr||arr.length===0) return;
  164. console.log(arr,'arrr')
  165. this.defaultKeys.push(arr[0].id);
  166. this.$emit("defaultKey", arr[0].id);
  167. this.$emit("defaultOrg", arr[0]);
  168. this.defaultKey = arr[0].id;
  169. setTimeout(() => {
  170. this.$refs.tree.setCurrentKey(arr[0].id);
  171. }, 100);
  172. },
  173. // 筛选节点
  174. filterNode(value, data) {
  175. if (!value) return true;
  176. return data.shortName.indexOf(value) !== -1;
  177. },
  178. // 节点单击事件
  179. handleNodeClick(data, node) {
  180. this.$emit("click", data, node);
  181. },
  182. },
  183. };
  184. </script>
  185. <style lang="scss" scoped>
  186. .org-tree{
  187. background-color: #fff;
  188. padding: 10px;
  189. box-shadow: 0 2px 8px #ccc;
  190. -min-height: calc(100vh - 107px);
  191. overflow: auto;
  192. }
  193. .el-tree-ex {
  194. ::v-deep .is-current > .el-tree-node__content {
  195. background-color: #d1e0f1 !important;
  196. display:inline-flex;
  197. min-width:100%;
  198. }
  199. }
  200. </style>