jp-picker.vue 1.5 KB

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