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

微信小程序(原生)搜索功能实现

 一、效果图

 二、代码

wxml

<van-searchvalue="{{ keyword }}"shape="round"background="#000"placeholder="请输入关键词"use-action-slotbind:change="onChange"bind:search="onSearch"bind:clear="onClear"
><view slot="action" bind:tap="onSearch" style="padding: 0 26rpx; color: #fff;">搜索</view>
</van-search><view class="history" wx:if="{{!productList.length &&  historyList.length}}"><view class="title"><view class="text">搜索历史</view><view class="remove" bind:tap="handleHistoryRemove"><van-icon name="delete-o" size="18" /><text class="font">清空历史</text></view></view><view class="content"><view class="item" wx:for="{{historyList}}" wx:key="item" data-value="{{item}}"  bind:tap="hisItemClick">{{item}}</view></view>
</view><view class="total" wx:if="{{total}}">一共搜索出<text class="totalNum">{{total}}</text>产品
</view><view class="product_main"><view class="main_item" wx:for="{{productList}}" wx:key="_id"><!-- 商品的组件 -->11</view>
</view><van-emptywx:if="{{noData}}"image="https://img01.yzcdn.cn/vant/empty-image-search.png"description="没有搜索结果"
/>

scss

.history{padding: 30rpx;.title{display: flex;justify-content: space-between;align-items: center;font-size: 30rpx;color: #666;.text{font-size: 32rpx;}.remove{color: #999;display: flex;align-items: center;.font{padding-left: 5rpx;}}}.content{padding: 20rpx 0;display: flex;flex-wrap: wrap;.item{color: #333;background-color: aqua;border-radius: 20rpx;font-size: 25rpx;padding: 8rpx 25rpx;margin: 0 20rpx 20rpx 0;}}
}
.total{padding: 30rpx;font-size: 28rpx;color: #666;.totalNum{color: red;}
}
.product_main{padding: 0 30rpx;display: flex;flex-wrap: wrap;justify-content: space-between;.main_item{margin-top: 20rpx;width: 320rpx;height: 200rpx;background-color: pink;}
}

js

import {queryProduct
} from '../../api/apis'
Page({data: {historyList: [], //搜索的历史记录productList: [], //搜到的产品total: 0,keyword: '',noData:false},onLoad(options) {let hisKey = wx.getStorageSync("hisKey") || null;if (hisKey) {this.setData({historyList: hisKey})}},hisItemClick(item) {this.setData({keyword: item.currentTarget.dataset.value})this.getSearchData()},onClear() {this.setData({keyword: '',productList: [],total: 0,noData:false})},onChange(e) {this.setData({keyword: e.detail})},// 确认搜索onSearch() {if (this.data.keyword.trim().length == '') {wx.showToast({title: '请输入搜索内容',icon: 'none',})return}console.log('123');let hisArr = this.data.historyList || []hisArr.unshift(this.data.keyword)hisArr = [...new Set(hisArr)]hisArr = hisArr.slice(0, 5)this.setData({historyList: hisArr})wx.setStorageSync("hisKey", hisArr)this.getSearchData()},// 获取搜索到的数据getSearchData() {queryProduct({keyword: this.data.keyword,limit: 10}).then(res => {console.log(res);let noData = falseif(res.data.length == 0) {noData=true}this.setData({total: res.total,productList: res.data,noData})})},handleHistoryRemove() {wx.showModal({title: '提示',content: '是否确定清空历史?',success: (res)=> {if (res.confirm) {this.setData({historyList: [],productList: [], total: 0,keyword: '',noData:false})wx.removeStorageSync('hisKey')} else if (res.cancel) {console.log('用户点击取消')}}})},
})

json文件是引入的vant

{"usingComponents": {"van-search": "@vant/weapp/search/index","van-empty": "@vant/weapp/empty/index"}
}

http://www.lryc.cn/news/124102.html

相关文章:

  • Android AOSP源码编译——AOSP整编(二)
  • 铁是地球科学争论的核心
  • TX Text Control .NET Server for ASP.NET Crack
  • 工作纪实36-ES跨集群迁移
  • 【MFC】11.MFC文档和单文档架构-笔记
  • 2023年“研究生科研素养提升”系列公益讲座在线测评题目与参考答案
  • MySQL8.xx一主两从复制安装与配置
  • 深度学习优化器
  • 由浅入深C系列五:使用libcurl进行基于http get/post模式的C语言交互应用开发
  • 高效实用小工具之Everything
  • 【Unity每日一记】关于物体(敌方)检测—(向量点乘相关)
  • Elasticsearch-查询
  • 首发 | FOSS分布式全闪对象存储系统白皮书
  • Java反射获取所有Controller和RestController类的方法
  • 设计模式--策略模式
  • VSCode使用SSH无密码连接Ubuntu
  • 通过版本号控制强制刷新浏览器或清空浏览器缓存
  • Redis系列(二):深入解读Redis的两种持久化方式
  • CNN之图像识别
  • nvcc not found
  • pdf怎么转换成jpg图片?这几个转换方法了解一下
  • 六轴机械臂码垛货物堆叠仿真
  • text-decoration 使用
  • linux shell快速入门
  • 【Spring源码】小白速通解析Spring源码,从0到1,持续更新!
  • Unity 鼠标实现对物体的移动、缩放、旋转
  • 67Class 的基本语法
  • 企业数字化转型:无形资产占比测算(2007-2021年)
  • [centos]设置主机名
  • 华为OD真题--新学习选址--带答案