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

一、效果图

二、代码

wxml

html 复制代码
<van-search
  value="{{ keyword }}"
  shape="round"
  background="#000"
  placeholder="请输入关键词"
  use-action-slot
  bind: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-empty
  wx:if="{{noData}}"
  image="https://img01.yzcdn.cn/vant/empty-image-search.png"
  description="没有搜索结果"
/>

scss

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

javascript 复制代码
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 = false
			if(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

javascript 复制代码
{
  "usingComponents": {
    "van-search": "@vant/weapp/search/index",
	 "van-empty": "@vant/weapp/empty/index"
  }
}
相关推荐
2601_9567436835 分钟前
上海小程序开发公司技术路径全解:从架构选型到工程落地的核心判断
小程序·架构·开发经验·上海
VX_bysjlw98544 分钟前
基于微信小程序的宠物用品商城系统-后端74346-计算机毕设原创(免费领源码+带部署教程)
java·redis·微信小程序·eclipse·mybatis·idea·微信开发者工具
mykj15512 小时前
灵感石这个手链DIY小程序,简直是手作人的福音
小程序·灵感石小程序开发·手串diy小程序
一只昀5 小时前
基于微信小程序的咖啡店点餐小程序
微信小程序·小程序
2501_9159184118 小时前
深入对比iOS开发中常用性能监控工具的底层原理与优缺点分析
android·ios·小程序·https·uni-app·iphone·webview
黄华SJ520it1 天前
连锁多门店超市小程序:单品牌多家门店,总部统一管理、门店独立接单、线上到店/配送商超系统设计与实现
小程序·系统开发
2301_810313181 天前
社交APP定制开发:海外交友app开发婚礼聊天直播交友系统源码搭建
小程序·团队开发·软件需求·交友
mykj15511 天前
消费积分抵扣物业费小程序商城开发
小程序·积分商城抵扣物业费·积分商城小程序开发
云迈科技-软件定制开发2 天前
2026年湖南长沙软件定制开发公司怎么选?本地企业做APP、小程序、管理系统的合作参考
小程序
admin and root2 天前
「移动安全」安卓APP 反编译&frida脱壳技巧分享
android·开发语言·python·web安全·微信小程序·移动安全·攻防演练