// pages/merchant_page/index.js var sum = 0; Page({ /** * 页面的初始数据 */ data: { foods: [], show: false, totalPrice: 0, info: '', showList: [] }, onInput(e) { this.setData({ info: e.detail.value }) }, onMinus(e) { var id = e.currentTarget.dataset.id; sum -= this.data.foods[id].nowPrice; // console.log(sum.toFixed(2)+"---------"); this.setData({ totalPrice: Math.abs(sum) }) }, onPlus(e) { var id = e.currentTarget.dataset.id; sum += this.data.foods[id].nowPrice; // console.log(sum.toFixed(2)+"+++++++++"); // console.log(typeof sum ); this.setData({ totalPrice: Math.abs(sum) }) }, onChange(e) { let id = e.currentTarget.dataset.id; //获取当前点击的Id let num = e.detail; //获取当前步进器的值 //给当前的对象里面的属性绑定其步进器的值 this.setData({ ["foods[" + id + "].num"]: num }) //接下来我们利用这个步进器的值,forEach遍历求和即可 // console.log(sum); }, onSearch() { let list = this.data.foods; let inputData = this.data.info; let showList = []; for (let i = 0; i < list.length; i++) { let name = list[i].foodsName if (name.indexOf(inputData) >= 0) { showList.push(list[i]); } } // showList if (showList.length == 0) { this.setData({ show: true, }) } else { this.setData({ show: false, }) } this.setData({ showList, }) }, /** * 生命周期函数--监听页面加载 */ onLoad() { wx.request({ url: 'https://www.fastmock.site/mock/1f6ac0783b50c03f315fe6c001ca8dea/search/search', success: (res) => { let { data } = res.data; // console.log(data); this.setData({ foods: data.dishName, showList: data.dishName }) } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })