微信小程序搜索并高亮关键字

更多解读可使用博客:
https://www.jianshu.com/p/86d73745e01c
实现流程:
1.在文本框中输入关键字key,如“比赛”,检索出比赛相关的列表
key = 小程序
2.处理结果列表:在key的前后加分隔符,如“%%”
3.通过第2步的分隔符进行切割,获取新的数组:str.split("%%")
4.页面for循环上面的新数组:与key相等,则高亮显示,否则常规样式显示
wxml:
<!-- 搜索并高亮关键字 -->
<view class="main">
<view class="header-search">
<image class="searchImg" src="/images/search_icon.png" mode="aspectFill" ></image>
<input type="text" class="searchInput" focus="{{autoFocus}}" hold-keyborad="true" placeholder="请输入名称进行搜索" placeholder-style="color:#999999" bindinput = "input"></input>
<view class="clearInput" bindtap = "clearInput" wx:if="{{searchText}}">
<image class="clearInputImg" src="/images/clear.png" mode="aspectFill" ></image>
</view>
</view>
<view style="padding: 0 24rpx;">
<view class="searchResult" wx:if="{{searchText}}">
<view class="searchText" data-type="1" bindtap = "toHandleSearch">搜索"{{searchText}}"</view>
<view class="border-LR"></view>
</view>
<view wx:for="{{searchResultList}}" wx:key="index">
<view class="searchResultItem" data-type="2" bindtap = "toHandleSearch">
<image class="searchImg2" src="/images/search_icon.png" mode="aspectFill" ></image>
<view class="searchResultItem-context">
<text wx:for="{{item.searchArray}}" wx:for-item="item2" wx:key="index2" style="{{item2 == searchText?'color:#4E70C7':''}}">{{item2}}</text>
</view>
</view>
<view class="border-item-LR"></view>
</view>
</view>
</view>
wxss
/* pages/gologin/gologin.wxss */
/* pages/search-effects/search-effects.wxss */
.header-search{
position: relative;
margin: 10rpx 24rpx 40rpx 24rpx;
}
/*搜索图标*/
.searchImg{
position: absolute;
left: 24rpx;
bottom: 18rpx;
width: 28rpx;
height: 28rpx;
}
/*搜索图标*/
.searchImg2{
width: 28rpx;
height: 28rpx;
}
/*搜索文本框*/
.searchInput{
width: 83%;
height: 64rpx;
font-size: 28rpx;
font-family: PingFang SC;
font-style: normal;
font-weight: normal;
line-height: 64rpx;
color: #000000;
opacity: 1;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
min-height: unset;
background: #f9f9f9;
border-radius: 200rpx;
padding: 0 58rpx;
}
/*清空文本框view*/
.clearInput{
width: 100rpx;
height: 64rpx;
display: flex;
align-items: center;
justify-content: center;
position: absolute;
right: 0;
bottom: 0;
z-index: 10;
}
/*清空图标*/
.clearInputImg{
width: 36rpx;
height: 36rpx;
flex-shrink: 0;
} /*搜索结果*/
.searchResult .searchText{
max-width: 93%;
font-family: PingFang SC;
font-style: normal;
font-weight: normal;
font-size: 26rpx;
line-height: 26rpx;
color: #4e70c7;
overflow-x: hidden;
white-space: nowrap;
text-overflow: ellipsis;
padding-bottom: 40rpx;
}
.searchResultItem {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
}
.searchResultItem-context{
font-family: PingFang SC;
font-style: normal;
font-weight: normal;
font-size: 26rpx;
line-height: 26rpx;
color: #000000;
margin-left: 20rpx;
padding: 24rpx 0;
overflow-x: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
wxjs
// pages/search-effects/search-effects.js
Page({ /**
* 页面的初始数据
*/
data: {
searchText:'',//文本框内容
searchResultList:[],//搜索结果
autoFocus: true,//自动聚焦
holdKeyboard: true//focus时,点击页面的时候收齐键盘 true:不收起
}, /**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () { },
//文本框输入
input(e){
// console.log(e)
this.setData({
searchText:e.detail.value.trim()
})
//根据名称进行搜索
this.getSearchListByName()
},
//根据名称进行搜索
getSearchListByName(){
let that = this
//模拟数据请求
var dataList=[
{M_NAME:'小程序教程'},
{M_NAME:'2020小程序大全'},
{M_NAME:'微信小程序开源框架'},
{M_NAME:'微信小程序组件化解决方法'},
{M_NAME:'微信小程序API'},
{M_NAME:'丰富的微信小程序组件'},
{M_NAME:'第三方微信小程序组件'},
{M_NAME:'自定义小程序UI组件'},
{M_NAME:'小程序可滑动标签的使用'}
]
var searchResultList = []
for(var i=0;i<dataList.length;i++){
let obj={
M_NAME:dataList[i].M_NAME
};
//高亮字符串数组
obj.searchArray=that.getHilightStrArray(dataList[i].M_NAME,this.data.searchText)
searchResultList.push(obj)
}
that.setData({
searchResultList:searchResultList
})
// wx.request({
// url: 'http://192.168.0.76:8080/open/getSearchListByName', // 仅为示例,并非真实的接口地址
// data: {
// m_name:this.data.searchText
// },
// method: 'post',
// header: {
// 'content-type': 'application/json' // 默认值
// },
// success(res) {
// console.log(res)
// var searchText = that.data.searchText
// if (res.data.errcode === 200) {
// var searchResultList = []
// //比赛 数据
// if(res.data.matchList){
// for(var i=0;i<res.data.matchList.rows.length;i++){
// let obj={
// M_NAME:res.data.matchList.rows[i].M_NAME
// };
// //高亮字符串数组
// obj.searchArray=that.getHilightStrArray(res.data.matchList.rows[i].M_NAME,searchText)
// searchResultList.push(obj)
// }
// }
// that.setData({
// searchResultList:searchResultList
// })
// }
// },
// fail() {
// }
// })
},
// 返回一个使用key切割str后的数组,key仍在数组中
getHilightStrArray(str,key){
return str.replace(new RegExp(`${key}`, 'g'), `%%${key}%%`).split('%%');
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () { }, /**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () { },
})
注意:下面的函数 比较关键
//返回一个使用key切割str后的数组,key仍在数组中
//str:字符串 key:关键字
function getHilightStrArray (str,key) {
return str.replace(new RegExp(`${key}`, 'g'), `%%${key}%%`).split('%%');
}
微信小程序搜索并高亮关键字的更多相关文章
- 微信小程序--搜索关键词高亮
代码地址如下:http://www.demodashi.com/demo/14249.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
- 微信小程序 —搜索框
wxSearch优雅的微信小程序搜索框 一.功能 支持自定义热门key 支持搜索历史 支持搜索建议 支持搜索历史(记录)缓存 二.使用 1.将wxSearch文件夹整个拷贝到根目录下 2.引入 // ...
- 微信小程序----搜索框input回车搜索事件
在微信小程序里的搜索框,按软键盘回车键触发搜索事件. <input type="text" placeholder="搜索" value="{ ...
- 微信小程序搜索框代码组件
search.wxml <view class="header"> <view class="search"> <icon typ ...
- Vscode插件--微信小程序格式化以及高亮组件wxml-vscode
wxml-vscode wxml-vscode 仓库 提问题 安装 通过 F1 或者 CMD + Shift + P 输入 install. 选择: Install Extension. 特性 格式化 ...
- Hbuilder 开发微信小程序的代码高亮
一.点击“工具”-“选项”-搜索“文件关联” 二.点击“添加”文件类型,点击确定 三.在相关联的编辑器中点击“添加”按钮,选择CSS Editor,点击确定,重新打开 *.wxss 类型的文件即可 其 ...
- 两天快速开发一个自己的微信小程序
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Songti SC" } p.p2 { margin: 0.0px 0. ...
- 小程序开发运营必看:微信小程序平台运营规范
一.原则及相关说明 微信最核心的价值,就是连接——提供一对一.一对多和多对多的连接方式,从而实现人与人.人与智能终端.人与社交化娱乐.人与硬件设备的连接,同时连接服务.资讯.商业. 微信团队一 ...
- 业余时间折腾了个微信小程序版本的街机游戏模拟器(吾爱街机),8090后的童年回忆,欢迎大家体验
好多年没来博客园了,有段时间想玩街机游戏,发现都需要下载安装,现在小程序这么流行,是不是可以集成到小程序里(无需下载,在线玩),出于这想法,就业余时间折腾了下,分享给大家,偶尔可以回味畅玩下. 中间遇 ...
随机推荐
- springboot druid 数据库连接池连接失败后一直重连
在使用个人阿里云测试机,在查询实时输出日志时,看到数据库连接失败后,服务器一直在重连服务器.开始以为是遭受重复攻击,后面把服务重启后,就没有出现一直重连的情况.看以下输出日志: 2022-02-09 ...
- Java8-Consumer、Supplier、Predicate和Function方法总结
这几个接口都在 java.util.function 包下的,分别是Consumer(消费型).supplier(供给型).predicate(谓词型).function(功能性): 那么,下面,我们 ...
- opencv结构IplImage
转载请注明来源:https://www.cnblogs.com/hookjc/ typedef struct _IplImage{int nSize; /* Ip ...
- Docker容器之搭建本地私有仓库
Docker容器之搭建本地私有仓库 本地私有仓库搭建的具体步骤 首先下载 registry 镜像 docker pull registry 在 daemon.json 文件中添加私有镜像仓库的地址并重 ...
- ASP.NET Core 6框架揭秘实例演示[04]:自定义依赖注入框架
ASP.NET Core框架建立在一个依赖注入框架之上,已注入的方式消费服务已经成为了ASP.NET Core基本的编程模式.为了使读者能够更好地理解原生的注入框架框架,我按照类似的设计创建了一个简易 ...
- Solution -「UR #2」「UOJ #32」跳蚤公路
\(\mathcal{Description}\) Link. 给定一个 \(n\) 个点 \(m\) 条边的带权有向图,每条边还有属性 \(s\in\{-1,0,1\}\).对于每个 \(u ...
- [入门到吐槽系列] Webix 10分钟入门 二 表单Form的使用
前言 继续接着上一篇的webix入门:https://www.cnblogs.com/zc22/p/15912342.html.今天完成剩下两个最重要的控件,表单和表格的使用.掌握了这两个,整个Web ...
- Failed to restart ssh.service: Unit not found.
环境 操作系统:CentOS 7 问题 重启ssh服务,启动报错:Failed to restart ssh.service: Unit not found. 操作步骤 1. 编辑sshd_confi ...
- Python中模块、类、函数、实例调用案例
19 a = '我是模块中的变量a' 20 21 def hi(): 22 a = '我是函数里的变量a' 23 print('函数"hi"已经运行!') 24 25 class ...
- Burp suite基本配置介绍
实验目的 利用Burp Spider功能探测目标网站的目录结构. 实验原理 1)Burp Suite是Web应用程序测试的最佳工具之一,其多种功能可以帮我们执行各种任务.请求的拦截和修改,扫描web应 ...