u-picker.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <template>
  2. <picker @change="change" range-key="pickerName" :value="selectedItem.valueIndex" :range="sourceOptions">
  3. <slot :data="selectedItem"/>
  4. </picker>
  5. </template>
  6. <script>
  7. export default {
  8. model: {
  9. prop: 'value',
  10. event: 'changeValue'
  11. },
  12. props: {
  13. value: {
  14. type: [Number, String],
  15. default: ''
  16. },
  17. options: {
  18. type: Array,
  19. default: () => {
  20. return []
  21. }
  22. },
  23. valueKey: {
  24. type: String,
  25. default: 'value'
  26. },
  27. labelKey: {
  28. type: String,
  29. default: 'label'
  30. },
  31. },
  32. computed: {
  33. sourceOptions(){
  34. const list = this.options.map(item=>{
  35. const pickerName = item.disabled===true? item[this.labelKey] + '(已选)' : item[this.labelKey]
  36. return {...item, pickerName}
  37. })
  38. // const list = this.options.filter(item=>item.disabled!==true)
  39. return list
  40. },
  41. selectedItem() {
  42. const valueIndex = this.options.findIndex(element => element[this.valueKey] == this.value)
  43. const item = valueIndex>=0 ? this.options[valueIndex] : {}
  44. item.valueIndex = valueIndex
  45. return item
  46. }
  47. },
  48. methods: {
  49. change(e) {
  50. const index = e.detail.value
  51. const item = this.options[index]
  52. this.$emit('changeValue', item[this.valueKey])
  53. const that = this
  54. setTimeout(function() {
  55. that.$emit('change', that.selectedItem[that.valueKey], that.selectedItem)
  56. }, 10);
  57. }
  58. }
  59. }
  60. </script>
  61. <style>
  62. </style>