test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. Page({
  2. data:{
  3. optionList:[
  4. {
  5. name: "标题1",
  6. subname: "副标题1"
  7. }, {
  8. name: "标题2",
  9. subname: "副标题2"
  10. }, {
  11. name: "标题3",
  12. subname: "副标题3"
  13. }, {
  14. name: "标题4",
  15. subname: "副标题4"
  16. },
  17. ],
  18. kelong: {
  19. top: 0,
  20. xt: '',
  21. name: '',
  22. subname: ''
  23. },
  24. replace: {
  25. xt: '',
  26. name: '',
  27. subname: ''
  28. },
  29. },
  30. dragStart: function (e) {
  31. var that = this
  32. var kelong = that.data.kelong
  33. var i = e.currentTarget.dataset.index
  34. kelong.name = this.data.optionList[i].name
  35. kelong.subname = this.data.optionList[i].subname
  36. var query = wx.createSelectorQuery();
  37. //选择id
  38. query.select('.listbox').boundingClientRect(function (rect) {
  39. // console.log(rect.top)
  40. kelong.top = e.changedTouches[0].clientY - rect.top - 30
  41. that.setData({
  42. kelong: kelong,
  43. showkelong: true
  44. })
  45. }).exec();
  46. },
  47. dragMove: function (e) {
  48. var that = this
  49. var i = e.currentTarget.dataset.index
  50. var query = wx.createSelectorQuery();
  51. var kelong = that.data.kelong
  52. var listnum = that.data.optionList.length
  53. var optionList = that.data.optionList
  54. query.select('.listbox').boundingClientRect(function (rect) {
  55. kelong.top = e.changedTouches[0].clientY - rect.top - 30
  56. if (kelong.top < -60) {
  57. kelong.top = -60
  58. } else if (kelong.top > rect.height) {
  59. kelong.top = rect.height - 60
  60. }
  61. that.setData({
  62. kelong: kelong,
  63. })
  64. console.log(kelong.top);
  65. }).exec();
  66. },
  67. dragEnd: function (e) {
  68. var that = this
  69. var i = e.currentTarget.dataset.index
  70. var query = wx.createSelectorQuery();
  71. var kelong = that.data.kelong
  72. var listnum = that.data.optionList.length
  73. var optionList = that.data.optionList
  74. query.select('.listbox').boundingClientRect(function (rect) {
  75. kelong.top = e.changedTouches[0].clientY - rect.top - 30
  76. if (kelong.top < -20) {
  77. wx.showModal({
  78. title: '删除提示',
  79. content: '确定要删除此条记录?',
  80. confirmColor: '#e4463b'
  81. })
  82. }
  83. var target = parseInt(kelong.top / 60)
  84. var replace = that.data.replace
  85. if (target >= 0) {
  86. replace.xt = optionList[target].xt
  87. replace.name = optionList[target].name
  88. replace.subname = optionList[target].subname
  89. optionList[target].name = optionList[i].name
  90. optionList[target].subname = optionList[i].subname
  91. optionList[i].name = replace.name
  92. optionList[i].subname = replace.subname
  93. }
  94. that.setData({
  95. optionList: optionList,
  96. showkelong: false
  97. })
  98. }).exec();
  99. },
  100. })