Vue实现模糊查询

本文介绍了如何在Vue和Vue3中使用JavaScript的filter和includes方法,结合v-for指令以及响应式特性实现模糊查询。给出了Vue实例和Vue3组件的代码示例。
摘要由CSDN通过智能技术生成

在Vue中实现模糊查询,你可以使用JavaScript的filterincludes方法,结合Vue的v-for指令。下面是一个简单的例子:

首先,你需要在你的Vue实例中定义一个数据数组和一个查询字符串。

data() {
return {
items: ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'],
query: ''
}
}

然后,你可以在你的模板中使用v-for来遍历这个数组,并使用v-model来绑定查询字符串。

<input v-model="query" type="text" placeholder="Search...">
<ul>
<li v-for="item in filteredItems" :key="item">
{{ item }}
</li>
</ul>

在上面的代码中,filteredItems是一个计算属性,它会返回过滤后的数组。你可以使用JavaScript的filterincludes方法来实现模糊查询。

computed: {
filteredItems() {
return this.items.filter(item => item.toLowerCase().includes(this.query.toLowerCase()));
}
}

在上面的代码中,filter方法会遍历数组中的每个元素,并返回一个新的数组,该数组只包含满足条件的元素。includes方法会检查一个字符串是否包含另一个字符串。在这个例子中,我们使用toLowerCase方法将字符串转换为小写,以便进行不区分大小写的查询。

当使用Vue3实现模糊查询时,你可以利用Vue的响应式特性和计算属性来实现。下面是一个简单的说明和代码示例:

说明:

  1. 创建一个Vue3组件,并引入必要的依赖。
  2. 在组件的setup()函数中,定义数据和计算属性。
  3. 使用v-model指令将输入框的值绑定到数据属性上。
  4. 定义一个计算属性filteredItems,根据输入框的值过滤数据数组。
  5. 在模板中使用v-for指令遍历过滤后的数据数组,并显示每个元素。

代码示例:

<template>
<div>
<input v-model="searchQuery" type="text" placeholder="Search...">
<ul>
<li v-for="item in filteredItems" :key="item.id">
{{ item.name }}
</li>
</ul>
</div>
</template>

<script>
import { ref } from 'vue';

export default {
setup() {
const items = ref([
{ id: 1, name: 'Apple' },
{ id: 2, name: 'Banana' },
{ id: 3, name: 'Cherry' },
{ id: 4, name: 'Date' },
{ id: 5, name: 'Elderberry' }
]);
const searchQuery = ref('');
const filteredItems = computed(() => {
if (!searchQuery.value) return items.value;
return items.value.filter(item => item.name.toLowerCase().includes(searchQuery.value.toLowerCase()));
});
return { items, searchQuery, filteredItems };
}
};
</script>

在上面的代码中,我们首先导入了Vue3的ref函数,用于创建响应式引用。然后,在组件的setup()函数中,我们定义了两个响应式引用itemssearchQuery,分别表示数据数组和查询字符串。接下来,我们定义了一个计算属性filteredItems,根据输入框的值过滤数据数组。最后,在模板中,我们使用v-model指令将输入框的值绑定到searchQuery上,并使用v-for指令遍历filteredItems数组,显示每个项目的名称。当输入框的值发生变化时,计算属性会自动更新,并触发重新渲染。

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在前端页面上添加一个输入框用于输入搜索关键字,并且在点击搜索按钮时将关键字传递到后端。 然后,在后端的控制器中,接收前端传递的关键字参数,并且调用MyBatisPlus提供的模糊查询API进行查询。 具体实现步骤如下: 1.前端页面实现前端页面上添加一个输入框和搜索按钮,当用户在输入框输入了关键字并点击搜索按钮时,将关键字传递到后端。 ```vue <template> <div> <input v-model="keyword" /> <button @click="search">搜索</button> </div> </template> <script> export default { data() { return { keyword: '', }; }, methods: { search() { this.$axios.get('/api/search', { params: { keyword: this.keyword } }).then((res) => { console.log(res.data); }); }, }, }; </script> ``` 2.后端控制器实现 在后端控制器中,接收前端传递的关键字参数,并且调用MyBatisPlus提供的模糊查询API进行查询。这里我们使用MyBatisPlus提供的QueryWrapper对象来构建查询条件,并且使用Lambda表达式来简化代码。 ```java @RestController @RequestMapping("/api") public class SearchController { @Autowired private UserService userService; @GetMapping("/search") public List<User> search(String keyword) { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.like("name", keyword).or().like("email", keyword); return userService.list(wrapper); } } ``` 在上面的代码中,我们构建了一个QueryWrapper对象,使用like方法来查询name或者email字段中包含关键字的数据。最终调用userService的list方法来执行查询操作。 需要注意的是,在这里我们假设UserService是一个MyBatisPlus提供的服务类,并且已经配置好了MapperScannerConfigurer来扫描Mapper接口。如果还没有配置,可以参考MyBatisPlus的文档进行配置。 3.配置MyBatisPlus 在使用MyBatisPlus的模糊查询功能前,需要先配置MyBatisPlus。在SpringBoot中,只需要在配置文件中添加如下配置即可: ```yaml mybatis-plus: mapper-locations: classpath:/mapper/**/*.xml global-config: db-config: logic-delete-value: 1 logic-not-delete-value: 0 configuration: map-underscore-to-camel-case: true ``` 在上面的配置中,我们指定了Mapper接口XML文件的位置,配置了逻辑删除的值和驼峰命名规则等。 4.编写Mapper接口和XML文件 最后,我们需要编写Mapper接口和XML文件来实现具体的查询操作。这里我们以User表为例,编写如下代码: ```java public interface UserMapper extends BaseMapper<User> { } ``` ```xml <mapper namespace="com.example.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="email" property="email" /> </resultMap> <select id="selectByKeyword" resultMap="BaseResultMap"> select * from user where name like concat('%', #{keyword}, '%') or email like concat('%', #{keyword}, '%') </select> </mapper> ``` 在上面的代码中,我们使用了MyBatisPlus提供的BaseMapper接口,并且编写了一个selectByKeyword方法来执行模糊查询操作。需要注意的是,我们使用了concat函数来将%和关键字拼接起来,以实现模糊查询的效果。 至此,我们已经完成了springboot+mybatisplus+vue实现模糊查询的全部操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值