Home  >  Article  >  Web Front-end  >  How to implement search association in Vue?

How to implement search association in Vue?

王林
王林Original
2023-06-25 09:28:231688browse

With the continuous development of network technology, more and more websites and applications provide search functions to facilitate users to quickly find what they need. Search association, also known as "auto-fill", is a function that allows users to find the content they want to search faster. In Vue, the search association function can be easily implemented. This article will introduce how to use Vue to create a simple search association component.

First, you need a data source containing the options to be searched in order to generate an association list. Here, we can use a simple array with some fake data:

const options = [
  { value: 'Java', label: 'Java' },
  { value: 'JavaScript', label: 'JavaScript' },
  { value: 'Python', label: 'Python' },
  { value: 'Ruby', label: 'Ruby' },
  { value: 'Swift', label: 'Swift' },
  { value: 'Kotlin', label: 'Kotlin' },
  { value: 'C#', label: 'C#' },
  { value: 'Go', label: 'Go' },
  { value: 'PHP', label: 'PHP' },
  { value: 'TypeScript', label: 'TypeScript' }
]

Now, we need to create a Vue component that displays the search box and prediction list. In this component, we will create a data object to store all relevant state and properties, which contains the current search term, and the list of associations to be displayed. We will also introduce a Vue computed property filteredOptions for filtering the options array based on the current search term. Finally, we need to add some methods for handling user input and selecting the association item.

<template>
  <div class="search-wrapper">
    <input type="text" v-model="searchTerm" @input="handleInput" @keydown.enter="handleEnter">
    <ul v-if="showList">
      <li v-for="(option, index) in filteredOptions" :key="index" @click="handleSelect(index)">{{ option.label }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  data () {
    return {
      searchTerm: '',
      showList: false,
      options: [
        { value: 'Java', label: 'Java' },
        { value: 'JavaScript', label: 'JavaScript' },
        { value: 'Python', label: 'Python' },
        { value: 'Ruby', label: 'Ruby' },
        { value: 'Swift', label: 'Swift' },
        { value: 'Kotlin', label: 'Kotlin' },
        { value: 'C#', label: 'C#' },
        { value: 'Go', label: 'Go' },
        { value: 'PHP', label: 'PHP' },
        { value: 'TypeScript', label: 'TypeScript' }
      ]
    }
  },
  computed: {
    filteredOptions () {
      return this.options.filter(option => option.label.toLowerCase().includes(this.searchTerm.toLowerCase()))
    }
  },
  methods: {
    handleInput () {
      this.showList = true
    },
    handleEnter () {
      if (this.filteredOptions.length > 0) {
        this.searchTerm = this.filteredOptions[0].label
        this.showList = false
      }
    },
    handleSelect (index) {
      this.searchTerm = this.filteredOptions[index].label
      this.showList = false
    }
  }
}
</script>

In the code, we first set up a data object, which contains the search terms entered by the current user and the Boolean value that controls whether the association list is displayedshowList , and data sourceoptions. To implement the filtering functionality, we use a computed property filteredOptions to filter the options based on the search term and map it to a new array. At the same time, we have defined 3 methods:

  • handleInput: A method that is triggered when the value of the input box changes. This method will showList Set to true.
  • handleEnter: Method triggered when the user presses the Enter key. This method checks whether there are currently association options and, if so, sets the value of the first option to the search term and showList to false.
  • handleSelect: Method triggered when the user clicks an option in the association list. This method sets the value of the selected option to the search term and showList to false.

Finally, we defined a search box and an association list in template, in which the value of the search box is bound to searchTerm. When the user When inputting content, the handleInput method will be called, thereby updating the status of showList. If showList is true, the association list is shown. Each option in the list is bound to an element in the filteredOptions array and rendered using v-for. When the user clicks on an option, the handleSelect method is called, setting the value of the selected option and closing the prediction list.

To sum up, we can see that it is not difficult to use Vue to implement search association. It's easy to create a search prediction component by setting up the data source as an array, filtering options based on search terms, and using simple methods and events to handle user input and selections.

The above is the detailed content of How to implement search association in Vue?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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