微信小程序 搜索框組件代碼實(shí)例
這篇文章主要介紹了微信小程序 搜索框組件代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
代碼如下
search.wxml
<view class="header"> <view class="search"> <icon type="search" size="18" color=""> </icon> <input type="text" confirm-type="search" bindconfirm="onConfirm" value="{{value}}" /> <icon type="clear" size="18" bind:tap="onToggle" /> </view> <button bind:tap="onCancel" plain="{{true}}" class="cancel">取消</button> </view> <view class="container" wx:if="{{!isSearch}}"> <view class="title"> <view class="line"></view> <text>歷史搜索</text> </view> <view class="history-container"> <block wx:for="{{words}}" wx:key="{{index}}"> <v-tag content="{{item}}" bind:comment="onConfirm"></v-tag> </block> </view> <view class="title"> <view class="line"></view> <text>熱門搜索</text> </view> <view class="history-container"> <block wx:for="{{hots}}" wx:key="{{index}}"> <v-tag content="{{item}}" bind:comment="onConfirm"></v-tag> </block> </view> </view> <view class="result" wx:if="{{isSearch}}" > <block wx:for="{{books}}" wx:key="index"> <v-book book="{{item}}"></v-book> </block> </view>
search.wxss
.header{ position: fixed; top:0; left: 0; z-index: 300; height:100rpx; display: flex; padding-left:20rpx; padding-right:20rpx; align-items: center; border-top: 1rpx solid #eee; border-bottom: 1rpx solid #eee; flex-direction: row; background: #fff; } .search{ width:530rpx; height:70rpx; background: rgb(245, 245, 245); border-radius:30rpx; padding-left: 20rpx; display: flex; align-items: center; } .search input{ flex:1; margin-left: 20rpx; } .cancel{ height:70rpx; border-radius: 30rpx; line-height: 70rpx; border-color: #888; } .container{ margin-top: 100rpx; padding: 20rpx; } .title{ display: flex; height:90rpx; align-items: center; } .line{ height:40rpx; width:10rpx; background: #333; } .result{ margin-top: 100rpx; padding-left:90rpx; padding-right:90rpx; display: flex; flex-wrap: wrap; justify-content: space-between; } v-book{ margin-bottom: 60rpx; }
search.js
// components/search/search.js import { Keyword } from "../../models/keyword"; import { BookModel } from "../../models/book"; const keyword = new Keyword(); const bookModel = new BookModel(); Component({ /** * 組件的屬性列表 */ properties: { }, /** * 組件的初始數(shù)據(jù) */ data: { words: [], hots: [], books:[], isSearch:false, //給輸入的默認(rèn)值 value:"" }, /** * 組件的方法列表 */ methods: { onConfirm(event) { let value = event.detail.value; // 只有在服務(wù)器上能搜索到的關(guān)鍵字才添加到緩存中 bookModel.getBookSearch(0, value).then(res => { if (res.total) { keyword.addHistory(value); let words = keyword.getHistory(); this.setData({ words, books:res.books, isSearch:true }) }// console.log(res); }) }, onToggle() { this.setData({ value: "", isSearch:false }) }, onCancel() { this.setData({ isSearch: false }) } }, attached() { // keyword.getHistory(); this.setData({ words: keyword.getHistory() }) keyword.getHotData().then(res => { // console.log(res.hot); this.setData({ hots: res.hot }) }) } })
models/keyword
import {HTTP} from "../utils/http-p"; class Keyword extends HTTP{ getHistory(){ const words = wx.getStorageSync('q') if(words){ return words }else{ return []; } } addHistory(value){ var words = this.getHistory(); const has = words.includes(value); if(value && !has){ if(words.length>4){ words.pop() } words.unshift(value); wx.setStorageSync('q', words) } } getHotData(){ return this.request({ url:`/book/hot_keyword` }) } getKeyword(start,value){ return this.request({ url:`/book/search`, data:{ start, q:value } }) } } export {Keyword}
models/book
import {HTTP} from "../utils/http-p"; class BookModel extends HTTP{ getHotBook(){ return this.request({ url:"/book/hot_list" }) } getBookDateil(id){ return this.request({ url:`/book/${id}/detail` }) } getBookComment(id){ return this.request({ url:`/book/${id}/short_comment` }) } getBookLike(id){ return this.request({ url:`/book/${id}/favor` }) } // 新增短評 addNewComment(id,content){ return this.request({ url:`/book/add/short_comment`, method:"POST", data:{ book_id:id, content } }) } // 獲取搜索結(jié)果 getBookSearch(start,value){ return this.request({ url:`/book/search`, data:{ start, q:value } }) } } export {BookModel};
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS回調(diào)函數(shù)原理與用法詳解【附PHP回調(diào)函數(shù)】
這篇文章主要介紹了JS回調(diào)函數(shù)原理與用法,結(jié)合實(shí)例形式詳細(xì)分析了JavaScript回調(diào)函數(shù)的概念、原理、用法,并給出了PHP回調(diào)函數(shù)的使用示例,需要的朋友可以參考下2019-07-07javascript設(shè)計(jì)模式--策略模式之輸入驗(yàn)證
策略模式中的策略就是一種算法或者業(yè)務(wù)規(guī)則,將這些策略作為函數(shù)進(jìn)行封裝,并向外提供統(tǒng)一的調(diào)用執(zhí)行,本文給大家介紹javascript設(shè)計(jì)模式--策略模式之輸入驗(yàn)證,需要的朋友參考下2015-11-11VS2008中使用JavaScript調(diào)用WebServices
這篇文章主要介紹了VS2008中使用JavaScript調(diào)用WebServices,需要的朋友可以參考下2014-12-12uni-app使用uni-download和uni.saveFile下載保存文件遇到的問題及解決方法
這篇文章主要給大家介紹了關(guān)于uni-app使用uni-download和uni.saveFile下載保存文件遇到的問題及解決方法的相關(guān)資料,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2024-01-01javascript getBoundingClientRect() 來獲取頁面元素的位置的代碼[修正版]
該方法已經(jīng)不再是IE Only了,F(xiàn)F3.0+和Opera9.5+已經(jīng)支持了該方法,可以說在獲得頁面元素位置上效率能有很大的提高,在以前版本的Opera和Firefox中必須通過循環(huán)來獲得元素在頁面中的絕對位置。2009-05-05