In many web applications, data filtering and search are key user experience features. As a popular front-end framework, many tools and technologies are provided to enable developers to implement these features easily. This article will explore in-depth how to perform data filtering and search implementations in Vue, as well as provide sample code to help you get started.
Basic concepts of data filtering and search
The core goal of the data filtering and search function is to filter out items that meet specific conditions from a set of data. These conditions may be determined based on user input, selected filters, or other parameters. Here are some common use cases:
- Text-based search: Search items in the dataset based on keywords entered by the user.
- Condition-based filtering: Filter data based on user-selected conditions (such as date range, price range, etc.).
- Combination of multiple filter criteria: Allows users to apply multiple filter criteria at the same time.
In Vue, you can implement these functions using computed properties and methods, and use Vue's responsive data binding to automatically update the interface.
Example: Text-based search
Let's start with a simple text-based search feature. We will create a Vue application that contains a set of movie data and allow users to search for these movies.
Create a Vue application
First, create a Vue application. You can use the Vue CLI to quickly set up a new Vue project:
vue create movie-app
Prepare movie data
In the Vue application, you need an array containing movie data. You can hardcode data into Vue components, or get data from an API or external data source. Here is an example movie data array:
const movies = [ { title: 'Movie 1', year: 2020 }, { title: 'Movie 2', year: 2019 }, { title: 'Movie 3', year: 2021 }, // ...More movie data];
Create Vue Components
Create a Vue component to display movie data and search boxes. In src/components/, add the following code:
<template> <div> <input type="text" v-model="searchTerm" placeholder="Search movies..." /> <ul> <li v-for="movie in filteredMovies" :key=""> {{ }} ({{ }}) </li> </ul> </div> </template> <script> export default { data() { return { movies: [...], // Movie data array searchTerm: '' // Search for keywords }; }, computed: { filteredMovies() { // Use computed properties to filter movie data return (movie => ().includes(()) ); } } }; </script>
In the above code, we create a Vue component that contains a search box and a list of movies. Use the v-model directive to bind the value of the search box to the searchTerm data attribute. We then use the computed attribute filteredMovies to filter the movie data, showing only the movies containing the search keywords.
Using Vue Components
Use the MovieList component in your Vue application. In src/, add the following code:
<template> <div > <h1>Movie Search App</h1> <movie-list /> </div> </template> <script> import MovieList from './components/'; export default { components: { MovieList } }; </script>
Run the Vue application
Now you can run the Vue application:
npm run serve
Open your browser and visit http://localhost:8080 and you will see a simple movie search application.
Example: Condition-based filtering
Next, let's look at a more complex example where users can filter movie data based on conditions.
Update Vue components
In the component, add a set of filters and a button to allow the user to filter movie data:
<template> <div> <!-- Search box --> <input type="text" v-model="searchTerm" placeholder="Search movies..." /> <!-- Filter criteria --> <label> Year: <select v-model="selectedYear"> <option value="">All</option> <option v-for="year in uniqueYears" :value="year">{{ year }}</option> </select> </label> <!-- Filter buttons --> <button @click="filterMovies">Filter</button> <!-- Movie List --> <ul> <li v-for="movie in filteredMovies" :key=""> {{ }} ({{ }}) </li> </ul> </div> </template> <script> export default { data() { return { movies: [...], // Movie data array searchTerm: '', // Search for keywords selectedYear: '', // Selected year }; }, computed: { filteredMovies() { // Use computed properties to filter movie data let filtered = ; if () { filtered = (movie => ().includes(()) ); } if () { filtered = (movie => === parseInt()); } return filtered; }, uniqueYears() { // Computation properties: Get the list of unique years in movie data const years = new Set((movie => )); return [''].concat((years)); }, }, methods: { filterMovies() { // Call this method when the user clicks the filter button // Update the movie list }, }, }; </script>
In the above code, we have added a drop-down menu for filters that allows the user to select a specific year. In the computed attribute filteredMovies, we filter movie data based on search keywords and selected year. The uniqueYears computed attribute is used to get a list of unique years in movie data.
Update the use of Vue components
In, update the use of the component to include filters and buttons:
<template> <div > <h1>Movie Search and Filter App</h1> <movie-list /> </div> </template> <script> import MovieList from './components/'; export default { components: { MovieList } }; </script>
Run the Vue application
npm run serve
Now, your movie search and filter app has more features. Users can filter movie data based on year and search keywords.
Summarize
Implementing data filtering and search functions in Vue applications is a common but important task. Vue's responsive data binding, computed properties, and methods make this task relatively simple.
This is the article about Vue's sample code to implement data filtering and search functions. For more related vue data filtering and search content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!