jp-area-select.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view>
  3. <view class="action arrow" @tap="showModal" data-target="bottomModal">
  4. <view class="uni-input upicker ellipsis-description" >{{labels?labels:'请选择'}}</view>
  5. </view>
  6. <view class="cu-modal bottom-modal" style="min-height: 200upx;" :class="modalName=='bottomModal'?'show':''">
  7. <view class="cu-dialog">
  8. <view class="cu-bar bg-white">
  9. <view class="action text-blue" @tap="hideModal">取消</view>
  10. <view class="action text-green" @tap="selectArea">确定</view>
  11. </view>
  12. <view>
  13. <ly-tree :tree-data="data"
  14. :props="props"
  15. node-key="id"
  16. :checkOnClickNode ="true"
  17. :showRadio="showRadio"
  18. :show-checkbox ="showCheckBox"
  19. :checkOnlyLeaf = "checkOnlyLeaf"
  20. ref="userTree" />
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import areaService from "@/api/sys/areaService"
  28. export default {
  29. data() {
  30. return {
  31. data: [],
  32. modalName: null,
  33. labels: null,
  34. treeList: []
  35. };
  36. },
  37. props: {
  38. limit: Number,
  39. value: String,
  40. size: String,
  41. readonly: {
  42. type: Boolean,
  43. default: () => { return false }
  44. },
  45. checkOnlyLeaf: {
  46. type: Boolean,
  47. default: () => { return false }
  48. },
  49. showRadio: {
  50. type: Boolean,
  51. default: () => { return true }
  52. },
  53. showCheckBox: {
  54. type: Boolean,
  55. default: () => { return false }
  56. },
  57. disabled: {
  58. type: Boolean,
  59. default: () => { return false }
  60. },
  61. props: {
  62. type: Object,
  63. default: () => {
  64. return {
  65. children: 'children',
  66. label: 'name'
  67. }
  68. }
  69. }
  70. },
  71. mounted() {
  72. areaService.treeData().then(({data})=>{
  73. this.data = data
  74. this.setTreeList(this.data)
  75. let labelArra = []
  76. if(this.value){
  77. this.treeList.forEach((area) => {
  78. if (this.value === area.id) {
  79. labelArra.push(area.name)
  80. }
  81. })
  82. this.labels = labelArra.join(',')
  83. }
  84. }).catch((e)=>{
  85. throw e
  86. })
  87. },
  88. methods:{
  89. setTreeList (datas) { // 遍历树 获取id数组
  90. for (var i in datas) {
  91. this.treeList.push(datas[i])
  92. if (datas[i].children) {
  93. this.setTreeList(datas[i].children)
  94. }
  95. }
  96. },
  97. selectArea () {
  98. let ids = this.$refs.userTree.getCheckedNodes().map((item)=>{
  99. return item.id
  100. }).join(",");
  101. let names = this.$refs.userTree.getCheckedNodes().map((item)=>{
  102. return item.name
  103. }).join(",");
  104. this.labels = names
  105. this.$emit('input', ids)
  106. this.hideModal()
  107. },
  108. showModal(e) {
  109. if(this.disabled){
  110. return
  111. }
  112. this.modalName = e.currentTarget.dataset.target
  113. },
  114. hideModal(e) {
  115. this.modalName = null
  116. }
  117. }
  118. }
  119. </script>