treeHouse.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. disabled
  9. placeholder="选择的房间"
  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. defaultExpandAllTree: {
  72. //是否默认展开所有节点
  73. type: Boolean,
  74. default: true
  75. },
  76. showHouseTree: {
  77. //显示房间树
  78. type: Boolean,
  79. default: true
  80. },
  81. accordion: {
  82. //房间展开手风琴
  83. type: Boolean,
  84. default: false
  85. },
  86. accordionPeople: {
  87. //人员展开手风琴
  88. type: Boolean,
  89. default: false
  90. },
  91. prevDetailData: {
  92. type: Array,
  93. default() {
  94. return [];
  95. }
  96. }
  97. },
  98. data() {
  99. return {
  100. filterText: '',
  101. selectHouse: '',
  102. selectPeople: '',
  103. organList: [],
  104. dataPeopleList: [],
  105. defaultSelectAll: [],
  106. defaultProps: {
  107. children: 'children',
  108. label: 'name'
  109. },
  110. defaultPropsPeople: {
  111. children: 'children',
  112. label: 'label'
  113. }
  114. };
  115. },
  116. watch: {
  117. filterText(val) {
  118. this.$refs.tree.filter(val);
  119. },
  120. dataPeopleList(val) {
  121. console.log('====================================');
  122. console.log('dataPeopleList', val);
  123. console.log('====================================');
  124. this.$refs.tree.filter(val);
  125. }
  126. },
  127. computed: {},
  128. methods: {
  129. // 过滤选中的社区下的房间
  130. filterhouse(val, datas) {
  131. let array = datas;
  132. let data;
  133. for (let index = 0; index < array.length; index++) {
  134. const element = array[index];
  135. if (element.id == val && element.children) {
  136. data = element.children;
  137. }
  138. }
  139. this.$nextTick(() => {
  140. this.organList = data;
  141. this.defaultSelectAll = data;
  142. this.selectAllHouse();
  143. this.clickCheckTree();
  144. // this.$refs.tree.setCheckedNodes('C1栋');
  145. });
  146. console.log(' this.$refs.tree.setCheckedNodes(this.organList);', this.organList);
  147. },
  148. // 选中所有房间
  149. selectAllHouse() {
  150. this.$nextTick(() => {
  151. this.$refs.tree.setCheckedNodes(this.organList);
  152. this.clickCheckTree();
  153. });
  154. },
  155. // 选中指定房间
  156. selectHouseOr() {
  157. this.$nextTick(() => {
  158. this.$refs.tree.setCheckedKeys([]);
  159. this.clickCheckTree();
  160. });
  161. },
  162. // 选中所有人员
  163. selectAllPeople() {
  164. this.$nextTick(() => {
  165. this.$refs.treePeople.setCheckedNodes(this.dataPeopleList);
  166. this.clickCheckTreePeople();
  167. });
  168. },
  169. // 选中指定人员
  170. selectPeopleOr() {
  171. this.$nextTick(() => {
  172. this.$refs.treePeople.setCheckedKeys([]);
  173. this.clickCheckTreePeople();
  174. });
  175. },
  176. // 获取人员
  177. getPeopleList() {
  178. this.$http.get('/sc-user-center/user/findUserList').then(({ status, data, msg }) => {
  179. if (status === 0) {
  180. this.dataPeopleList = data;
  181. this.$emit('dataPeople', data);
  182. } else {
  183. this.$message(warning, '获取人员失败,请稍后重试');
  184. }
  185. console.log('获取人员', data);
  186. });
  187. },
  188. getSelect(data) {
  189. var str = [];
  190. const getStr = function (list) {
  191. list.forEach(function (row) {
  192. if (row.children) {
  193. getStr(row.children);
  194. } else {
  195. str.push(row.value);
  196. }
  197. });
  198. };
  199. getStr(data);
  200. return str;
  201. console.log('getStr', str);
  202. },
  203. // 多选框返回选中房间的数据
  204. clickCheckTree(val) {
  205. let tree = this.$refs.tree;
  206. let nameArr = [];
  207. let array = tree.getCheckedNodes();
  208. let arrays = tree.getCheckedKeys();
  209. for (let index = 0; index < array.length; index++) {
  210. const element = array[index];
  211. nameArr.push(element.name);
  212. if (Array.isArray(element) && element.length > 0) {
  213. nameArr.push(element.name);
  214. }
  215. }
  216. // 输入框显示的房间
  217. this.selectHouse = nameArr;
  218. var arr = [];
  219. array.forEach(function (item) {
  220. if (item.type === 'room' && Number(item.value) !== String) {
  221. arr.push(Number(item.value));
  222. }
  223. });
  224. let obj = {};
  225. obj.userList = arr;
  226. obj.checkData = array;
  227. console.log('多选框返回选中房间的数据', array);
  228. // 选中的房间id
  229. this.$emit('selectData', obj);
  230. },
  231. // 多选框返回选中人员的数据
  232. clickCheckTreePeople(val) {
  233. let nameArr = [];
  234. let tree = this.$refs.treePeople;
  235. let array = tree.getCheckedNodes();
  236. let arrays = tree.getCheckedKeys();
  237. for (let index = 0; index < array.length; index++) {
  238. const element = array[index];
  239. nameArr.push(element.label);
  240. if (Array.isArray(element) && element.length > 0) {
  241. nameArr.push(element.label);
  242. }
  243. }
  244. let obj = {};
  245. obj.userList = arrays;
  246. obj.checkData = array;
  247. let checkDatas = tree.getCheckedNodes();
  248. // let data = tree.getCheckedKeys();
  249. // [
  250. // {
  251. // id: 1,
  252. // label: 'xxx',
  253. // parentId: 0,
  254. // children: [
  255. // {
  256. // id: 2,
  257. // label: 'xxx',
  258. // parentId: 1
  259. // }
  260. // ]
  261. // }
  262. // ];
  263. // array.map((t) => {});
  264. console.log('多选框返回选中人员的数据', JSON.stringify(checkDatas));
  265. console.log('====================================');
  266. // 输入框显示的人员
  267. this.selectPeople = nameArr.toString();
  268. // 选中的人员id
  269. this.$emit('selectPeople', obj);
  270. },
  271. dimension(arr) {
  272. arr.map((item, index) => {
  273. if (!!item.children) {
  274. this.dimension(item.children);
  275. if (item.type == 'unit') {
  276. item.name = this.CheckChinese(item.name, '单元');
  277. } else if (item.type == 'building') {
  278. item.name = this.CheckChinese(item.name, '楼栋');
  279. }
  280. }
  281. });
  282. },
  283. CheckChinese(val, name) {
  284. var reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g');
  285. let newVal = val;
  286. if (!reg.test(val)) {
  287. newVal = val + name;
  288. }
  289. return newVal;
  290. },
  291. getOrgTreeList() {
  292. this.$http
  293. .get('/sc-community/assets/tree/community/find', { buildingType: this.buildingType })
  294. .then(({ status, data, msg }) => {
  295. if (status === 0 && data) {
  296. this.organList = data;
  297. this.dimension(data);
  298. this.$nextTick().then(() => {
  299. const firstNode = document.querySelector('.el-tree-node');
  300. firstNode.click();
  301. });
  302. }
  303. });
  304. },
  305. filterNode(value, data) {
  306. if (!value) return true;
  307. return data.name.indexOf(value) !== -1;
  308. },
  309. filterNodePeople(value, data) {
  310. if (!value) return true;
  311. return data.label.indexOf(value) !== -1;
  312. }
  313. // treeClick(e) {
  314. // if (e.value == 0) return;
  315. // let onData = '';
  316. // let newValueIds = e.id.split('-');
  317. // let thisE = this.$refs.tree.getNode(e);
  318. // if (e.type == 'building') {
  319. // onData = {
  320. // communityId: thisE.parent.data.value,
  321. // buildingId: e.value,
  322. // unitId: '',
  323. // roomId: ''
  324. // };
  325. // this.$emit('buildingInformation', onData);
  326. // } else if (e.type == 'unit') {
  327. // onData = {
  328. // communityId: thisE.parent.parent.data.value,
  329. // buildingId: thisE.parent.data.value,
  330. // unitId: e.value,
  331. // roomId: ''
  332. // };
  333. // this.$emit('buildingInformation', onData);
  334. // } else if (e.type == 'room') {
  335. // onData = {
  336. // communityId: newValueIds.length == 4 ? thisE.parent.parent.parent.data.value : thisE.parent.parent.data.value,
  337. // buildingId: newValueIds.length == 4 ? thisE.parent.parent.data.value : thisE.parent.data.value,
  338. // unitId: newValueIds.length == 4 ? thisE.parent.data.value : '',
  339. // roomId: e.value
  340. // };
  341. // this.$emit('buildingInformation', onData);
  342. // } else {
  343. // this.$emit('buildingInformation', e);
  344. // }
  345. // }
  346. },
  347. created() {
  348. // this.getOrgTreeList();
  349. this.getPeopleList();
  350. }
  351. };
  352. </script>
  353. <style lang="scss" scoped>
  354. .organ-tree {
  355. width: 260px;
  356. background: #ffffff;
  357. padding: 20px;
  358. box-sizing: border-box;
  359. float: left;
  360. height: 100%;
  361. overflow: auto;
  362. &::before {
  363. clear: both;
  364. }
  365. .tree-style-box {
  366. margin-top: 20px;
  367. max-height: calc(100vh - 200px);
  368. overflow: scroll;
  369. }
  370. }
  371. </style>