card.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // myComponents/card/card.js
  2. Component({
  3. options: {
  4. multipleSlots: true // 在组件定义时的选项中启用多slot支持
  5. },
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. edit:{
  11. type:Boolean,
  12. value:true
  13. },
  14. menu:{
  15. type:Array,
  16. value:[]
  17. },
  18. data:null,
  19. index:null
  20. },
  21. /**
  22. * 组件的初始数据
  23. */
  24. data: {
  25. popover:null
  26. },
  27. /**
  28. * 组件的方法列表
  29. */
  30. methods: {
  31. onTap: function (e) {
  32. if(this.properties.menu.length == 0){
  33. return false
  34. }
  35. var dom = this.selectComponent('#myPopover')
  36. dom.showMenu()
  37. return
  38. console.log(e)
  39. console.log(this.data.popover)
  40. // 获取按钮元素的坐标信息
  41. var id = e.currentTarget.id // 或者 e.target.id 获取点击元素的 ID 值
  42. const query = wx.createSelectorQuery().in(this);
  43. var that = this
  44. query.select('#'+id).boundingClientRect(function(res) {
  45. // console.log(res.id); // 节点的ID
  46. // console.log(res.dataset); // 节点的dataset
  47. // console.log(res.left); // 节点的左边界坐标
  48. // console.log(res.right); // 节点的右边界坐标
  49. // console.log(res.top); // 节点的上边界坐标
  50. // console.log(res.bottom); // 节点的下边界坐标
  51. // console.log(res.width); // 节点的宽度
  52. // console.log(res.height); // 节点的高度
  53. that.data.popover.onDisplay(res);
  54. }).exec();
  55. },
  56. // 响应popover组件中的子元素点击事件
  57. onClickA: function (e) {
  58. console.log(e)
  59. const item = e.currentTarget.dataset.item
  60. item.data = this.properties.data
  61. item.index = this.properties.index
  62. // wx.showToast({
  63. // title: '你点击了A',
  64. // icon: 'none'
  65. // });
  66. // 调用自定义组件 popover 中的 onHide 方法
  67. // this.data.popover.onHide();
  68. console.log(item)
  69. var dom = this.selectComponent('#myPopover')
  70. dom.showMenu()
  71. this.triggerEvent('value',item)
  72. }
  73. },
  74. /**
  75. * 组件的生命周期函数列表
  76. */
  77. lifetimes: {
  78. // 在组件实例进入页面节点树时执行
  79. attached: function () {
  80. // 初始化操作
  81. this.setData(
  82. {
  83. popover:this.selectComponent('#popover')
  84. }
  85. )
  86. // this.data.popover = this.selectComponent('#popover')
  87. console.log(this.data.popover)
  88. // ...
  89. },
  90. // 在组件实例被移除出页面节点树时执行
  91. detached: function () {
  92. // 清理工作
  93. console.log('组件销毁');
  94. // ...
  95. },
  96. // ...
  97. },
  98. })