commonSelect.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <view>
  3. <u-input v-model="v" readonly placeholder="请选择">
  4. <template v-if="!readonly" slot="suffix">
  5. <u-button @tap="openSelect" text="点击选择" type="primary" size="small"></u-button>
  6. </template>
  7. </u-input>
  8. <action-sheet :actions="options" title="请选择" :show="showSelector" @closeOnClickOverlay="showSelector = false"
  9. @close="showSelector = false" @select="onSelect" />
  10. </view>
  11. </template>
  12. <script>
  13. import {
  14. str,
  15. clone
  16. } from '@/utils/tools';
  17. import * as allStatic from './options.js'
  18. import actionSheet from './u-action-sheet/my-action-sheet.vue'
  19. export default {
  20. name: "commonSelect",
  21. props: {
  22. value: {
  23. type: [String, Number],
  24. required: false,
  25. default: null,
  26. },
  27. data: {
  28. type: [String, Array],
  29. required: true
  30. },
  31. remote: {
  32. type: Boolean,
  33. required: false,
  34. default: false,
  35. },
  36. readonly: {
  37. type: Boolean,
  38. required: false,
  39. default: false,
  40. }
  41. },
  42. components: {
  43. // uInput
  44. actionSheet
  45. },
  46. data() {
  47. return {
  48. v: str(this.value, null),
  49. _name: this.getName(),
  50. options: [],
  51. showSelector: false,
  52. };
  53. },
  54. watch: {
  55. value(n, o) {
  56. this._vlaue = str(n, null)
  57. },
  58. data: {
  59. handler: function(n, o) {
  60. this.initOptions()
  61. },
  62. immediate: true,
  63. }
  64. },
  65. methods: {
  66. getName() {
  67. if (!this.v) {
  68. return ''
  69. }
  70. },
  71. openSelect() {
  72. uni.hideKeyboard()
  73. if (!this.readonly) {
  74. this.showSelector = true
  75. }
  76. },
  77. onSelect(n) {
  78. this.v = n.name
  79. console.log('select', arguments);
  80. },
  81. parseData(d) {
  82. return {
  83. name: d.text,
  84. value: d.value,
  85. raw: d,
  86. }
  87. },
  88. initOptions() {
  89. if (Array.isArray(this.data)) { // 本地数据
  90. this.options = clone(this.data).map(this.parseData)
  91. console.log(this.options);
  92. } else if(this.remote){ // 字典数据
  93. } else { // 静态数据
  94. this.options = allStatic[this.data].map(this.parseData)
  95. }
  96. }
  97. }
  98. }
  99. </script>
  100. <style>
  101. </style>