simpleCascader.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <uni-data-picker v-model="select" :placeholder="placeholder" popup-title="请选择"
  3. :localdata="options"
  4. @change="onchange" @nodeclick="onnodeclick">
  5. </uni-data-picker>
  6. </template>
  7. <script>
  8. import {
  9. getAllChildren
  10. } from '@/api/system'
  11. export default {
  12. name: "simpleCascader",
  13. props: {
  14. value: {
  15. type: [String, Number],
  16. required: false,
  17. default: null,
  18. },
  19. data: {
  20. type: [String, Array],
  21. required: true
  22. },
  23. loadType: {
  24. type: String,
  25. required: false,
  26. default: "local"
  27. },
  28. field: {
  29. type: String,
  30. required: false,
  31. default: "default"
  32. },
  33. ext: {
  34. type: Object,
  35. required: false,
  36. default: () => ({}),
  37. },
  38. readonly: {
  39. type: Boolean,
  40. required: false,
  41. default: false,
  42. },
  43. placeholder: {
  44. type: String,
  45. required: false,
  46. default: '请选择',
  47. }
  48. },
  49. computed: {
  50. isLocal() {
  51. return this.loadType === 'local'
  52. },
  53. isDict() {
  54. return this.loadType === 'dict'
  55. },
  56. isStatic() {
  57. return this.loadType === 'static'
  58. }
  59. },
  60. data() {
  61. return {
  62. select: this.value,
  63. options: []
  64. };
  65. },
  66. watch: {
  67. value(n, o) {
  68. this.select = n
  69. }
  70. },
  71. mounted() {
  72. this.init()
  73. },
  74. methods: {
  75. init() {
  76. this.options = []
  77. if(this.isDict) {
  78. this.loadDict()
  79. }else if(this.isLocal) {
  80. this.options = this.data
  81. }else if(this.isStatic) {
  82. }
  83. },
  84. parseList(list) {
  85. return list.map(d => {
  86. let children = null
  87. if(d.children && d.children.length > 0) {
  88. children = this.parseList(d.children)
  89. }
  90. const ret = {
  91. text: d.enumname,
  92. raw: d,
  93. }
  94. if (this.field === 'default') {
  95. ret.value = d.enumid
  96. } else if (this.field.startsWith('raw.')) {
  97. const [
  98. _,
  99. f
  100. ] = this.field.split('\.')
  101. ret.value = d[f]
  102. }
  103. if(children) {
  104. ret.children = children
  105. }
  106. return ret
  107. })
  108. },
  109. loadDict() {
  110. getAllChildren({
  111. enumid: this.data,
  112. isQueryFristNode: false,
  113. ... this.ext
  114. }).then(resp => {
  115. const list = resp.data || []
  116. this.options = this.parseList(list)
  117. })
  118. },
  119. onchange() {
  120. this.$emit('input', this.select)
  121. },
  122. onnodeclick() {
  123. }
  124. }
  125. }
  126. </script>
  127. <style>
  128. </style>