
更多解讀可使用博客:
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('%%'); }
