jp-picker2.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <view>
  3. <picker @change="PickerChange" :value="index" :disabled="disabled" :range-value="rangeValue" :range-key="rangeKey" :range="range">
  4. <view class=" picker action">
  5. <view class=" ">
  6. {{label}}▾
  7. </view>
  8. </view>
  9. </picker>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. index: -1,
  17. label: '请选择'
  18. };
  19. },
  20. props: {
  21. value: String,
  22. empty: {
  23. type: String,
  24. default: ''
  25. },
  26. rangeKey: {
  27. type: String,
  28. default: 'label'
  29. },
  30. rangeValue: {
  31. type: String,
  32. default: 'value'
  33. },
  34. range: {
  35. type: Array,
  36. default: []
  37. },
  38. disabled: {
  39. type: Boolean,
  40. default: false
  41. },
  42. onChangeNew: {
  43. type: Function,
  44. default: null,
  45. }
  46. },
  47. mounted() {
  48. if (this.empty) {
  49. this.label = this.empty
  50. }
  51. },
  52. watch:{
  53. value: {
  54. handler (val) {
  55. if(val) {
  56. let options = this.range.filter((option)=>{
  57. return option.value === val
  58. })
  59. // if(options.length === 0){
  60. // this.label = '请选择'
  61. // } else {
  62. this.label = options[0][this.rangeKey]
  63. //}
  64. }
  65. },
  66. immediate: true,
  67. deep: false
  68. }
  69. },
  70. methods:{
  71. PickerChange(e) {
  72. this.index = e.detail.value;
  73. //if(this.index !== -1){
  74. this.label = this.range[this.index][this.rangeKey]
  75. this.$emit('input', this.range[this.index][this.rangeValue])
  76. this.$emit('change', this.range[this.index][this.rangeValue])
  77. // }else{
  78. // this.label = '请选择'
  79. // this.$emit('input', null)
  80. // }
  81. if (this.onChangeNew) {
  82. this.onChangeNew()
  83. }
  84. }
  85. }
  86. }
  87. </script>
  88. <style>
  89. </style>