Sample code for highlighting search keywords in WeChat mini program

Sample code for highlighting search keywords in WeChat mini program

1. Introduction

When you encounter a requirement in the project, search for data and highlight the keywords. When you receive the requirement, start working immediately. Here are the renderings first. The source code has been made into a small program code snippet and put into GitHub. There is a source code link at the bottom of the article.

2. Ideas

The first thing the blogger thought of was to use split. According to the searched keywords, he processed the data returned by the background, then found the keywords with indexOf, and added a deep field to each word. If deep is true, it will be highlighted, and if it is false, it will be normal. Since it is a small program, the OP directly made it into a highlight component. The code is very simple and the implementation steps are as follows.

3. Code logic

The simulated data is as follows

list:[
 'Wuhan University',
 'Huazhong University of Science and Technology',
 'Huazhong Normal University',
 'Zhongnan University of Economics and Law',
 'China University of Geosciences',
 'Wuhan University of Technology',
 'Huazhong Agricultural University',
 'Wuhan University of Science and Technology',
],

An empty array is defined in data to store the data filtered by the search key

filterList:[], //Filtered

wxml and methods for searching

// wxml
<view class="search_box">
 <input type="text" placeholder="Please enter the 985/211 university in Wuhan" bindinput="searchValue" class="search"/>
</view>

// Search method searchValue(e){
 let val = e.detail.value;
 this.setData({ value:val })
 if(val.length > 0){
  this.setData({ filterList:[] })
  let arr = [];
  for(let i = 0;i < this.data.list.length;i++){
   if(this.data.list[i].indexOf(val) > -1){
    arr.push(this.data.list[i])
   }
  }
  this.setData({ filterList: arr })
 }else{
  this.setData({ filterList: [] })
 }
}

Defines a highlight component highlight

"usingComponents": {
  "highlight":"../../components/highlight/highlight"
 }

Pass each item and key parameter found in the page to the component

<view class="list_li" wx:for="{{ filterList }}" wx:key="index" data-index="{{ index }}" bindtap="pitchOn">
 <highlight text="{{ item }}" key="{{ value }}" />
</view>

Receive in component

properties:
 text:String,
 key:{
  type:String,
  value:'',
  observer:'_changeText'
 }
}

Initial data of the component

data: {
 highlightList:[], //Processed data}

Component's wxml

<text class="{{ item.deep ? 'green' : '' }}" wx:for="{{ highlightList }}" wx:key="index">{{ item.val }}</text>

Component highlight data processing

// Non-empty filter_changeText(e){
  if(e.length > 0 && this.properties.text.indexOf(e) > -1){
   this._filterHighlight(this.properties.text, e);
  }
 },
 /**
 * Keyword highlighting* @param { String } text - text* @param { String } key - keyword*/
 _filterHighlight(text, key){
  let textList = text.split("");
  let keyList = key.split("");
  let list = [];
  for(let i = 0;i < textList.length;i++){
   let obj = {
    deep:false,
    val:textList[i]
   }
   list.push(obj);
  };
  for(let k = 0; k < keyList.length; k++){
   list.forEach(item => {
    if(item.val === keyList[k]){
     item.deep = true;
    }
   })
  }
  this.setData({ highlightList:list })
 }

Source code GitHub address: https://github.com/pdd11997110103/ComponentWarehouse

This concludes this article about the sample code for implementing search keyword highlighting in WeChat mini-programs. For more relevant mini-program search keyword highlighting content, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • WeChat applet uses custom component navigation to highlight the current page
  • WeChat applet implements global search code highlighting example

<<:  xtrabackup backup and restore MySQL database

>>:  Telnet is moved to busybox-extras in Alpine image

Recommend

How to modify the IP restriction conditions of MySQL account

Preface Recently, I encountered a requirement at ...

Detailed explanation of the function and usage of keepAlive component in Vue

Preface During the interview, many interviewers m...

Detailed explanation of JS memory space

Table of contents Overview 1. Stack and Heap 2. V...

How to use Navicat to operate MySQL

Table of contents Preface: 1. Introduction to Nav...

VMware installation of Centos8 system tutorial diagram (command line mode)

Table of contents 1. Software and system image 2....

Detailed explanation of Vuex environment

Table of contents Build Vuex environment Summariz...

Summary of Common Commands for Getting Started with MySQL Database Basics

This article uses examples to describe the common...

How to solve the problem of FileZilla_Server:425 Can't open data connection

When installing FileZilla Server on the server, t...

How to add indexes to MySQL

Here is a brief introduction to indexes: The purp...

Three ways to jump to a page by clicking a button tag in HTML

Method 1: Using the onclick event <input type=...

A brief discussion on whether CSS will block page rendering

Maybe everyone knows that js execution will block...

WeChat applet calculator example

This article shares the specific code of the WeCh...

Common writing examples for MySQL and Oracle batch insert SQL

Table of contents For example: General writing: S...