chooseAreaModal.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <a-drawer
  3. title="选择区域"
  4. width="50%"
  5. :zIndex="100"
  6. :visible="visible"
  7. :get-container="false"
  8. :wrap-style="{ position: 'absolute' }"
  9. @close="visible = false">
  10. <div class="areaSetModal">
  11. <div :style="{ height: tableHeight+84.5+'px' }">
  12. <div>
  13. <a-checkbox id="chooseAreaModal-checkAll-btn" :checked="checkAll" @change="onCheckAllChange">全选</a-checkbox>
  14. </div>
  15. <div :style="{ height: tableHeight+30+'px' }" style="overflow-y:scroll;">
  16. <a-tree
  17. v-model="checkedKeys"
  18. checkable
  19. id="chooseAreaModal-tree"
  20. :auto-expand-parent="autoExpandParent"
  21. :tree-data="subregionData"
  22. :checkStrictly="true"
  23. :expanded-keys="parentArr"
  24. @check="onCheck"
  25. @expand="onExpand"
  26. :replaceFields="{children:'subareaAreaList', title:'name', key:'areaSn'}"
  27. />
  28. </div>
  29. </div>
  30. <div class="areaFooter">
  31. <a-button style="margin-right: 15px" @click="visible = false" :disabled="disabled" id="chooseAreaModal-close">取消</a-button>
  32. <a-button type="primary" @click="save" :disabled="disabled" id="chooseAreaModal-save">保存</a-button>
  33. </div>
  34. </div>
  35. </a-drawer>
  36. </template>
  37. <script>
  38. import { commonMixin } from '@/utils/mixin'
  39. // 接口
  40. import { subareaQueryAll } from '@/api/subarea'
  41. export default {
  42. mixins: [commonMixin],
  43. props: {
  44. showModal: {
  45. type: Boolean,
  46. default: false
  47. },
  48. chooseData: {
  49. type: Object,
  50. default: () => {
  51. return {}
  52. }
  53. }
  54. },
  55. watch: {
  56. showModal (newValue, oldValue) {
  57. this.visible = newValue
  58. },
  59. visible (newValue, oldValue) {
  60. if (!newValue) {
  61. this.checkedKeys = { checked: [], halfChecked: [] }
  62. this.checkAll = false
  63. this.$emit('close')
  64. }
  65. }
  66. },
  67. data () {
  68. return {
  69. autoExpandParent: true, // 是否展开子级数据
  70. checkAll: false, // 全选
  71. // 选中数据
  72. checkedKeys: {
  73. checked: [],
  74. halfChecked: []
  75. },
  76. subregionData: null, // 树数据
  77. tableHeight: 0, // 表格高度
  78. visible: false, // 是否打开弹窗
  79. disabled: false, // 取消、保存按钮禁用状态
  80. parentArr: []// 父级展开数据
  81. }
  82. },
  83. methods: {
  84. // 树 默认展开被选子级的父级
  85. onExpand (expandedKeys) {
  86. console.log('onExpand', expandedKeys)
  87. this.parentArr = expandedKeys
  88. this.autoExpandParent = false
  89. },
  90. // 获取区域数据
  91. getArea () {
  92. subareaQueryAll().then(res => {
  93. if (res.status == 200) {
  94. res.data && res.data.map(item => {
  95. item.name = item.subareaName
  96. item.areaSn = item.subareaSn
  97. if (item.subareaAreaList) {
  98. item.subareaAreaList.map(cd => {
  99. cd.name = cd.subareaAreaName
  100. cd.areaSn = cd.subareaAreaSn + '_'
  101. const falg = this.chooseData.checked.length > 0 ? this.chooseData.checked.includes(cd.subareaAreaSn + '_') : false
  102. if (falg) {
  103. this.parentArr.push(cd.subareaSn)
  104. }
  105. })
  106. }
  107. })
  108. this.subregionData = res.data
  109. this.checkedKeys = this.chooseData ? this.chooseData : []
  110. }
  111. })
  112. },
  113. // 保存
  114. save () {
  115. this.$emit('ok', this.checkedKeys)
  116. },
  117. // 全选
  118. onCheckAllChange (e) {
  119. this.checkAll = e.target.checked
  120. if (e.target.checked) {
  121. this.checkedKeys.checked = []
  122. this.checkedKeys.halfChecked = []
  123. this.getAllFun(this.subregionData)
  124. } else {
  125. this.checkedKeys.checked = []
  126. this.checkedKeys.halfChecked = []
  127. }
  128. },
  129. // 递归完成全选
  130. getAllFun (data) {
  131. if (data) {
  132. data.map((item, index) => {
  133. this.checkedKeys.checked.push(item.areaSn)
  134. if (item.subareaAreaList && item.subareaAreaList.length > 0) {
  135. this.getAllFun(item.subareaAreaList)
  136. }
  137. })
  138. }
  139. },
  140. // 选中 check
  141. onCheck (checkedKeys) {
  142. this.checkedKeys = checkedKeys
  143. },
  144. // 初始化
  145. pageInit () {
  146. const _this = this
  147. this.$nextTick(() => { // 页面渲染完成后的回调
  148. _this.setTableH()
  149. })
  150. this.getArea()
  151. },
  152. // 计算表格高度
  153. setTableH () {
  154. this.tableHeight = window.innerHeight - 325
  155. }
  156. }
  157. }
  158. </script>
  159. <style lang="less" scoped>
  160. .areaSetModal {
  161. width: 100%;
  162. .areaFooter {
  163. text-align:center;
  164. }
  165. }
  166. </style>