微信小程序项目_秋泊优选69

64、商品搜索_页面取消按钮功能

在这里插入图片描述
1 给商品列表添加对应的超链接——>跳转到商品详情页面
pages\search\index.wxml

<view class="search_row">
    <input value="{{inpValue}}" placeholder="请输入您要搜索的商品" bindinput="handleInput"></input>
    <button bindtap="handleCancel" hidden="{{!isFocus}}">取消</button>
</view>

<view class="search_content">
    <navigator url="/pages/goos_detail/index?goods_id={{item.goods_id}}" class="search_item" wx:for="{{goods}}" wx:key="goods_id">
        {{item.goods_name}}
    </navigator>
</view>

pages\search\index.js

// 0 引入 来发送请求的方法 一定要把路径补全
import {request} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods:[],
    // 取消按钮是否显示
    isFocus:false,
    // 输入框的值 
    inpValue:""
  },
  TimeId:-1,

  // 输入框的值改变就会触发的事件
  handleInput(e){
    // 1 获取输入框的值
    const {value}=e.detail;
    // 2 检测合法性
    if(!value.trim()){
    
      this.setData({
        goods:[],
        isFocus:false
      })

      // 值不合法
      return;
    }
    // 3 准备发送请求获取数据
    clearTimeout(this.TimeId);
    this.TimeId=setTimeout(()=>{
      this.qsearch(value);
    }, 1000);

    // 输入完成显示 取消按钮
    this.setData({
      isFocus:true
    })
    
  },

  // 发送请求获取搜索建议数据
  async qsearch(query){
    const res=await request({url: "/goods/qsearch", data:{query}});
    console.log(res);
    this.setData({
      goods:res
    })
  },

  // 点击取消按钮
  handleCancel(){
    this.setData({
      inpValue:"",
      isFocus:false,
      goods:[]
    })
  }


})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值