微信小程序搜索框代码组件
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: {
},
/**
* 组件的初始数据
*/
data: {
words: [],
hots: [],
books:[],
isSearch:false,
//给输入的默认值
value:""
},
/**
* 组件的方法列表
*/
methods: {
onConfirm(event) {
let value = event.detail.value;
// 只有在服务器上能搜索到的关键字才添加到缓存中
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
}
})
}
// 获取搜索结果
getBookSearch(start,value){
return this.request({
url:`/book/search`,
data:{
start,
q:value
}
})
}
}
export {BookModel};
若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理。
请点赞!因为你们的赞同/鼓励是我写作的最大动力!
欢迎关注达叔小生的简书!
这是一个有质量,有态度的博客

微信小程序搜索框代码组件的更多相关文章
- 微信小程序 —搜索框
wxSearch优雅的微信小程序搜索框 一.功能 支持自定义热门key 支持搜索历史 支持搜索建议 支持搜索历史(记录)缓存 二.使用 1.将wxSearch文件夹整个拷贝到根目录下 2.引入 // ...
- 微信小程序----搜索框input回车搜索事件
在微信小程序里的搜索框,按软键盘回车键触发搜索事件. <input type="text" placeholder="搜索" value="{ ...
- 微信小程序--搜索关键词高亮
代码地址如下:http://www.demodashi.com/demo/14249.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
- [小程序开发] 微信小程序audio音频播放组件+api_wx.createAudioContext
引言: audio是微信小程序中的音频组件,可以轻松实现小程序中播放/停止音频等自定义动作. 附上微信小程序audio组件的相关属性说明:https://mp.weixin.qq.com/debug/ ...
- 微信小程序页面调用自定义组件内的事件
微信小程序页面调用自定义组件内的事件 page page.json { "usingComponents": { "my-component": ". ...
- 微信小程序(二十)-UI组件(Vant Weapp)-01按装配置
1.官网 https://vant-contrib.gitee.io/vant-weapp/#/intro https://gitee.com/vant-contrib/vant-weapp 2.按装 ...
- 微信小程序富文本渲染组件html2wxml及html2wxml代码块格式化在ios下字体过大问题
1.组件使用: 之前微信小程序的富文本渲染组件用的wxParse,对普通富文本确实可以,但是对于代码格式pre标签则无法使用. 下面这个html2wxml很不错,可以支持代码高亮. 详细文档:http ...
- 微信小程序开发—快速掌握组件及API的方法
微信小程序框架为开发者提供了一系列的组件和API接口. 组件主要完成小程序的视图部分,例如文字.图片显示.API主要完成逻辑功能,例如网络请求.数据存储.音视频播放控制,以及微信开放的微信登录.微信支 ...
- 微信小程序开发—快速掌握组件及API的方法---转载
微信小程序框架为开发者提供了一系列的组件和API接口. 组件主要完成小程序的视图部分,例如文字.图片显示.API主要完成逻辑功能,例如网络请求.数据存储.音视频播放控制,以及微信开放的微信登录.微信支 ...
随机推荐
- AutoFac的简单使用教程
Autofac可以对代码进行依赖注入,实现控制反转.以下是本菜鸟在初次入门时的代码配置,其源码,内部原理都还有待日后研究.目前也只是仅仅做到了能够使项目正常使用而已. 跟我一样刚刚入门的菜鸟朋友们可以 ...
- docker实战 (3) 常规配置
本节会持续更新,在项目实战中遇到的docker配置都会更新进来 docker常用命令: docker 介绍: what: 是什么 why: 为什么用 how: 怎么用 docker 特点: 轻量级,可 ...
- Maven整合eclipse
1.配置eclipse本地Maven 点击Window-->Perference 选择Maven-->Installations 点击Add添加本地Maven 然后勾选本地Maven, ...
- Ubuntu安装Java环境经历
1.权限不够 sudo su gedit /etc/sudoers 添加 用户名 ALL=(ALL:ALL) ALL 2.配置java 放到 /usr/lib/jvm/下 sudo gedit /et ...
- maven学习笔记五(仓库搭建,私服配置)
实际项目中,我们往往都是多人开发,这个时候,假如一个项目有300多M.用的jar包有100多个.只要项目组来一个人就从中央仓库下载依赖的jar,这种下载一般都需要持续很久.而且中央仓库一般都是配置在外 ...
- c++ 初始化静态static成员变量或static复合成员变量
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c https://sta ...
- MySQL/MariaDB数据库的多表查询操作
MySQL/MariaDB数据库的多表查询操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.单表查询小试牛刀 [root@node105.yinzhengjie.org.cn ...
- 监控服务zabbix部署
目录 1. zabbix介绍 2. zabbix特点 3. zabbix配置文件 4. 部署zabbix 4.1 zabbix服务端安装 4.2 zabbix服务端配置 4.3 zabbix服务端we ...
- C++(五十一) — 容器中常见算法(查找、排序、拷贝替换)
1.find(); find()算法的作用是在指定的一段序列中查找某个数,包含三个参数,前两个参数是表示元素范围的迭代器,第三个参数是要查找的值. 例:fing(vec.begin(), vec.en ...
- ArcGIS pro 发布地图服务(一)动态地图服务
1.软件:arcgis pro 2.4 数据:.mxd文档. 2.导入mxd文档. 3.登录portal账号 4.分析—发布 5.在server中的地图服务 JavaScript api 查看 6. ...