当前位置:网站首页>Wechat applet realizes keyword highlighting
Wechat applet realizes keyword highlighting
2022-07-01 19:42:00 【Su Su is little Su Su】
1. Realization effect

2. Realization principle
1. Compare the search value with each item in the list , If that one indexOf( Search value )!=-1, It means that this item contains the keyword , Process list , Set the corresponding properties for this item .
2.split Split strings and search values into arrays , Circular array .
3. Implementation code
<view class="head flex-row">
<view class="head_input">
<image src="/img/search_icon.png" class="search_icon"></image>
<input type="text" placeholder=" Search for " placeholder-class="place_holder" bindinput="getValue" value="{
{search}}"></input>
</view>
<view class="sha_icon" catchtap="clear_input"> Cancel </view>
</view>
<view class="con">
<view class="item" wx:for="{
{ filterList }}" wx:key="index" data-index="{
{ index }}">
<text wx:for="{
{item.list}}" class="{
{i0.set &&'ative'}}" wx:key="idx" wx:for-item="i0">{
{
i0.val}}</text>
</view>
</view>
/* pages/jsCase/keyWordHight/index2.wxss */
.head {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 90rpx;
box-sizing: border-box;
padding: 0 20rpx;
background: #fff;
}
.head_input {
position: relative;
flex: 1;
margin-left: 12rpx;
}
.search_icon {
position: absolute;
top: 50%;
left: 0;
margin-top: -15rpx;
width: 28rpx;
height: 30rpx;
padding-left: 23rpx;
}
.head input {
height: 60rpx;
padding-left: 75rpx;
font-size: 28rpx;
border-radius: 30rpx;
background: #F5F5F5;
}
.place_holder {
font-size: 28rpx;
color: #999999;
}
.sha_icon {
margin-left: 18rpx;
font-size: 28rpx;
color: #333333;
}
.con {
padding-top: 90rpx;
margin: 0 20rpx;
}
.item {
border-bottom: 1rpx solid rgb(241, 239, 239);
padding: 20rpx 0;
font-size: 28rpx;
color: #333333;
}
.item:last-child {
border-bottom: none;
}
.ative {
color: rgb(76, 156, 221);
}
// pages/jsCase/keyWordHight/index2.js
Page({
/** * Initial data of the page */
data: {
search: "",
filterList: [],
list: [
{
name: " Shanghai "
},
{
name: " jiangsu "
},
{
name: " Nanjing, Jiangsu "
},
{
name: " Suqian, Jiangsu "
},
{
name: " Suzhou, Jiangsu "
},
{
name: " sichuan "
},
]
},
getValue(e) {
let val = e.detail.value;
this.setData({
search: val })
if (val.length > 0) {
let arr = [];
for (let i = 0; i < this.data.list.length; i++) {
if (this.data.list[i].name.indexOf(val) > -1) {
arr.push({
name: this.data.list[i].name })
}
}
this.setData({
filterList: arr, }, () => {
this.getHighlight(arr, val)
})
} else {
this.setData({
filterList: [], })
}
},
/** * Keyword highlighting * @param { String } datalist - Text * @param { String } val - keyword */
getHighlight(datalist, val) {
datalist.forEach(item => {
let textList = item.name.split("");
let keyList = val.split("");
let list = [];
for (let i = 0; i < textList.length; i++) {
let obj = {
set: false,
val: textList[i]
}
list.push(obj);
};
for (let k = 0; k < keyList.length; k++) {
list.forEach(i0 => {
if (i0.val === keyList[k]) {
i0.set = true;
}
})
}
item.list = list;
});
this.setData({
filterList: datalist
})
},
clear_input() {
this.setData({
search: "",
filterList: []
})
}
})
4. Source code , Focus on Su Su's code cloud !, If it works for you , One star that will do , It's an encouragement to me ~ More applet code demo, Wechat attention Su Su bug
边栏推荐
猜你喜欢

【AI服务器搭建】CUDA环境

JS的Proxy

【无标题】

uni-app微信小程序一键登录获取权限功能
![[Mori city] random talk on GIS data (I)](/img/4d/1ed4bbf397b8d756c91241705528de.jpg)
[Mori city] random talk on GIS data (I)

Introduction and installation of crunch, and making password dictionary with crunch

After studying 11 kinds of real-time chat software, I found that they all have these functions

Les canaux de culture intensive s'efforcent de développer Fu Xin et Wei Shi jiajie pour organiser une conférence de formation sur les nouveaux produits

自定义插入页面标签以及实现类似通讯录的首字母搜索

新版国标GB28181视频平台EasyGBS如何配置WebRTC视频流格式播放?
随机推荐
Native JS creates a calendar - supports mouse wheel scrolling to select months - and can be ported to any framework
Optaplanner learning notes (I) case cloud balance
Oracle物理体系结构
Actual combat of flutter - fast implementation of audio and video call application
博途V16 获取系统时间转换成字符串
Uni app product classification
GB28181之SIP协议
Mysql查询结果去除换行
axure不显示元件库
Uni app wechat applet one click login to obtain permission function
Flutter 实战-快速实现音视频通话应用
118. 杨辉三角
Introduction and installation of crunch, and making password dictionary with crunch
JVM内存模型
为什么一定要从DevOps走向BizDevOps?
[English grammar] Unit1 articles, nouns, pronouns and numerals
703. 数据流中的第 K 大元素
Optimization of video streaming with repeated requests in the case of unstable easygbs network
MySQl的基本使用
Why has instagram changed from a content sharing platform to a marketing tool? How do independent sellers use this tool?