课程章节: 列表排序解决方案与实现热门文章排名功能
主讲老师: Sunday
课程内容:
今天学习的内容包括:
创建文章功能的实现
课程收获:
10.1 心得:
"use strict";
var app = getApp();
Component({
properties: {
historyKey: {
type: String,
value: '',
observer: "onHistoryKeyChange"
},
},
data: {
historyList: [],
isHiddenHistory: false,
searchList: [],
isHiddenTips: true,
},
methods: {
onHistoryKeyChange: function onHistoryKeyChange() {
this.setData({
historyKey: this.data.historyKey,
});
let _this = this;
wx.getStorage({
key: this.data.historyKey,
success(res) {
if (res.data != undefined && res.data != null && res.data.length > 0) {
let historyList = JSON.parse(res.data);
_this.setData({
historyList: historyList.reverse()
});
}
}
})
},
onBindSearchList: function (e) {
this.setData({
searchList: e
});
// console.log(this.data.searchList);
},
/**
* 输入内容变化
* @param {*} e
*/
onSearchInputChange: function onSearchInputChange(e) {
this.setData({
isHiddenHistory: e.detail.content.length > 0 ? true : false,
isHiddenTips: e.detail.content.length > 0 ? false : true
});
if (e.detail.content.length > 0) {
this.triggerEvent("onSearchChange", e.detail.content);
}
},
/**
* 点击搜索
* @param {*} e
*/
onClickSearchSubmit: function onClickSearchSubmit(e) {
this.setData({
isHiddenTips: true,
});
if (e.detail.content == '') {
return;
}
let isAddToHistoryList = true;
this.data.historyList.forEach(function (value, index) {
if (e.detail.content == value) {
isAddToHistoryList = false;
}
});
if (isAddToHistoryList) {
this.data.historyList.push(e.detail.content);
this.setData({
historyList: this.data.historyList.reverse().slice(0, 5),
});
wx.setStorage({
key: this.data.historyKey,
data: JSON.stringify(this.data.historyList.reverse().slice(0, 5))
});
}
this.triggerEvent('onClickSubmit', { content: e.detail.content });
},
/**
* 点击搜索历史item
* @param {*} e
*/
onClickHistoryItem: function onClickHistoryItem(e) {
this.setData({
isHiddenHistory: true,
});
this.searchInput = this.selectComponent("#searchInput");
this.searchInput.onChangeInputValue(e.detail);
this.triggerEvent('onClickSubmit', { content: e.detail });
},
/**
* 点击搜索建议item
* @param {*} e
*/
onClickItem: function onClickItem(e) {
this.setData({
isHiddenTips: true,
});
this.searchInput = this.selectComponent("#searchInput");
this.searchInput.onChangeInputValue(e.currentTarget.dataset.name);
this.data.historyList.push(e.currentTarget.dataset.name);
this.setData({
historyList: this.data.historyList.reverse().slice(0, 5),
});
wx.setStorage({
key: this.data.historyKey,
data: JSON.stringify(this.data.historyList.reverse().slice(0, 5))
});
this.triggerEvent('onClickSubmit', { content: e.currentTarget.dataset.name });
},
/**
* 点击清空历史搜索
* @param {*} e
*/
onClickClearHistory: function onClickClearHistory(e) {
this.setData({
historyList: [],
});
wx.setStorage({
key: this.data.historyKey,
data: JSON.stringify([])
});
},
}
});
.container {
display: flex;
background-color: #fff;
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
flex-direction: column;
}
.search_tips,.tips_item_container{
display: flex;
background-color: #fff;
margin: 0;
padding: 0;
box-sizing: border-box;
}
.search_tips{
flex-direction: column;
}
.tips_item_container{
flex-direction: row;
justify-content: space-between;
align-items: center;
padding: 30rpx 0;
margin: 0 32rpx;
border-bottom: 1rpx solid #F6F6F6;
}
.right_arrow{
width: 15rpx;
height: 25rpx;
flex-shrink: 0;
margin-left: 10rpx;
margin-right: 10rpx;
}
.tips_item_content{
line-height: 34rpx;
font-size: 24rpx;
flex-shrink: 0;
text-align: center;
color: #333333;
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦