my-select.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <view class="cu-form-group">
  3. <view class="title">{{label}}</view>
  4. <picker @change="PickerChange" :value="index" :range-key="rangeKey" :range="dictOptions">
  5. <view class="picker">
  6. {{index>-1?dictOptions[index][rangeKey]:'请选择'}}
  7. </view>
  8. </picker>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'MySelect',
  14. props: {
  15. dictCode: String,
  16. value: String,
  17. label:String,
  18. rangeKey:{type:String,default:'label'},
  19. valueKey:{type:String,default:'value'},
  20. searchUrl:String,
  21. },
  22. watch: {
  23. dictCode: {
  24. immediate: true,
  25. handler() {
  26. this.initDictData()
  27. },
  28. },
  29. },
  30. computed: {
  31. },
  32. data() {
  33. return {
  34. dictOptions: [],
  35. index: -1,
  36. }
  37. },
  38. methods: {
  39. initDictData() {
  40. //根据字典Code, 初始化字典数组
  41. if (this.searchUrl){
  42. this.$api.get(this.searchUrl,{"code":this.dictCode}).then(res=>{
  43. this.dictOptions=res;
  44. this.getIndex()
  45. })
  46. }else{
  47. this.$api.getDict(this.dictCode).then(res => {
  48. this.dictOptions = res;
  49. this.getIndex()
  50. })
  51. }
  52. },
  53. PickerChange(e) {
  54. this.index=e.detail.value
  55. if(this.index==-1){
  56. this.index=0
  57. this.$emit('input',this.dictOptions[0][this.valueKey])
  58. }else{
  59. this.$emit('input', this.dictOptions[this.index][this.valueKey]);
  60. }
  61. },
  62. getIndex() {
  63. for (var i = 0; i < this.dictOptions.length; i++) {
  64. if (this.dictOptions[i].value == this.value) {
  65. this.index = i
  66. return
  67. }
  68. }
  69. this.index=-1
  70. },
  71. }
  72. }
  73. </script>
  74. <style>
  75. </style>