index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <view class="jnpf-area-select">
  3. <u-input input-align='right' type="select" :select-open="selectShow" v-model="innerValue"
  4. :placeholder="placeholder" @click="openSelect" />
  5. <Tree v-if="selectShow" v-model="selectShow" :multiple="multiple" :props="props" :selectedData="selectedData"
  6. :level='level' :ids='modelValue' @confirm="handleConfirm" @close="handleClose()" />
  7. </view>
  8. </template>
  9. <script>
  10. import Tree from './Tree.vue';
  11. import {
  12. getProvinceSelectorInfoList
  13. } from '@/api/common.js'
  14. export default {
  15. name: 'jnpf-area-select',
  16. components: {
  17. Tree
  18. },
  19. props: {
  20. modelValue: {
  21. default: ''
  22. },
  23. placeholder: {
  24. type: String,
  25. default: '请选择'
  26. },
  27. props: {
  28. type: Object,
  29. default: () => ({
  30. label: 'fullName',
  31. value: 'id',
  32. children: 'children',
  33. isLeaf: 'isLeaf'
  34. })
  35. },
  36. disabled: {
  37. type: Boolean,
  38. default: false
  39. },
  40. multiple: {
  41. type: Boolean,
  42. default: false
  43. },
  44. level: {
  45. type: Number,
  46. default: 2
  47. }
  48. },
  49. watch: {
  50. modelValue: {
  51. handler(val) {
  52. this.setDefault(val)
  53. },
  54. immediate: true
  55. }
  56. },
  57. data() {
  58. return {
  59. selectShow: false,
  60. innerValue: '',
  61. selectedData: []
  62. }
  63. },
  64. methods: {
  65. setDefault(id) {
  66. this.innerValue = ''
  67. this.selectedData = []
  68. if (!Array.isArray(id) || id.length === 0) return
  69. if (!this.multiple) id = [id]
  70. getProvinceSelectorInfoList(id).then(res => {
  71. const list = res.data
  72. let txt = ''
  73. for (let i = 0; i < list.length; i++) {
  74. txt += (i ? ',' : '') + list[i].join('/')
  75. this.selectedData.push(list[i].join('/'))
  76. }
  77. this.innerValue = txt
  78. })
  79. },
  80. openSelect() {
  81. if (this.disabled) return
  82. this.selectShow = true
  83. },
  84. handleClose() {
  85. this.selectShow = false
  86. },
  87. handleConfirm(e, selectId, selectData) {
  88. this.selectedData = e;
  89. let label = '';
  90. let value = [];
  91. this.defaultValue = value
  92. this.innerValue = e.join()
  93. if (!this.multiple) {
  94. this.$emit('update:modelValue', selectId[0])
  95. this.$emit('change', selectId[0], selectData)
  96. } else {
  97. this.$emit('update:modelValue', selectId)
  98. this.$emit('change', selectId, selectData)
  99. }
  100. },
  101. }
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. .jnpf-area-select {
  106. width: 100%;
  107. }
  108. </style>