Home Web Front-end Vue.js How to implement search and filtering functions through vue and Element-plus

How to implement search and filtering functions through vue and Element-plus

Jul 16, 2023 pm 10:09 PM
vue filter search

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.

  1. 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
Copy after login
  1. 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
Copy after login

Then, select the configuration options according to the prompts, or directly use the default configuration to generate the Vue application.

  1. Import Element-Plus components
    In the created Vue application, we need to import the related components of Element-Plus. Open the src/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')
Copy after login

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.

  1. Create search and filter components
    We can create two Vue components to implement search and filter functions. Create two files SearchBar.vue and FilterBar.vue in the src/components directory. Add the following code in the SearchBar.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>
Copy after login

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>
Copy after login

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
  1. In the App component, we can use the search and filter components created previously. Open the
    src/App.vue file and add the following code:
  2. <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>
    Copy after login
Here, we imported the SearchBar and FilterBar components in the App component and passed

< SearchBar @search="handleSearch">andBind 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
  1. Finally, we can run the following command in the command line to start the application:
  2. npm run serve
    Copy after login
    Then access

    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.

    Conclusion:

    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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

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.

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

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.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

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.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

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

React vs. Vue: Which Framework Does Netflix Use? React vs. Vue: Which Framework Does Netflix Use? Apr 14, 2025 am 12:19 AM

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

What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

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.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

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

How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

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.

See all articles