一、功能需求
1、用戶在【首頁】輸入搜索關鍵詞,點擊【搜索】按鈕:
- 攜帶搜索關鍵詞跳轉到【搜索菜品】頁面
- 在【搜索菜品】頁面顯示關鍵詞
- 在【搜索菜品】頁面顯示搜索出來的菜品列表
2、用戶在【搜索菜品】頁面
- 接收【首頁】傳遞過來的關鍵詞並顯示到頁面
- 根據傳遞過來的關鍵詞正則查詢出符合條件的菜品列表
- 用戶在【菜品搜索】頁面輸入關鍵詞,點擊【搜索】正則查詢出符合條件的數據
- 用戶在【菜品搜索】頁面手動點擊+和-,分別增加和減少菜品的數量
二、代碼實現
1、food.wxml
1 <!-- 搜索 --> 2 <view class="searchRoot"> 3 <input type="text" value="{{searchKey}}" class="searchInput" placeholder="搜索菜品" bindinput="getSearchContent" confirm-type="search" bindconfirm="searchFood"></input> 4 <image src="/images/search.png" class="searchIcon" bindtap="searchFood"></image> 5 </view> 6 7 <!-- 菜品列表 --> 8 <view class="hotFoodRoot"> 9 <view class="hotFoodItem" wx:for="{{foodList}}"> 10 <image src="{{item.icon}}"></image> 11 <view class="hotFoodText"> 12 <view class="itemName">{{item.name}}</view> 13 <view class="itemSell">銷量:{{item.sell}}</view> 14 <view class="addAndMinusRoot"> 15 <view class="itemPrice">{{item.price}}</view> 16 <!-- 菜品數量的加減 --> 17 <view class="addAndMinus"> 18 <image class="img" src="/images/minus.png" bindtap="minus" data-id="{{item._id}}"></image> 19 <text class="number">{{item.number}}</text> 20 <image class="img" src="/images/add.png" bindtap="add" data-id="{{item._id}}"></image> 21 </view> 22 </view> 23 </view> 24 </view> 25 </view>
2、food.wxss
1 /*搜索*/ 2 .searchRoot{ 3 display: flex; 4 flex-direction: row; 5 /*彈性盒內各項元素沿着主軸居中顯示*/ 6 align-items: center; 7 padding: 20rpx; 8 } 9 .searchInput{ 10 border: 1rpx solid #FF9966; 11 border-radius: 20rpx; 12 padding: 0 30rpx; 13 flex: 1; 14 height: 76rpx; 15 } 16 .searchIcon{ 17 width: 60rpx; 18 height: 60rpx; 19 margin-left: 20rpx; 20 } 21 /*菜品數據*/ 22 .hotFoodRoot{ 23 /*設置下外邊距,防止固定的購物車擋住最后一個菜品*/ 24 margin-bottom: 140rpx; 25 } 26 .hotFoodItem{ 27 display: flex; 28 margin: 20rpx 20rpx 0 20rpx; 29 border-bottom: 1rpx solid rgb(245, 245, 245); 30 } 31 .hotFoodItem image{ 32 width: 120rpx; 33 height: 120rpx; 34 margin-right: 20rpx; 35 border-radius: 10rpx; 36 /*防止標題過長把圖片擠走*/ 37 min-width: 120rpx; 38 } 39 40 .hotFoodItem .itemName{ 41 font-size: 32rpx; 42 /*設置菜品名稱超過一行時顯示省略號*/ 43 width: 500rpx; 44 white-space: nowrap; 45 text-overflow: ellipsis; 46 overflow: hidden; 47 } 48 .hotFoodItem .itemSell{ 49 font-size: 28rpx; 50 color: gray; 51 } 52 53 /*數量加減*/ 54 .addAndMinusRoot{ 55 display: flex; 56 justify-content: space-between; 57 align-items: center; 58 } 59 .addAndMinusRoot .itemPrice{ 60 font-size: 30rpx; 61 color: #FF9966; 62 } 63 .addAndMinusRoot .itemPrice::before{ 64 /*人民幣符號*/ 65 content: "¥"; 66 color:#FF9966; 67 } 68 .addAndMinusRoot .addAndMinus{ 69 display: flex; 70 justify-content: flex-end; 71 align-items: center; 72 } 73 .addAndMinus .img{ 74 margin: 0; 75 width: 50rpx; 76 /*必須加上min-width*/ 77 min-width: 50rpx; 78 height: 50rpx; 79 } 80 .addAndMinus .number{ 81 margin: 0 20rpx; 82 }
3、food.js
1 //定義db 2 const db = wx.cloud.database() 3 //搜索的菜品信息 4 let foodList = '' 5 //用戶輸入的搜索詞 6 let searchKey = '' 7 Page({ 8 //頁面的初始數據 9 data: { 10 //搜索內容 11 searchKey:'', 12 //搜索到的菜品數據 13 foodList:[] 14 }, 15 16 //生命周期函數--監聽頁面加載 17 onLoad: function (options) { 18 //從首頁攜帶搜索詞 19 console.log("從首頁攜帶過來的搜索內容",options.searchKey); 20 searchKey = options.searchKey 21 //調用自定義方法,執行搜索功能 22 this.searchFood(); 23 //如果用戶輸入關鍵詞搜索 24 if(searchKey&&searchKey.length>0){ 25 this.setData({ 26 searchKey:searchKey 27 }) 28 }else{ //如果用戶不輸入關鍵詞搜索,則搜索所有菜品 29 searchKey='' 30 } 31 }, 32 //獲取用戶輸入的搜索內容 33 getSearchContent(e){ 34 console.log("用戶輸入的搜索內容",e.detail.value); 35 searchKey = e.detail.value 36 }, 37 //點擊搜索按鈕實現功能 38 searchFood(){ 39 console.log("用戶點擊了搜索按鈕,輸入的關鍵詞為",searchKey); 40 //使用正則查詢 41 db.collection("food").where({ 42 name:db.RegExp({ 43 regexp:searchKey, //搜索池 44 options:'i' //不區分大小寫 45 }) 46 }).get() 47 .then(res=>{ 48 console.log("搜索成功",res); 49 foodList = res.data 50 if(foodList&&foodList.length>0){ 51 foodList.forEach(item=>{ 52 //遍歷數組foodList,為數組添加新字段number,賦值為0 53 item.number = 0 54 }) 55 this.setData({ 56 foodList:foodList 57 }) 58 } 59 }).catch(err=>{ 60 console.log("搜索失敗",err); 61 }) 62 }, 63 64 //點擊減少菜品數量 65 minus(e){ 66 console.log("點擊了加",e.currentTarget.dataset.id); 67 let id = e.currentTarget.dataset.id 68 //遍歷當前菜品數組 69 foodList.forEach(item=>{ 70 if(item._id==id){ 71 if(item.number>0){ 72 item.number-=1 73 }else{ 74 wx.showToast({ 75 title: '數量不能小於0', 76 icon:'none' 77 }) 78 } 79 } 80 }) 81 console.log("遍歷以后的當前菜品列表",foodList); 82 this.setData({ 83 foodList:foodList 84 }) 85 }, 86 87 //點擊增加菜品數量 88 add(e){ 89 console.log("點擊了加",e.currentTarget.dataset.id); 90 let id = e.currentTarget.dataset.id 91 //遍歷當前菜品數組 92 foodList.forEach(item=>{ 93 if(item._id==id){ 94 item.number+=1 95 } 96 }) 97 console.log("遍歷以后的當前菜品列表",foodList); 98 this.setData({ 99 foodList:foodList 100 }) 101 }, 102 })
三、效果展示