jp-picker.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <view style="width: 100%;">
  3. <picker style="width: 100%;" @change="PickerChange" :value="index" :disabled="disabled" :range-value="rangeValue" :range-key="rangeKey" :range="range">
  4. <u--input
  5. v-model="label"
  6. suffixIcon="arrow-down"
  7. suffixIconStyle="color: #17a2f8"
  8. disabled
  9. disabledColor="#ffffff"
  10. placeholder="请选择"
  11. border="none"
  12. ></u--input>
  13. </picker>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. index: -1,
  21. label: '请选择'
  22. };
  23. },
  24. props: {
  25. value: String,
  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. },
  43. mounted() {
  44. },
  45. watch:{
  46. value: {
  47. handler (val) {
  48. if(val) {
  49. let options = this.range.filter((option)=>{
  50. return option[this.rangeValue] === val
  51. })
  52. if(options.length === 0){
  53. this.label = '请选择'
  54. } else {
  55. this.label = options[0][this.rangeKey]
  56. }
  57. }
  58. },
  59. immediate: true,
  60. deep: false
  61. }
  62. },
  63. methods:{
  64. PickerChange(e) {
  65. this.index = e.detail.value;
  66. if(this.index !== -1){
  67. this.label = this.range[this.index][this.rangeKey]
  68. this.$emit('input', this.range[this.index][this.rangeValue])
  69. }else{
  70. this.label = '请选择'
  71. this.$emit('input', null)
  72. }
  73. }
  74. }
  75. }
  76. </script>
  77. <style>
  78. </style>