poptreeSelect.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <el-scrollbar class="el-scrollbar-byself thisColor" style="width: 100%">
  3. <el-tree
  4. :data="tenantsTree"
  5. show-checkbox
  6. node-key="value"
  7. :props="treedefaultProps"
  8. check-strictly
  9. :default-checked-keys="defaultcheckedkeys"
  10. ref="tenantstree"
  11. @check="checkChange"
  12. >
  13. </el-tree>
  14. </el-scrollbar>
  15. </template>
  16. <script>
  17. export default {
  18. props: ['params'],
  19. data() {
  20. return {
  21. tenantsTree: [],
  22. treedefaultProps: {
  23. children: 'children',
  24. label: 'name'
  25. },
  26. defaultcheckedkeys: [],
  27. unitPa: {
  28. buildingId: '',
  29. buildingName: '',
  30. unitId: '',
  31. unitName: '',
  32. houseId: '',
  33. houseName: '',
  34. type: ''
  35. }
  36. };
  37. },
  38. computed: {},
  39. mounted() {},
  40. methods: {
  41. submit() {
  42. if (this.$refs.tenantstree.getCheckedNodes().length) {
  43. let thisRoom = this.$refs.tenantstree.getCheckedNodes()[0];
  44. let thisObj = this.$refs.tenantstree.getNode(thisRoom);
  45. this.unitPa.type = thisRoom.type;
  46. let newValueIds = thisRoom.id.split('-');
  47. if (thisRoom.type == 'room') {
  48. this.unitPa.buildingName = newValueIds.length == 4 ? thisObj.parent.parent.data.name : thisObj.parent.data.name;
  49. this.unitPa.buildingId = newValueIds.length == 4 ? thisObj.parent.parent.data.value : thisObj.parent.data.value;
  50. this.unitPa.unitName = newValueIds.length == 4 ? thisObj.parent.data.name : '';
  51. this.unitPa.unitId = newValueIds.length == 4 ? thisObj.parent.data.value : '';
  52. this.unitPa.houseName = thisRoom.name;
  53. this.unitPa.houseId = thisRoom.value;
  54. }
  55. this.params.callback && this.params.callback(this.unitPa);
  56. this.$emit('close');
  57. } else {
  58. this.$message.error('请选择房间或取消');
  59. }
  60. },
  61. dimension(arr) {
  62. arr.map((item, index) => {
  63. if (!!item.children) {
  64. this.dimension(item.children);
  65. if (item.type == 'unit') {
  66. item.name = this.CheckChinese(item.name, '单元');
  67. } else if (item.type == 'building') {
  68. item.name = this.CheckChinese(item.name, '楼栋');
  69. }
  70. }
  71. });
  72. },
  73. checkChange(data, checked) {
  74. if (checked) {
  75. if (this.$refs.tenantstree.getCheckedNodes().length > 1) {
  76. this.$message({
  77. message: '只能选择一个房间!',
  78. type: 'error',
  79. showClose: true
  80. });
  81. this.$refs.tenantstree.setChecked(data, false);
  82. }
  83. }
  84. },
  85. CheckChinese(val, name) {
  86. var reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g');
  87. let newVal = val;
  88. if (!reg.test(val)) {
  89. newVal = val + name;
  90. }
  91. return newVal;
  92. },
  93. filterTreeData(trData) {
  94. trData.map((item, index) => {
  95. if (this.isNotEmpty(item.children)) {
  96. item.disabled = true;
  97. this.filterTreeData(item.children);
  98. } else {
  99. item.disabled = false;
  100. }
  101. });
  102. },
  103. isNotEmpty(arr) {
  104. return arr && Array.isArray(arr) && arr.length > 0;
  105. }
  106. },
  107. created() {
  108. this.dimension(this.params.tenantsTree);
  109. this.filterTreeData(this.params.tenantsTree);
  110. this.tenantsTree = this.params.tenantsTree;
  111. }
  112. };
  113. </script>
  114. <style lang="scss" scoped>
  115. // .thisColor /deep/ .el-tree .is-current {
  116. // color: #56c6ff;
  117. // }
  118. </style>