Element-ui's built-in two remote search (fuzzy query) usage explanation

Element-ui's built-in two remote search (fuzzy query) usage explanation

Problem Description

There is a type of query called front-end remote search and fuzzy query. Ele.me comes with two ways to do this, one is to use el-autocomplete in el-input, and the other is to use el-select and el-option. Both of these options are acceptable, but the specific implementation ideas should be discussed with the backend. Who does the fuzzy query?

If the backend does

Then the front end only needs to throw the keywords entered by the user in the input box to the back end. The back end will perform fuzzy query in the database based on the keywords that the user wants to query passed by the front end, and throw the related data found to the front end. After the front end gets the data, it can directly present it to the user. Save time on the front end

If the front end does

Under normal circumstances, the backend will actually do more fuzzy queries. Suppose a user enters the character "王" and wants to query all the data containing the character "王". If there are tens of thousands of data in the database, will the backend throw tens of thousands of data to the frontend at once? The front end then filters, selects and searches? This will cause the front end to be stuck for a long time and the data will be inaccurate, because when the front end filters the data returned from the back end, the data may have changed, etc. But it doesn’t mean that the front end can’t be done. This article introduces two methods provided by Ele.me. I personally prefer the second method. Without further ado, here's the code...

Method 1

Method 1 effect diagram

Entering the word "Sun" will bring up related data, which is the meaning of fuzzy query.

<template>
 <div id="app">
  <!-- For remote search, use filterable and remote -->
  <el-select
   v-model="value"
   filterable
   remote
   placeholder="Please enter keywords"
   :remote-method="remoteMethod"
   :loading="loading"
  >
   <!-- Hook function encapsulated by remote-method. When the user enters content in the input box, the execution of this function will be triggered.
   The value corresponding to the input box is passed as a parameter to the callback function. Loading means the waiting time during remote search, that is: loading -->
   <el-option
    v-for="item in options"
    :key="item.value"
    :label="item.label"
    :value="item.value"
   >
   </el-option>
  </el-select>
 </div>
</template>
<script>
export default {
 name: "app",
 data() {
  return {
   options: [],
   value: [],
   loading: false,
  };
 },
 methods: {
  // When the user enters content to start remote search fuzzy query, the remoteMethod method remoteMethod(query) {
   // If the user enters content, send a request to get data and remotely search for fuzzy query if (query !== "") {
    this.loading = true; // Start getting data // Here we simulate sending a request, and res will be regarded as the data returned by the request.
    let res = [
     {
      label: "Sun Wukong",
      value: 500,
     },
     {
      label: "Sun Shangxiang",
      value: 18,
     },
     {
      label: "Sha Monk",
      value: 1000,
     },
     {
      label: "Sha Junior Brother",
      value: 999,
     },
    ];
    this.loading = false // Get the data // Then filter all the data obtained first, filter the related data into a new array and give it to options using this.options = res.filter((item)=>{

     // indexOf equal to 0 means that only the first word is matched, such as: searching for Wang Wang Xiaohu's data will be filtered out, but Xiaohu Wang's data will not be filtered out. Because indexOf equals 0, it means it starts with something // return item.label.toLowerCase().indexOf(query.toLowerCase()) == 0

     // indexOf greater than -1 means that as long as the word appears, it will be filtered out, such as: searching for Wang Wang Xiaohu, Xiao Hu Wang, and Xiao Wang Hu will all be filtered out. Because indexOf cannot find it, it will return -1.
     // Greater than -1 means it's OK as long as it exists, no matter if it's at the beginning, middle, or end return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1
    })
   } else {
    this.options = [];
   }
  },
 },
};
</script>

To be honest, I personally prefer method 2. Come on, put the code

Method 2

Method 2 effect diagram

<template>
 <div id="app">
  <div>
   <el-autocomplete
    v-model="state2"
    :fetch-suggestions="querySearch"
    placeholder="Please enter content"
    :trigger-on-focus="false"
    @select="handleSelect"
    size="small"
   ></el-autocomplete>
  </div>
  <div>
   <ul>
    <li v-for="(item, index) in fruit" :key="index">{{ item.value }}</li>
   </ul>
  </div>
 </div>
</template>
<script>
export default {
 name: "app",
 data() {
  return {
   state2: "",
   fruit:
    {
     value: "Banana",
     price: "8.58",
    },
    {
     value: "Cherry",
     price: "39.99",
    },
    {
     value: "Walnut",
     price: "26.36",
    },
    {
     value: "mango",
     price: "15.78",
    },
   ],
  };
 },
 methods: {
  // Step 2 // When queryString is not empty, it means the user has entered something. We compare the user's input in the database. If there is any fuzzy association, we directly retrieve it // and return it to the input box with search suggestions. The input box presents the returned data in the form of a drop-down box for the user to choose.
  querySearch(queryString, cb) {
   if (queryString != "") {
    // Do fuzzy search after entering content setTimeout(() => {
     let callBackArr = []; // Prepare an empty array, which is the array that is finally returned to the input box // This res is the data obtained from the backend after sending the request const res = [
      {
       value: "Apple",
       price: "13.25",
      },
      {
       value: "Apple 1",
       price: "13.25",
      },
      {
       value: "Apple 2",
       price: "13.25",
      },
      {
       value: "Apple 3",
       price: "13.25",
      },
      {
       value: "Apple 4",
       price: "13.25",
      },
      {
       value: "Apple 5",
       price: "13.25",
      },
      
     ];
     res.forEach((item) => {
      // Traverse the database and compare the word entered by the user with each item in the database // if (item.value.indexOf(queryString) == 0) { // equal to 0 and starts with something if (item.value.indexOf(queryString) > -1) { // greater than -1, as long as it is included, the position does not matter // If there is related data callBackArr.push(item); // store it in callBackArr and prepare to return and present }
     });
     // After this wave of query operations, if the array is still empty, it means that no related data has been found, and it will be directly returned to the user that there is no data if (callBackArr.length == 0) {
      cb([{ value: "No data", price: "No data" }]);
     }
     // If data is found after this wave of query operations, the array callBackArr containing the related data is presented to the user else {
      cb(callBackArr);
     }
    }, 1000);
   }
  },
  // Click on whoever is put in handleSelect(item) {
   this.fruit = []
   this.fruit.push(item)
  },
 },
};
</script>

Summarize

Both are similar, which is to request data, get data, process and filter data, and present data. The case in this article is that fuzzy query filtering and screening of data is done by the front end, but of course it can also be done by the back end. When doing specific projects, you can discuss with the back end.

This is the end of this article about the usage of the two remote searches (fuzzy queries) that come with Element-ui. For more relevant Element-ui fuzzy query content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue implements the fuzzy query method of Input input box
  • Vue implements the sample code of fuzzy query of input box (application scenario of throttling function)
  • Vue2 filter fuzzy query method
  • Sample code for fuzzy query of Vue input box

<<:  Historical Linux image processing and repair solutions

>>:  Related operations of adding and deleting indexes in mysql

Recommend

Linux nohup to run programs in the background and view them (nohup and &)

1. Background execution Generally, programs on Li...

Mariadb remote login configuration and problem solving

Preface: The installation process will not be des...

Detailed explanation of the use of Vue mixin

Table of contents Use of Vue mixin Data access in...

Example of implementing the skeleton screen of WeChat applet

Table of contents What is a skeleton screen How t...

Import csv file into mysql using navicat

This article shares the specific code for importi...

MySQL starts slow SQL and analyzes the causes

Step 1. Enable MySQL slow query Method 1: Modify ...

Tutorial on installing Ubuntu 20.04 and NVIDIA drivers

Install Ubuntu 20.04 Install NVIDIA drivers Confi...

Specific use of nginx keepalive

The default request header of the http1.1 protoco...

HTML table markup tutorial (6): dark border color attribute BORDERCOLORDARK

In a table, you can define the color of the lower...

Example code for evenly distributing elements using css3 flex layout

This article mainly introduces how to evenly dist...

How to implement the @person function through Vue

This article uses vue, and adds mouse click event...