当前位置:网站首页>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
边栏推荐
- 【无标题】
- After studying 11 kinds of real-time chat software, I found that they all have these functions
- Methods of finding various limits
- 音频编解码基础知识
- Introduction to relevant processes and functions of wechat official account development
- 研究了11种实时聊天软件,我发现都具备这些功能…
- ffmpeg AVFrame 转 cv::Mat
- 直播HLS协议
- Uni app wechat applet one click login to obtain permission function
- Audio and video, encoding and decoding related e-books, gadgets, packaged for free!
猜你喜欢
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
DDR4 test-2
GC garbage collection
axure不显示元件库
Instagram 为何从内容共享平台变成营销工具?独立站卖家如何利用该工具?
精耕渠道共謀發展 福昕攜手偉仕佳傑開展新產品培訓大會
SQL 入门计划-1-选择
Facebook聊单,SaleSmartly有妙招!
JS 之 常用内置类的使用
【AI服务器搭建】CUDA环境
随机推荐
Summary of SQL query de duplication statistics methods
Learning records of building thingsboard, an Internet of things platform
GB28181的NAT穿透
Uni app product classification
Use the uni app demo provided by Huanxin to quickly realize one-on-one chat
What is the essential difference between Bi development and report development?
新窗口打开页面-window.open
使用 Kibana Timelion 进行时间序列分析
解决VSCode下载慢或下载失败的问题
数字化转型企业成功的关键,用数据创造价值
MySQL signale une erreur can 't create table' demo01. TB Étudiant '(errno: 150)
GetMessage底层机制分析
New window open page -window open
Botu V16 obtains the system time and converts it into a string
一文读懂C语言中的结构体
【sql优化】with as 和 临时表的区别
Salesmartly has some tricks for Facebook chat!
为什么一定要从DevOps走向BizDevOps?
uni-app商品分类
类加载机制