uview 搜索+防抖

debounce(func, wait = 500, immediate = false) 

在连续的操作中,无论进行了多长时间,只有某一次的操作后在指定的时间内没有再操作,这一次才被判定有效

需要搜索功能的页面,click事件一般用于跳转到搜索页

<template>
    <view>
		<view>
            <u-search margin="10rpx" v-model="keyword" disabled @click="go_search"></u-search>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
			return {
				keyword: '搜点什么...'//这里做好是后端给的数据
			}
		},
        methods: {
            go_search(){
				let that = this
				uni.navigateTo({
					url: '/pages/search/search?keyword=' + that.keyword
				})
			},
        }
    }
</script>

搜索页面,html部分

<template>
    <view>
          <view style="margin-top: 10rpx;">
			<u-search margin="10rpx" placeholder="搜点什么..." v-model="keyword" clearabled @change="change"
				@search="search"></u-search>
		</view>
		<view class="tag-item">
			<view style="margin: 10rpx 6rpx;" v-for="(item,index) in listkeyword" :key="index">
				<u-tag :text="item.goods_name" plain shape="circle" size="mini" @click="go_goods(item.id)"></u-tag>
			</view>
		</view>
        <u-divider text="产品" textPosition="left"></u-divider>
		<view>
			<view class="goods_list">
				<view class="goods_item" v-for="(item,index) in listkeyword" :key='item.id'
					@click="go_goods(item.id)">
					<image :src="item.img[0].url" mode="aspectFit"
						style="border-radius: 10rpx;background-color: #fef0f0;"></image>
					<view class="name">
						<u--text size="16" :lines="1" :text="item.goods_name"></u--text>
					</view>
					<view class="prie">
						<text class="text1">¥{{item.goods_present_price}}</text>
						<text class="text2"
							v-if="item.goods_market_price > 0">{{item.goods_market_price}}</text>
						<text class="text3" v-if="item.goods_dingjin_price > 0">
							<text style="font-size: 30rpx;">定金</text>{{item.goods_dingjin_price}}
						</text>
					</view>
				</view>
			</view>
			<u-divider text="宝宝底线啦"></u-divider>
			<view style="height: 20rpx;"></view>
		</view>
    </view>
</template>

搜索页面,css部分

<style lang="scss">
	.tag-item{
		display: flex;
		flex-wrap: wrap;
	}
	
	.goods_list {
		padding: 0 10rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 15rpx;
	
		.goods_item {
			margin: 5rpx 0;
			width: 355rpx;
			border-radius: 15rpx;
			box-shadow: 0px 0px 8px #cecece;
			background-color: #ffffff;
	
			image {
				width: 300rpx;
				height: 300rpx;
				display: block;
				margin: auto;
			}
	
			.prie {
				font-size: 32rpx;
				margin-bottom: 15rpx;
				margin-left: 15rpx;
	
				.text1 {
					font-weight: bold;
					color: #fb6583;
					font-size: 35rpx;
					width: 166px;
				}
	
				.text2 {
					color: #606266;
					font-size: 20rpx;
					margin-left: 20rpx;
					width: 166px;
					text-decoration: line-through;
				}
	
				.text3 {
					font-weight: bold;
					color: #fb6583;
					font-size: 35rpx;
					margin-left: 20rpx;
					width: 196px;
				}
			}
	
			.name {
				font-size: 30rpx;
				line-height: 50rpx;
				padding-bottom: 6rpx;
				padding-top: 10rpx;
				margin-left: 18rpx;
			}
		}
	}
</style>

搜索页面,js部分,uni.$u.debounce(执行的函数,时间)

<script>
    export default {
        data() {
			return {
				keyword: '',
				listkeyword: []//数据源
			}
		},
        methods: {
            change() {//当数据发生变化
                let that = this
				uni.$u.debounce(that.toNext, 600)
			},
			search() {//回车或者输入法的搜索
				let that = this
				uni.$u.debounce(that.toNext, 600)
			},
			toNext() {
				//发起请求
			}
        }
    }
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值