index.vue 4.6 KB

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