


How to implement search and filtering functions through vue and Element-plus
How to implement search and filtering functions through Vue and Element-Plus
Introduction:
In modern web applications, search and filtering functions are a very important part, which can help users quickly find what they want. Need information. Vue is a popular JavaScript framework, and Element-Plus is a UI component library for Vue. Their combination can easily implement search and filtering functions. This article will introduce how to use Vue and Element-Plus to implement these functions, and provide relevant code examples.
- Preparation
First, we need to install Vue and Element-Plus. You can install them with the following command:
npm install vue npm install element-plus
- Create Vue Application
Next, we need to create a Vue application. We can use Vue's scaffolding tools to create a basic Vue application. Run the following command in the command line:
vue create search-filter-app cd search-filter-app
Then, select the configuration options according to the prompts, or directly use the default configuration to generate the Vue application.
- Import Element-Plus components
In the created Vue application, we need to import the related components of Element-Plus. Open thesrc/main.js
file and add the following code:
import { createApp } from 'vue' import App from './App.vue' import ElementPlus from 'element-plus' createApp(App).use(ElementPlus).mount('#app')
Here we use the ES6 module import syntax and import the createApp
function and Requires the use of Element-Plus components. Then we created a Vue application using the createApp
function and used Element-Plus in the application.
- Create search and filter components
We can create two Vue components to implement search and filter functions. Create two filesSearchBar.vue
andFilterBar.vue
in thesrc/components
directory. Add the following code in theSearchBar.vue
file:
<template> <div> <el-input v-model="searchKeyword" placeholder="请输入搜索关键字"></el-input> </div> </template> <script> export default { data() { return { searchKeyword: '' } }, watch: { searchKeyword(newKeyword) { this.$emit('search', newKeyword) } } } </script>
Add the following code in the FilterBar.vue
file:
<template> <div> <el-select v-model="filterOption" placeholder="请选择过滤条件" @change="filterData"> <el-option label="选项1" value="option1"></el-option> <el-option label="选项2" value="option2"></el-option> <el-option label="选项3" value="option3"></el-option> </el-select> </div> </template> <script> export default { data() { return { filterOption: '' } }, methods: { filterData() { this.$emit('filter', this.filterOption) } } } </script>
here , we created two components respectively, and used the Element-Plus input box and drop-down selection box components in the components. Note that in the SearchBar
component we use the v-model
directive to implement two-way data binding, and monitor searchKeyword## in the
watch option # changes and passes the value to the parent component through the
$emit method.
- Using search and filter components
- In the App component, we can use the search and filter components created previously. Open the
src/App.vuefile and add the following code:
<template> <div> <SearchBar @search="handleSearch"></SearchBar> <FilterBar @filter="handleFilter"></FilterBar> <ul> <li v-for="item in filteredData" v-bind:key="item.id">{{ item.name }}</li> </ul> </div> </template> <script> import SearchBar from './components/SearchBar.vue' import FilterBar from './components/FilterBar.vue' export default { components: { SearchBar, FilterBar }, data() { return { data: [ { id: 1, name: 'item1', option: 'option1' }, { id: 2, name: 'item2', option: 'option2' }, { id: 3, name: 'item3', option: 'option3' } ], searchKeyword: '', filterOption: '' } }, computed: { filteredData() { let result = this.data if (this.searchKeyword) { result = result.filter(item => item.name.toLowerCase().includes(this.searchKeyword.toLowerCase())) } if (this.filterOption) { result = result.filter(item => item.option === this.filterOption) } return result } }, methods: { handleSearch(keyword) { this.searchKeyword = keyword }, handleFilter(option) { this.filterOption = option } } } </script>
< SearchBar @search="handleSearch">and
Bind events to methods of App components . A data array is defined in data, and filtered according to the search keywords and filter conditions to obtain the filteredData array. Then use the
v-for directive to render each element in the filteredData array as a list item.
- Run the code
- Finally, we can run the following command in the command line to start the application:
npm run serve
http in the browser ://localhost:8080, you will see a page with a search box and a drop-down selection box. When you enter search keywords or select filter conditions, the data in the list will be searched and filtered based on the input.
With Vue and Element-Plus, we can easily implement search and filtering functions. We used Vue's data binding and Element-Plus's input box and drop-down selection box components to apply search keywords and filter conditions to the data through the transmission of events and data, thus realizing the search and filtering functions. The above is a simple example, you can further develop and customize it according to your own needs.
The above is the detailed content of How to implement search and filtering functions through vue and Element-plus. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values for each element; and the .map method can convert array elements into new arrays.
