simpleSelect copy.vue 2.1 KB

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