treeHouse.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <div class="organ-tree">
  3. <div v-show="showHouseTree">
  4. <div>
  5. <el-input v-model="filterText" v-if="!showCheckboxTree" placeholder="请输入关键字" suffix-icon="el-icon-search"></el-input>
  6. <el-input
  7. v-model="selectHouse"
  8. placeholder="选择的房间"
  9. @focus="inputFocue"
  10. maxlength="10"
  11. suffix-icon="el-icon-search"
  12. v-else
  13. ></el-input>
  14. </div>
  15. <div class="tree-style-box no-scrollbar" v-if="organList">
  16. <el-tree
  17. class="tree-style"
  18. :data="organList"
  19. ref="tree"
  20. node-key="id"
  21. :highlight-current="true"
  22. :props="defaultProps"
  23. :expand-on-click-node="false"
  24. @check="clickCheckTree"
  25. :default-expand-all="defaultExpandAllTree"
  26. :filter-node-method="filterNode"
  27. :show-checkbox="showCheckboxTree"
  28. :accordion="accordion"
  29. :prevDetailData="prevDetailData"
  30. :default-expanded-keys="defaultSelectAll"
  31. :check-on-click-node="true"
  32. >
  33. </el-tree>
  34. </div>
  35. </div>
  36. <div v-show="!showHouseTree" v-if="dataPeopleList">
  37. <el-input v-model="selectPeople" disabled placeholder="选择的人员" maxlength="10" suffix-icon="el-icon-search"></el-input>
  38. <div class="tree-style-box no-scrollbar">
  39. <el-tree
  40. class="tree-style"
  41. :data="dataPeopleList"
  42. ref="treePeople"
  43. node-key="id"
  44. :highlight-current="true"
  45. :props="defaultPropsPeople"
  46. :expand-on-click-node="false"
  47. @check="clickCheckTreePeople"
  48. :default-expand-all="defaultExpandAllTree"
  49. :filter-node-method="filterNodePeople"
  50. :show-checkbox="showCheckboxTree"
  51. :accordion="accordionPeople"
  52. :selectAll="selectAll"
  53. :prevDetailData="prevDetailData"
  54. :check-on-click-node="true"
  55. >
  56. </el-tree>
  57. </div>
  58. </div>
  59. </div>
  60. </template>
  61. <script>
  62. export default {
  63. name: 'treeHouse',
  64. props: {
  65. buildingType: { type: String, default: 'buildingType' },
  66. showCheckboxTree: {
  67. //显示多选框
  68. type: Boolean,
  69. default: false
  70. },
  71. disabledHouse: {
  72. type: Boolean,
  73. default: true
  74. },
  75. disabledPeople: {
  76. type: Boolean,
  77. default: true
  78. },
  79. defaultExpandAllTree: {
  80. //是否默认展开所有节点
  81. type: Boolean,
  82. default: true
  83. },
  84. showHouseTree: {
  85. //显示房间树
  86. type: Boolean,
  87. default: true
  88. },
  89. accordion: {
  90. //房间展开手风琴
  91. type: Boolean,
  92. default: false
  93. },
  94. accordionPeople: {
  95. //人员展开手风琴
  96. type: Boolean,
  97. default: false
  98. },
  99. prevDetailData: {
  100. type: Array,
  101. default() {
  102. return [];
  103. }
  104. }
  105. },
  106. data() {
  107. return {
  108. filterText: '',
  109. selectHouse: '',
  110. selectPeople: '',
  111. organList: [],
  112. dataPeopleList: [],
  113. defaultSelectAll: [],
  114. defaultProps: {
  115. children: 'children',
  116. label: 'name'
  117. },
  118. defaultPropsPeople: {
  119. children: 'children',
  120. label: 'label'
  121. }
  122. };
  123. },
  124. watch: {
  125. selectHouse(val) {
  126. // this.$refs.tree.filter(val);
  127. },
  128. dataPeopleList(val) {
  129. console.log('====================================');
  130. console.log('dataPeopleList', val);
  131. console.log('====================================');
  132. this.$refs.tree.filter(val);
  133. }
  134. },
  135. computed: {},
  136. methods: {
  137. // 输入框获得焦点
  138. inputFocue() {},
  139. // 过滤选中的社区下的房间
  140. filterhouse(val, datas) {
  141. let array = datas;
  142. let data;
  143. for (let index = 0; index < array.length; index++) {
  144. const element = array[index];
  145. if (element.id == val && element.children) {
  146. data = element.children;
  147. }
  148. }
  149. this.$nextTick(() => {
  150. this.organList = data;
  151. this.defaultSelectAll = data;
  152. this.selectAllHouse();
  153. this.clickCheckTree();
  154. // this.$refs.tree.setCheckedNodes('C1栋');
  155. });
  156. console.log('this.$refs.tree.setCheckedNodes(this.organList);', this.organList);
  157. },
  158. // 选中所有房间
  159. selectAllHouse() {
  160. this.$nextTick(() => {
  161. this.$refs.tree.setCheckedNodes(this.organList);
  162. this.clickCheckTree();
  163. });
  164. },
  165. // 选中指定房间
  166. selectHouseOr() {
  167. this.$nextTick(() => {
  168. this.$refs.tree.setCheckedKeys([]);
  169. this.clickCheckTree();
  170. });
  171. },
  172. // 选中所有人员
  173. selectAllPeople() {
  174. this.$nextTick(() => {
  175. this.$refs.treePeople.setCheckedNodes(this.dataPeopleList);
  176. this.clickCheckTreePeople();
  177. });
  178. },
  179. // 选中指定人员
  180. selectPeopleOr() {
  181. this.$nextTick(() => {
  182. this.$refs.treePeople.setCheckedKeys([]);
  183. this.clickCheckTreePeople();
  184. });
  185. },
  186. // 获取人员
  187. getPeopleList() {
  188. this.$http.get('/sc-user-center/user/findUserList').then(({ status, data, msg }) => {
  189. if (status === 0) {
  190. this.dataPeopleList = data;
  191. this.$emit('dataPeople', data);
  192. } else {
  193. this.$message(warning, '获取人员失败,请稍后重试');
  194. }
  195. console.log('获取人员', data);
  196. });
  197. },
  198. getSelect(data) {
  199. var str = [];
  200. const getStr = function (list) {
  201. list.forEach(function (row) {
  202. if (row.children) {
  203. getStr(row.children);
  204. } else {
  205. str.push(row.value);
  206. }
  207. });
  208. };
  209. getStr(data);
  210. return str;
  211. console.log('getStr', str);
  212. },
  213. // 多选框返回选中房间的数据
  214. clickCheckTree(val) {
  215. let tree = this.$refs.tree;
  216. let nameArr = [];
  217. let array = tree.getCheckedNodes();
  218. for (let index = 0; index < array.length; index++) {
  219. const element = array[index];
  220. nameArr.push(element.name);
  221. if (Array.isArray(element) && element.length > 0) {
  222. nameArr.push(element.name);
  223. }
  224. }
  225. // 输入框显示的房间
  226. this.selectHouse = nameArr;
  227. var arr = [];
  228. array.forEach(function (item) {
  229. if (item.type === 'room' && Number(item.value) !== String) {
  230. arr.push(Number(item.value));
  231. }
  232. });
  233. let obj = {};
  234. obj.userList = arr;
  235. obj.checkData = array;
  236. console.log('多选框返回选中房间的数据', array);
  237. // 选中的房间id
  238. this.$emit('selectData', obj);
  239. },
  240. // 多选框返回选中人员的数据
  241. clickCheckTreePeople(val) {
  242. let nameArr = [];
  243. let tree = this.$refs.treePeople;
  244. let array = tree.getCheckedNodes();
  245. for (let index = 0; index < array.length; index++) {
  246. const element = array[index];
  247. nameArr.push(element.label);
  248. if (Array.isArray(element) && element.length > 0) {
  249. nameArr.push(element.label);
  250. }
  251. }
  252. let checkDatas = tree.getCheckedNodes();
  253. var a = [];
  254. for (let i in checkDatas) {
  255. if (checkDatas[i].children == null) {
  256. a.push(checkDatas[i].id);
  257. }
  258. }
  259. let obj = {};
  260. obj.userList = a;
  261. obj.checkData = array;
  262. console.log('多选框返回选中人员的数据', JSON.stringify(checkDatas));
  263. // 输入框显示的人员
  264. this.selectPeople = nameArr.toString();
  265. // 选中的人员id
  266. this.$emit('selectPeople', obj);
  267. },
  268. // filterPeoPle(arr) {
  269. // arr.map((item) => {
  270. // if (item.children) {
  271. // this.filterPeoPle(item.children);
  272. // } else if (item.children == null) {
  273. // item.id.concat(+',' + item.id);
  274. // }
  275. // });
  276. // },
  277. // dimension(arr) {
  278. // arr.map((item, index) => {
  279. // if (!!item.children) {
  280. // this.dimension(item.children);
  281. // if (item.type == 'unit') {
  282. // item.name = this.CheckChinese(item.name, '单元');
  283. // } else if (item.type == 'building') {
  284. // item.name = this.CheckChinese(item.name, '楼栋');
  285. // }
  286. // }
  287. // });
  288. // },
  289. // CheckChinese(val, name) {
  290. // var reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g');
  291. // let newVal = val;
  292. // if (!reg.test(val)) {
  293. // newVal = val + name;
  294. // }
  295. // return newVal;
  296. // },
  297. // getOrgTreeList() {
  298. // this.$http
  299. // .get('/sc-community/assets/tree/community/find', { buildingType: this.buildingType })
  300. // .then(({ status, data, msg }) => {
  301. // if (status === 0 && data) {
  302. // this.organList = data;
  303. // this.dimension(data);
  304. // this.$nextTick().then(() => {
  305. // const firstNode = document.querySelector('.el-tree-node');
  306. // firstNode.click();
  307. // });
  308. // }
  309. // });
  310. // },
  311. filterNode(value, data) {
  312. if (!value) return true;
  313. return data.name.indexOf(value) !== -1;
  314. },
  315. filterNodePeople(value, data) {
  316. if (!value) return true;
  317. return data.label.indexOf(value) !== -1;
  318. }
  319. },
  320. created() {
  321. // this.getOrgTreeList();
  322. this.getPeopleList();
  323. }
  324. };
  325. </script>
  326. <style lang="scss" scoped>
  327. .organ-tree {
  328. width: 260px;
  329. background: #ffffff;
  330. padding: 20px;
  331. box-sizing: border-box;
  332. float: left;
  333. height: 100%;
  334. overflow: auto;
  335. &::before {
  336. clear: both;
  337. }
  338. .tree-style-box {
  339. margin-top: 20px;
  340. max-height: calc(100vh - 200px);
  341. overflow: scroll;
  342. }
  343. }
  344. </style>