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

MySQL decimal unsigned update negative numbers converted to 0

Today, when verifying the concurrency problem of ...

Docker builds kubectl image implementation steps

If the program service is deployed using k8s inte...

Sample code for testing technology application based on Docker+Selenium Grid

Introduction to Selenium Grid Although some new f...

How to use pdf.js to preview pdf files in Vue

When we preview PDF on the page, some files canno...

How to use docker+devpi to build local pypi source

Some time ago, I needed to use pip downloads freq...

Detailed explanation of the basic usage of the img image tag in HTML/XHTML

The image tag is used to display an image in a we...

Learn MySQL in a simple way

Preface The database has always been my weak poin...

Mysql date formatting and complex date range query

Table of contents Preface Query usage scenario ca...

Analysis of the Linux input subsystem framework principle

Input subsystem framework The linux input subsyst...

What are mysql dirty pages?

Table of contents Dirty pages (memory pages) Why ...

Teach you how to build Tencent Cloud Server (graphic tutorial)

This article was originally written by blogger We...