[vue] vue3.x 前端列表搜索过滤

vue-next-admin,这是基于 vue3.x + CompositionAPI + typescript + vite + element plus + vue-router-next + next.vuex,适配手机、平板、pc 的后台开源免费模板库

一、效果图

在这里插入图片描述

二、组件代码

<template>
	<div class="market-details-container">
		<input type="text" v-model="userSearch" placeholder="请输入姓名" />
		<ul>
			<li v-for="(v, k) in filterUserList" :key="k">姓名:{{ v.name }},年龄:{{ v.age }}</li>
		</ul>
	</div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, onMounted, computed, getCurrentInstance } from 'vue';
export default defineComponent({
	name: 'marketDetails',
	setup() {
		const { proxy } = getCurrentInstance() as any;
		const state = reactive({
			userSearch: '',
			userList: [],
		});
		onMounted(() => {
			for (let i = 0; i < 10; i++) {
				state.userList.push({
					id: Math.random(),
					name: `
					${unescape(`\\u${(Math.round(Math.random() * 20901) + 19968).toString(16)}`.replace(/\\/g, '%'))}${unescape(
						`\\u${(Math.round(Math.random() * 20901) + 19968).toString(16)}`.replace(/\\/g, '%')
					)}${unescape(`\\u${(Math.round(Math.random() * 20901) + 19968).toString(16)}`.replace(/\\/g, '%'))}
					`,
					age: (Math.random() * 20 + 18).toFixed(0),
				});
			}
		});
		const filterUserList = computed(() => {
			let { userSearch, userList } = proxy;
			let arr = [...userList];
			arr = userList.filter((v) => v.name.indexOf(userSearch) !== -1);
			return arr;
		});
		return {
			filterUserList,
			...toRefs(state),
		};
	},
});
</script>

<style scoped lang="scss">
.market-details-container {
	padding: 50px;
	ul {
		margin-top: 15px;
		li {
			padding: 5px 0;
		}
	}
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值