当前位置: 首页 > article >正文

第62讲商品搜索动态实现以及性能优化

商品搜索后端动态获取数据

后端动态获取数据:

    /**
     * 商品搜索
     * @param q
     * @return
     */
    @GetMapping("/search")
    public R search(String q){
        List<Product> productList = productService.list(new QueryWrapper<Product>().like("name", q));
        Map<String,Object> map=new HashMap<>();
        map.put("message",productList);
        return R.ok(map);
    }

定义productList

    /**
     * 页面的初始数据
     */
    data: {
      productList:[], // 商品数组
      inputValue:"", // 输入框的值
      isFocus:false // 取消 按钮 是否显示
    },

前端调用

  /**
   * 请求后端 商品搜索
   */
  async search(q){
    const result = await requestUtil({
      url: '/product/search',
      data:{q}
    });
    this.setData({
      productList: result.productList
    })
  },

    // 处理input事件
    handleInput(e){
      const {value}=e.detail;
      if(!value.trim()){
        this.setData({
          productList:[],
          isFocus:false
        })
        return;
      }
      this.setData({
        isFocus:true
      })
      clearTimeout(this.TimeoutId);
      this.TimeoutId=setTimeout(()=>{
        this.search(value)
      },1000)
     
    },

页面渲染:

<view class="search_row">
  <input type="text" model:value="{{inputValue}}" placeholder="请输入商品关键字" bindinput="handleInput"/>
  <button hidden="{{!isFocus}}">取消</button>
</view>
<view class="search_content">
  <navigator
    class="search_item"
    wx:for="{{productList}}"
    wx:key="id"
    url="/pages/product_detail/index?id={{item.id}}"
  >
  {{item.name}}
  </navigator>
</view>

样式:

page{
  background-color: #F9F9F9;
  padding-top: 20rpx;
}

.search_row{
  height: 50rpx;
  display: flex;
  input{
    background-color: #FFFFFF;
    flex:1;
    height: 100%;
    padding-left: 30rpx;
  }
  button{
    display:flex;
    justify-content: center;
    align-items: center;
    width:110rpx !important;
    font-size: 26rpx;
    padding: 0;
    margin: 0 10rpx;
    height: 100%;
  }
}

.search_content{
  margin-top: 15rpx;
  padding: 0px;
  .search_item{
    background-color: #FFFFFF;
    font-size: 26rpx;
    padding: 15rpx;
    align-items: center;
    border-bottom: 1rpx solid #EAEAEA;

    overflow:hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}

取消按钮:

<view class="search_row">
  <input type="text" model:value="{{inputValue}}" placeholder="请输入商品关键字" bindinput="handleInput"/>
  <button hidden="{{!isFocus}}">取消</button>
</view>
  // 点击 取消按钮
  handleCancel(){
    this.setData({
      productList:[], // 商品数组
      isFocus:false, // 取消 按钮 是否显示
      inputValue:"" // 输入框的值
    })
  },

在这里插入图片描述
搜索性能优化

搞个定时器

// 处理input事件
    handleInput(e){
      const {value}=e.detail;
      if(!value.trim()){
        this.setData({
          productList:[],
          isFocus:false
        })
        return;
      }
      this.setData({
        isFocus:true
      })
      clearTimeout(this.TimeoutId);
      this.TimeoutId=setTimeout(()=>{
        this.search(value)
      },1000)
     
    },

http://www.kler.cn/news/233605.html

相关文章:

  • python实现基数排序
  • 苹果macbook电脑删除数据恢复该怎么做?Mac电脑误删文件的恢复方法
  • 物资捐赠管理系统
  • 8种基本类型的包装类(与String的转换)
  • 降准是什么意思?降准对股市有哪些影响?
  • 第70讲axios后端请求工具类封装
  • MySQL单主模式部署组复制集群
  • 机器学习聚类算法
  • Collectors.toMap
  • MySQL基础查询篇(9)-数学函数在查询中的应用
  • Linux中的numactl命令指南
  • 代码随想录算法训练营day15||二叉树part02、102.二叉树的层序遍历、 226.翻转二叉树(优先掌握递归)、101. 对称二叉树 (优先掌握递归)
  • MATLAB实现随机森林回归算法
  • 会声会影绿幕抠图操作方法 会声会影绿幕抠图有绿色残边 绿幕抠图视频有绿边怎么处理 抖音怎么剪辑视频 视频剪辑软件推荐
  • Linux 设置自动挂载磁盘
  • LabVIEW伺服阀性能参数测试
  • VitePress-13- 配置-title的作用详解
  • 微服务介绍、使用 Nacos 实现远程调用以及 OpenFeign 的使用
  • containerd中文翻译系列(二十)快照器
  • OnlyOffice-8.0版本深度测评
  • 鸿蒙开发系列教程(十四)--组件导航:Tabs 导航
  • codeforces 126B password
  • CSS中的继承是什么?哪些属性可以继承,哪些不能继承?
  • Blazor入门100天 : 自做一个手势滑动组件
  • 计算机视觉 | OpenCV 实现手势虚拟控制亮度和音量
  • 【Python 千题 —— 基础篇】查找年龄
  • C++ static 修饰全局变量时的作用探究
  • ARM PAC/BTI/MTE三剑客精讲与实战
  • 【2024-01-20】 瑞幸咖啡小程序-blackbox
  • 视觉开发板—K210自学笔记(二)