index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // commpents/picker/index.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. label: { type: String },
  8. model: { type: String },
  9. columns: { type: Array },
  10. value: {
  11. type: String,
  12. observer: function (val) {
  13. this.initData()
  14. }
  15. },
  16. valueKey: { type: String, value: 'value' },
  17. labelKey: { type: String, value: 'label' }
  18. },
  19. /**
  20. * 组件的初始数据
  21. */
  22. data: {
  23. text: '请选择',
  24. show: false,
  25. },
  26. /**
  27. * 组件的方法列表
  28. */
  29. methods: {
  30. initData() {
  31. const val = this.properties.value
  32. if (!val) return;
  33. const res = this.properties.columns.find(f => f[this.properties.valueKey] === val)
  34. if (!res) return;
  35. const label = res[this.properties.labelKey]
  36. this.setData({ text: label })
  37. },
  38. onChange(event) {
  39. const obj = event?.detail?.value;
  40. const label = obj[this.properties.labelKey]
  41. const value = obj[this.properties.valueKey]
  42. this.setData({ text: label })
  43. this.triggerEvent('selected', { value, model: this.properties.model })
  44. this.toClose();
  45. },
  46. toOpen() {
  47. this.setData({ show: true })
  48. },
  49. toClose() {
  50. this.setData({ show: false })
  51. }
  52. }
  53. })