Ver Fonte

搜索实现

LiShiwei há 2 anos atrás
pai
commit
8a460bd196
2 ficheiros alterados com 62 adições e 31 exclusões
  1. 57 28
      pages/search/index.js
  2. 5 3
      pages/search/index.wxml

+ 57 - 28
pages/search/index.js

@@ -11,8 +11,8 @@ Page({
         foodsName: '麻烫',
         foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
         initPrice: 28.80,
-        nowPrice: 10.98
-
+        nowPrice: 10.98,
+        num : 0
       },
       {
         id: 1,
@@ -20,7 +20,8 @@ Page({
         foodsName: '麻辣',
         foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
         initPrice: 28.80,
-        nowPrice: 10.98
+        nowPrice: 10.98,
+        num : 10
       },
       {
         id: 2,
@@ -28,8 +29,8 @@ Page({
         foodsName: '麻辣烫',
         foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
         initPrice: 28.80,
-        nowPrice: 10.98
-
+        nowPrice: 10.98,
+        num : 0
       },
       {
         id: 3,
@@ -37,8 +38,8 @@ Page({
         foodsName: '麻辣烫',
         foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
         initPrice: 28.80,
-        nowPrice: 10.98
-
+        nowPrice: 10.98,
+        num : 0
       },
       {
         id: 4,
@@ -46,8 +47,8 @@ Page({
         foodsName: '麻辣烫',
         foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
         initPrice: 28.80,
-        nowPrice: 10.98
-
+        nowPrice: 10.98,
+        num : 0
       },
       {
         id: 5,
@@ -55,8 +56,8 @@ Page({
         foodsName: '麻辣烫',
         foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
         initPrice: 28.80,
-        nowPrice: 10.98
-
+        nowPrice: 10.98,
+        num : 0
       },
       {
         id: 6,
@@ -64,8 +65,8 @@ Page({
         foodsName: '麻辣烫',
         foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
         initPrice: 28.80,
-        nowPrice: 10.98
-
+        nowPrice: 10.98,
+        num : 0
       },
       {
         id: 7,
@@ -73,13 +74,14 @@ Page({
         foodsName: '麻辣烫',
         foodsDesc: '包含香菜、菠菜、面、豆芽、海带',
         initPrice: 28.80,
-        nowPrice: 10.98
-
+        nowPrice: 10.98,
+        num : 0
       }
     ],
     show: false,
     totalPrice: 0,
-    info: ''
+    info: '',
+    showList: []
   },
   onInput(e) {
     this.setData({
@@ -88,28 +90,55 @@ Page({
   },
   onChange(e) {
     let id = e.currentTarget.dataset.id;
-    let count = e.detail;
-    let foodsPrice = this.data.foods[id].nowPrice;
-    // console.log(id,foodsPrice);
+    let num = e.detail;
+    // console.log(num); 
+    let ans  = this.data.foods[0].num 
+    console.log(ans);
+    this.setData({
+      [ans ]:'num'
+    })
+    // for( let i = 0 ; i < showList.length ;i ++ ) {
+    //   showList[i].num 
+    // }
+    // for( let i = 0 ; i < showList.length ;i ++ ) {
+    //   showList[i].
+    // }
+    // totalPrice
   },
   onSearch() {
     let list = this.data.foods;
-    let arr=[];
-    list.forEach(res=>{
-     if(this.data.info===res.foodsName)
-     {
-      arr.push(res)
-     }
-    })
-    console.log(arr);
+    // 给你一个数组  里面包含a这个字的元素  将这些元素抛入新数组 
+    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]);
+      }
+    }
+    console.log(showList);
+
+    // showList
+    if (showList.length != 0) {
+      this.setData({
+        show: false,
+      })
+    } else {
+      this.setData({
+        show: true,
+      })
+    }
     this.setData({
-      show: !this.data.show,
+      showList,
     })
   },
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad(options) {
+    this.setData({
+      showList: this.data.foods
+    })
   },
 
   /**

+ 5 - 3
pages/search/index.wxml

@@ -17,7 +17,7 @@
   </view>
 
   <view wx:else>
-    <view class="search_content_false" wx:for="{{foods}}" wx:key="*this">
+    <view class="search_content_false" wx:for="{{showList}}" wx:key="*this" >
       <van-row>
         <van-col span="7" offset="1">
           <view class="pic">
@@ -35,11 +35,13 @@
         </van-col>
 
         <van-col span="8">
-          <van-stepper data-id="{{item.id}}" style="position: relative;top:180rpx;right:-20rpx;" min="0" value="{{ 0 }}" bind:change="onChange" />
+          <van-stepper data-id="{{item.id}}" style="position: relative;top:180rpx;right:-20rpx;" min="0" value="{{ item.num }}" bind:change="onChange" />
         </van-col>
       </van-row>
     </view>
-    <view class="search_total">
+    <!-- 分割 -->
+    <view style="height: 80rpx;width: 100%;"></view>
+    <view class="search_total" >
     <van-submit-bar  price="{{ totalPrice }}" button-text="去结算" bind:submit="onSubmit" />
   </view>
   </view>