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

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

后端动态获取数据:

bash 复制代码
    /**
     * 商品搜索
     * @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

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

前端调用

bash 复制代码
  /**
   * 请求后端 商品搜索
   */
  async search(q){
    const result = await requestUtil({
      url: '/product/search',
      data:{q}
    });
    this.setData({
      productList: result.productList
    })
  },
bash 复制代码
    // 处理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)
     
    },

页面渲染:

bash 复制代码
<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>

样式:

bash 复制代码
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;
  }
}

取消按钮:

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

搜索性能优化

搞个定时器

bash 复制代码
// 处理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)
     
    },
相关推荐
烂蜻蜓1 小时前
前端已死?什么是前端
开发语言·前端·javascript·vue.js·uni-app
Rowrey2 小时前
react+typescript,初始化与项目配置
javascript·react.js·typescript
谢尔登2 小时前
Vue 和 React 的异同点
前端·vue.js·react.js
祈澈菇凉6 小时前
Webpack的基本功能有哪些
前端·javascript·vue.js
小纯洁w6 小时前
Webpack 的 require.context 和 Vite 的 import.meta.glob 的详细介绍和使用
前端·webpack·node.js
想睡好7 小时前
css文本属性
前端·css
qianmoQ7 小时前
第三章:组件开发实战 - 第五节 - Tailwind CSS 响应式导航栏实现
前端·css
记得早睡~7 小时前
leetcode150-逆波兰表达式求值
javascript·算法·leetcode
zhoupenghui1687 小时前
golang时间相关函数总结
服务器·前端·golang·time
White graces7 小时前
正则表达式效验邮箱格式, 手机号格式, 密码长度
前端·spring boot·spring·正则表达式·java-ee·maven·intellij-idea