index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // pages/merchant_page/index.js
  2. var sum=0;
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. foods: [{
  9. id: 0,
  10. foodsImg: 'https://bpic.588ku.com/back_origin_min_pic/20/11/05/a7438bbf54ace893e790545815e14ef3.jpg',
  11. foodsName: '麻烫',
  12. foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
  13. initPrice: 28.80,
  14. nowPrice: 10.98,
  15. num: 0
  16. },
  17. {
  18. id: 1,
  19. foodsImg: 'https://bpic.588ku.com/back_origin_min_pic/20/11/05/a7438bbf54ace893e790545815e14ef3.jpg',
  20. foodsName: '麻辣',
  21. foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
  22. initPrice: 28.80,
  23. nowPrice: 10.98,
  24. num: 0
  25. },
  26. {
  27. id: 2,
  28. foodsImg: 'https://bpic.588ku.com/back_origin_min_pic/20/11/05/a7438bbf54ace893e790545815e14ef3.jpg',
  29. foodsName: '辣烫',
  30. foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
  31. initPrice: 28.80,
  32. nowPrice: 10.98,
  33. num: 0
  34. },
  35. {
  36. id: 3,
  37. foodsImg: 'https://bpic.588ku.com/back_origin_min_pic/20/11/05/a7438bbf54ace893e790545815e14ef3.jpg',
  38. foodsName: '麻烫',
  39. foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
  40. initPrice: 28.80,
  41. nowPrice: 10.98,
  42. num: 0
  43. },
  44. {
  45. id: 4,
  46. foodsImg: 'https://bpic.588ku.com/back_origin_min_pic/20/11/05/a7438bbf54ace893e790545815e14ef3.jpg',
  47. foodsName: '麻辣',
  48. foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
  49. initPrice: 28.80,
  50. nowPrice: 10.98,
  51. num: 0
  52. },
  53. {
  54. id: 5,
  55. foodsImg: 'https://bpic.588ku.com/back_origin_min_pic/20/11/05/a7438bbf54ace893e790545815e14ef3.jpg',
  56. foodsName: '辣烫',
  57. foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
  58. initPrice: 28.80,
  59. nowPrice: 10.98,
  60. num: 0
  61. },
  62. {
  63. id: 6,
  64. foodsImg: 'https://bpic.588ku.com/back_origin_min_pic/20/11/05/a7438bbf54ace893e790545815e14ef3.jpg',
  65. foodsName: '辣烫',
  66. foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
  67. initPrice: 28.80,
  68. nowPrice: 10.98,
  69. num: 0
  70. },
  71. {
  72. id: 7,
  73. foodsImg: 'https://bpic.588ku.com/back_origin_min_pic/20/11/05/a7438bbf54ace893e790545815e14ef3.jpg',
  74. foodsName: '麻辣烫',
  75. foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
  76. initPrice: 28.80,
  77. nowPrice: 10.98,
  78. num: 0
  79. }
  80. ],
  81. show: false,
  82. totalPrice: 0,
  83. info: '',
  84. showList: []
  85. },
  86. onInput(e) {
  87. this.setData({
  88. info: e.detail.value
  89. })
  90. },
  91. onMinus(e){
  92. var id = e.currentTarget.dataset.id;
  93. sum-=this.data.foods[id].nowPrice;
  94. // console.log(sum.toFixed(2)+"---------");
  95. this.setData({
  96. totalPrice:Math.abs(sum)
  97. })
  98. },
  99. onPlus(e){
  100. var id = e.currentTarget.dataset.id;
  101. sum+=this.data.foods[id].nowPrice;
  102. // console.log(sum.toFixed(2)+"+++++++++");
  103. // console.log(typeof sum );
  104. this.setData({
  105. totalPrice:Math.abs(sum)
  106. })
  107. },
  108. onChange(e) {
  109. let id = e.currentTarget.dataset.id; //获取当前点击的Id
  110. let num = e.detail; //获取当前步进器的值
  111. //给当前的对象里面的属性绑定其步进器的值
  112. this.setData({
  113. ["foods["+id+"].num"]:num
  114. })
  115. //接下来我们利用这个步进器的值,forEach遍历求和即可
  116. // console.log(sum);
  117. },
  118. onSearch() {
  119. let list = this.data.foods;
  120. // 给你一个数组 里面包含a这个字的元素 将这些元素抛入新数组
  121. let inputData = this.data.info
  122. let showList = [];
  123. for (let i = 0; i < list.length; i++) {
  124. let name = list[i].foodsName
  125. if (name.indexOf(inputData) >= 0) {
  126. showList.push(list[i]);
  127. }
  128. }
  129. // console.log(showList);
  130. // showList
  131. if (showList.length == 0) {
  132. this.setData({
  133. show: true,
  134. })
  135. } else {
  136. this.setData({
  137. show: false,
  138. })
  139. }
  140. this.setData({
  141. showList,
  142. })
  143. },
  144. /**
  145. * 生命周期函数--监听页面加载
  146. */
  147. onLoad() {
  148. this.setData({
  149. showList: this.data.foods
  150. })
  151. },
  152. /**
  153. * 生命周期函数--监听页面初次渲染完成
  154. */
  155. onReady() {
  156. },
  157. /**
  158. * 生命周期函数--监听页面显示
  159. */
  160. onShow() {
  161. },
  162. /**
  163. * 生命周期函数--监听页面隐藏
  164. */
  165. onHide() {
  166. },
  167. /**
  168. * 生命周期函数--监听页面卸载
  169. */
  170. onUnload() {
  171. },
  172. /**
  173. * 页面相关事件处理函数--监听用户下拉动作
  174. */
  175. onPullDownRefresh() {
  176. },
  177. /**
  178. * 页面上拉触底事件的处理函数
  179. */
  180. onReachBottom() {
  181. },
  182. /**
  183. * 用户点击右上角分享
  184. */
  185. onShareAppMessage() {
  186. }
  187. })