当前位置:网站首页>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
边栏推荐
- HLS4ML进入方法
- Test self-study people must see: how to find test items in software testing?
- ffmpeg常用命令(二)
- Nat penetration of gb28181
- Audio and video, encoding and decoding related e-books, gadgets, packaged for free!
- axure不显示元件库
- Interview questions for audio and video positions in Dachang -- today's headline
- Ffmpeg avframe to cv:: mat
- 通过js实现金字塔(星号金字塔,回文对称数字金字塔)
- H264 encoding profile & level control
猜你喜欢
随机推荐
AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
Brpc understanding
Botu V16 obtains the system time and converts it into a string
Ubuntu14 install MySQL and configure root account local and remote access
博途V16 获取系统时间转换成字符串
Interview question 16.16 Partial sorting - Double finger needling
Wireshark packet analysis TCP, FTP
再回顾集合容器
uni-app微信小程序一键登录获取权限功能
Introduction to relevant processes and functions of wechat official account development
测试自学人必看:软件测试如何找测试项目?
qobject_cast用法
Define dichotomy lookup
GetMessage底层机制分析
为什么一定要从DevOps走向BizDevOps?
Optaplanner learning notes (I) case cloud balance
正则表达式=Regex=regular expression
Anaconda安装虚拟环境到指定路径
数字化转型企业成功的关键,用数据创造价值
Collation of open source protocols of open source frameworks commonly used in Web Development








