buildingTree.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <div class="organ-tree">
  3. <div>
  4. <el-input v-model="filterText" placeholder="请输入关键字" suffix-icon="el-icon-search"></el-input>
  5. </div>
  6. <div class="tree-style-box no-scrollbar" v-if="organList">
  7. <el-tree
  8. class="tree-style"
  9. :data="organList"
  10. ref="tree"
  11. node-key="id"
  12. :highlight-current="true"
  13. :props="defaultProps"
  14. :expand-on-click-node="false"
  15. @node-click="treeClick"
  16. @check="clickCheckTree"
  17. :default-expand-all="defaultExpandAllTree"
  18. :filter-node-method="filterNode"
  19. :show-checkbox="showCheckboxTree"
  20. :accordion="accordion"
  21. >
  22. </el-tree>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. name: 'buildingTree',
  29. props: {
  30. buildingType: { type: String, default: 'buildingType' },
  31. showCheckboxTree: {
  32. //显示多选框
  33. type: Boolean,
  34. default: false
  35. },
  36. defaultExpandAllTree: {
  37. //是否默认展开所有节点
  38. type: Boolean,
  39. default: true
  40. },
  41. accordion: {
  42. //房间展开手风琴
  43. type: Boolean,
  44. default: false
  45. },
  46. accordionPeople: {
  47. //人员展开手风琴
  48. type: Boolean,
  49. default: false
  50. }
  51. },
  52. data() {
  53. return {
  54. filterText: '',
  55. organList: [],
  56. defaultProps: {
  57. children: 'children',
  58. label: 'name'
  59. }
  60. };
  61. },
  62. watch: {
  63. filterText(val) {
  64. this.$refs.tree.filter(val);
  65. }
  66. },
  67. computed: {},
  68. methods: {
  69. dimension(arr) {
  70. arr.map((item, index) => {
  71. if (!!item.children && item.type !== 'unit' && item.type !== 'building') {
  72. this.dimension(item.children);
  73. } else {
  74. if (item.type === 'unit') {
  75. item.name = this.CheckChinese(item.name, '单元');
  76. } else if (item.type === 'building') {
  77. item.name = this.CheckChinese(item.name, '楼栋');
  78. }
  79. }
  80. });
  81. },
  82. getOrgTreeList() {
  83. this.$http
  84. .get('/sc-community/assets/tree/community/find', { buildingType: this.buildingType })
  85. .then(({ status, data, msg }) => {
  86. if (status === 0 && data) {
  87. this.organList = data;
  88. this.dimension(data);
  89. this.$nextTick().then(() => {
  90. const firstNode = document.querySelector('.el-tree-node');
  91. firstNode.click();
  92. });
  93. }
  94. });
  95. },
  96. filterNode(value, data) {
  97. if (!value) return true;
  98. return data.name.indexOf(value) !== -1;
  99. },
  100. filterNodePeople(value, data) {
  101. if (!value) return true;
  102. return data.label.indexOf(value) !== -1;
  103. },
  104. treeClick(e) {
  105. if (e.value == 0) return;
  106. let onData = '';
  107. let newValueIds = e.id.split('-');
  108. let thisE = this.$refs.tree.getNode(e);
  109. if (e.type == 'building') {
  110. onData = {
  111. communityId: thisE.parent.data.value,
  112. buildingId: e.value,
  113. unitId: '',
  114. roomId: ''
  115. };
  116. this.$emit('buildingInformation', onData);
  117. } else if (e.type == 'unit') {
  118. onData = {
  119. communityId: thisE.parent.parent.data.value,
  120. buildingId: thisE.parent.data.value,
  121. unitId: e.value,
  122. roomId: ''
  123. };
  124. this.$emit('buildingInformation', onData);
  125. } else if (e.type == 'room') {
  126. onData = {
  127. communityId: newValueIds.length == 4 ? thisE.parent.parent.parent.data.value : thisE.parent.parent.data.value,
  128. buildingId: newValueIds.length == 4 ? thisE.parent.parent.data.value : thisE.parent.data.value,
  129. unitId: newValueIds.length == 4 ? thisE.parent.data.value : '',
  130. roomId: e.value
  131. };
  132. this.$emit('buildingInformation', onData);
  133. } else {
  134. this.$emit('buildingInformation', e);
  135. }
  136. },
  137. CheckChinese(val, name) {
  138. var reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g');
  139. let newVal = val;
  140. if (!reg.test(val)) {
  141. newVal = val + name;
  142. }
  143. return newVal;
  144. }
  145. },
  146. created() {
  147. this.getOrgTreeList();
  148. }
  149. };
  150. </script>
  151. <style lang="scss" scoped>
  152. .organ-tree {
  153. width: 260px;
  154. background: #ffffff;
  155. padding: 20px;
  156. box-sizing: border-box;
  157. float: left;
  158. height: 100%;
  159. overflow: auto;
  160. &::before {
  161. clear: both;
  162. }
  163. .tree-style-box {
  164. margin-top: 20px;
  165. max-height: calc(100vh - 200px);
  166. overflow: scroll;
  167. }
  168. }
  169. </style>