最近姐姐又琢磨微信小程序了,其中需要實現小程序搜索功能,網上找了些不錯的資料,挑選了一些資料仿着實現了搜索功能,整理了代碼分享給大家。
參考資料:
https://www.runoob.com/w3cnote/javascript-table-search-filter.html
本人微信小程序主要學習教程(老師講得還是不錯的,重點后面講解了實例加深了理解,但是這些都沒有親自實踐根據需求寫上代碼來的有效):
https://www.bilibili.com/video/BV11A411Y75o?p=1
微信小程序實現搜索功能我用了三個page:
page1 -- SearchInput 界面(注意,因為搜索界面是經常在其他界面會被調用到的,所以這里把它模塊化了,我把它放在和 pages 同層級新建的一個 components 的文件夾的子文件夾 SearchInput 下,然后用右鍵點擊 SearchInput 文件夾,選擇新建 Component 的方法來創建它,並修改以下內容)
(1)SearchInput.wxml
1 <view class="search_input">
2 <navigator url="/pages/search/search" class="iconfont icon-search" open-type="navigate">
3 <icon class="searchicon" size='20' type='search'></icon>
4 <text >搜索</text>
5 </navigator>
6 </view>
(2)SearchInput.wxss
1 .search_input {
2 height: 90rpx;
3 padding: 30rpx;
4 background-color: #04cfff;
5 }
6 .search_input .searchicon {
7 margin-right: 20rpx;
8 }
9 .search_input navigator {
10 height: 100%;
11 display: flex;
12 justify-content: center;
13 align-items: center;
14 background-color: #fff;
15 border-radius: 15rpx;
16 color: #666;
17 }
page2 -- report界面
(1) report.wxml
1 <!-- bindtap是傳遞點擊事件 data-index是點擊時要傳遞的參數 -->
2 <view>
3 <SearchInput></SearchInput>
4 <view class="report_list">
5 <scroll-view scroll-y>
6 <view 7 class="menu_item"
8 wx:for="{{reportList}}"
9 wx:key="*this"
10 bindtap="handleItemTap"
11 data-index="{{index}}"
12 >
13 <view class="menu_wrap">
14 <view class="menu_image">
15 <image src="../../icons/baogao.png" mode="widthFix"/>
16 </view>
17
18 <view class="menu_time">
19 <text decode="{{true}}">日期: </text>
20 {{item}} 21 </view>
22 </view>
23 </view>
24
25 </scroll-view>
26 </view>
27 </view>
代碼講解:通過第3行實現 SearchInput 組件的調用。這里我搜索的內容就是 scroll-view 列表顯示的標題,所以設置了一個可縱向滾動視圖區域,其中顯示的內容存儲在 reportList 中,bindtap是點擊事件所調用的函數,data-index是點擊時要傳遞的參數。
(2)report.wxss
1 .report_list .menu_wrap {
2 display: flex;
3 }
4 .report_list .menu_wrap .menu_image {
5 flex: 1;
6 display: flex;
7 justify-content: center;
8 align-items: center;
9 }
10 .report_list .menu_wrap .menu_image image {
11 width: 40%;
12 margin-top: 20rpx;
13 margin-bottom: 20rpx;
14 }
15 .report_list .menu_wrap .menu_time {
16 flex: 3;
17 display: flex;
18 border-bottom: 1rpx solid #ccc;
19 font-size: 32rpx;
20 justify-content: center;
21 align-items: center;
22 }
(3)report.js
1 Page({ 2 data: { 3 reportList: [], 4 currentClickIndex: 0, 5 }, 6
7 onLoad: function (options) { 8 // 1 發送異步請求獲取數據
9 wx.request({ 10 url: 'http://......', 11 success: (result)=>{ 12 console.log("請求成功!!!!"); 13 var timeList = result.data; 14 console.log(timeList); 15 wx.setStorageSync("timeList", timeList); 16 this.setData({ 17 reportList: timeList 18 }); 19 }, 20 fail: (err)=>{ 21 console.log("請求失敗!!!!") 22 console.log(err); 23 }, 24 }); 25
26 }, 27
28 // 列表的點擊事件
29 handleItemTap(e){ 30 // console.log(e);
31 const {index} = e.currentTarget.dataset; 32 var currentClickIndex = this.data.reportList[index]; 33 wx.navigateTo({ 34 url: '/pages/drawImage/drawImage?time=' + currentClickIndex, 35 }); 36 } 37
38 })
代碼講解:當一加載這個網頁,就從服務器端獲取我要的事件列表參數 timeList ,並且因為這是個經常用到的參數,也把它存到緩存中。然后設置 wxml 中循環的變量 {{ reportList }} 為獲取到的 timeList. handleItemTap是點擊事件,當點擊列表某一項的時候就跳轉頁面。
(4)report.json
1 {
2 "usingComponents": { 3 "SearchInput":"../../components/SearchInput/SearchInput" 4 }, 5 "navigationBarTitleText": "文檔", 6 "navigationBarTextStyle": "black", 7 "navigationBarBackgroundColor": "#04cfff" 8
9 }
report界面如下:
page3 -- search界面
(1)search.wxml
1 <view class="search_row">
2 <input value="{{inputValue}}" placeholder="請輸入您要搜索的商品" bindinput="handleInput"/>
3 <button bindtap="handleCancel" class="button_search" hidden="{{!isFocus}}">取消</button>
4 </view>
5
6 <view class="search_content">
7 <navigator 8 class="search_item"
9 wx:for="{{showList}}"
10 wx:key="index"
11 url="/pages/drawImage/drawImage?time={{item}}"
12 >
13 {{item}} 14 </navigator>
15 </view>
代碼解說:serach_row 存放搜索框和按鍵,search_content 用來顯示搜索的結果。並且用 navigator 標簽,表示點擊某一行的時候則跳轉界面,這里,跳轉界面的時候我傳遞了 time 參數,而 time 的值就等於 item 。
(2)search.wxss
1 page {
2 box-sizing: border-box;
3 background-color: #fff;
4 padding: 20rpx;
5 }
6 .search_row {
7 height: 80rpx;
8 display: flex;
9 }
10 .search_row input {
11 flex: 1;
12 height: 100%;
13 padding-left: 30rpx;
14 border-color: var(--themeColor);
15 border-style: solid;
16 }
17 .search_row .button_search {
18 width: 110rpx;
19 height: 100%;
20 padding: 0;
21 margin-left: 20rpx;
22 display: flex;
23 justify-content: center;
24 align-items: center;
25 font-size: 26rpx;
26 color: #fff;
27 background-color: var(--themeColor);
28 }
29 .search_content {
30 margin-top: 30rpx;
31 }
32 .search_content .search_item {
33 background-color: #ecf0f1;
34 font-size: 26rpx;
35 padding: 20rpx 20rpx;
36 border-bottom: 1rpx solid #95a5a6;
37 overflow: hidden;
38 white-space: nowrap;
39 text-overflow: ellipsis;
40 font-size: 28rpx;
41 }
(3)search.js
1 // 輸入框的值該改變 就會觸發的事件
2 handleInput(e){ 3 // 3 准備發送請求獲取數據
4 let timeList =wx.getStorageSync("timeList"); 5 console.log("timeList = ", timeList); 6
7 // 數據不太多的時候,可以不用防抖體驗更好
8 // clearTimeout(this.TimeId);
9 // this.TimeId=setTimeout(()=>{
10 // 1 獲取輸入框的值 解構
11 var value = e.detail.value; 12
13 // 2 檢驗合法性(判斷是不是全是空格)
14 if (!value.trim()){ 15 // 當值為空時則清楚內容並且隱藏按鍵
16 this.setData({ 17 showList: [], 18 isFocus: false
19 }) 20 // 值不合法
21 return; 22 } 23 this.setData({ 24 isFocus: true
25 }) 26 var valueLowerCase = value.toLowerCase(); 27 var showListData = []; 28 // 搜索功能的實現就是遍歷每個數據看是否包含搜索的內容,有的話則顯示
29 for (var i=0; i<timeList.length; i++) { 30 if (timeList[i].toLowerCase().indexOf(valueLowerCase)>=0) { 31 showListData.push(timeList[i]); 32 } 33 } 34 this.setData({ 35 showList: showListData 36 }); 37
38 // }, 1000);
39 }, 40 // 點擊取消則清空內容並且隱藏按鍵
41 handleCancel(){ 42 this.setData({ 43 inputValue: "", 44 isFocus: false, 45 showList: [] 46 }) 47 } 48 })
Search結果圖: