微信小程序:历史搜索及根据关键词列表查询

在这里插入图片描述
searchValue 是搜索内容,searchList 是存放搜索内容的数组,通过使用 wx.setStorageSync('searchList', searchList)在本地存储一个名为searchList(ps:也可以起个其他的名字,看您心情)的变量,然后在onload函数里面调用wx.getStorageSync('searchList'),并将值赋给data中的searchList,这样每次进入搜索页面时都会显示之前的搜索内容,也就是历史记录了
接下来就是对历史纪录的一些优化
1.搜索的内容和之前的搜索内容相同时,禁止存储到本地
2.优先展示最新的搜索内容
3.对展示的历史记录数量进行限制
4.历史记录可以一次性删除所有,也可以单个删除
5.点击历史记录,将历史记录显示在搜索框
6.点击搜索时,隐藏历史记录,显示符合条件的数据列表,并点击数据可以调转到对应的详情

search.js

// pages/job/search/search.js
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '',
    searchList: [],
    isShow: true, //控制标签的是否可删除
    isTag: true, //控制标签的显示与隐藏
    isBtn: true, //控制删除按钮的显示与隐藏
    list: [], //查询列表
    showList: false,
  },
  //点击tag标签删除
  tagClick(e) {
    var a = e.currentTarget.dataset.index
    let searchList = this.data.searchList;
    searchList.splice(a, 1);
    this.setData({
      searchList: searchList
    })
    wx.setStorageSync('searchList', searchList)
  },
  giveInput(e) {
    let newIndex = e.currentTarget.dataset.index;
    let newSearch = this.data.searchList[newIndex];
    this.setData({
      searchValue: newSearch
    })
  },
  onChange(e) {
    this.setData({
      searchValue: e.detail
    })
  },
  //搜索
  onSearch() {
    let page = this;
    var inputValue = this.data.searchValue;
    var searchList = this.data.searchList || [];
    if (inputValue !== "") {
      //示例通用接口 获取列表信息
      wx.request({
        url: '此处省略哈/tag/getlist',//列表接口
        data: {
          table: 'job_position', //表名
          keywords: inputValue,
          fields: 'name'
        },
        method: 'GET',
        success(res) {
          console.log(res.data.data);
          let returnData = res.data.data;
          page.setData({
            list: returnData,
          })
          if (returnData.length > 0) {
            page.setData({
              showList: true,
            })
          } else {
            page.setData({
              showList: false,
            })
          }
        }
      })
      //若是输入的值不存在数组中则添加
      if (!searchList.includes(inputValue)) {
        //将搜索值放入历史记录中,只能放前五条
        if (searchList.length < 5) {
          searchList.unshift(inputValue)
        } else {
          searchList.pop() //删掉旧的时间最早的第一条
          searchList.unshift(inputValue)
        }
        //将历史记录数组整体储存到缓存中
        wx.setStorageSync('searchList', searchList)
      }
    } else {
      wx.showToast({
        title: '请输入搜索内容',
        icon: 'error',
        duration: 1500,
      })
      page.setData({
        showList: false,
      })
    }
  },
  //前往职位详情页面
  toJobDetail: util.throttle(function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/job/jobDetail/jobDetail?id="+id,
    })
  }, 1000),
  // 删除图标
  searchDelete() {
    this.setData({
      isShow: false,
      isBtn: false
    })
  },
  //完成按钮
  finishClick() {
    this.setData({
      isShow: true,
      isBtn: true
    })
  },
  //全部删除按钮
  deleteAll() {
    wx.clearStorageSync('searchList')
    this.setData({
      searchList: []
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let localData = wx.getStorageSync('searchList');
    this.setData({
      searchList: localData
    })
  },
})

search.html

<view>
  <van-search background="rgb(49, 121, 255)" value="{{searchValue}}" bind:change="onChange" shape="round"
    placeholder="输入职位 / 公司 / 标签" bind:search="onSearch" use-action-slot>
    <view slot="action" bind:tap="onSearch" class="search_color">搜索</view>
  </van-search>
  <!-- 查询列表 -->
  <view wx:for="{{list}}" wx:key="index" wx:if="{{showList}}" class="margin_top">
    <view class="job_list" bindtap="toJobDetail" data-id="{{item.id}}">
      <view class="job_list_top">
        <view class="job_list_top_left">
          <image class="job_list_top_left_image" src="{{item.logo}}"></image>
        </view>
        <view class="job_list_top_right">
          <view class="job_flex job_list_top_right_out">
            <view class="job_list_top_right_job">{{item.jobName}}</view>
            <!-- <view class="job_list_top_right_zhaopin">{{item.jobFair}}</view> -->
            <view></view>
          </view>
          <view class="job_flex job_list_top_center">
            <view class="job_list_top_right_name">{{item.name}}</view>
            <view class="job_list_top_right_xinzi">{{item.min}}-{{item.max}}</view>
          </view>
          <view class="job_list_top_bottom job_list_top_right_name"><text>{{item.area}}</text> | <text>{{item.education}}</text> | <text>{{item.experience}}</text></view>
        </view>
      </view>
      <view class="job_list_footer job_flex">
        <view class="job_list_footer_time">
           <text>面试时间:</text><text class="job_list_footer_time_color">{{item.face}}</text>
           <!-- <text>面试时间:</text><text class="job_list_footer_time_color">短信通知</text> -->
        </view>
        <view>已申请 <text class="job_list_top_right_xinzi">{{item.applyNum}}</text> </view>
        <!-- 若是申请后,则显示 -->
        <!-- <view class="job_list_footer_applied">已申请</view> -->
      </view>
    </view>
  </view>
  <view class="search_history" wx:if="{{searchList.length>0 && !showList}}">
    <view>历史搜索</view>
    <view wx:if="{{isBtn}}">
      <van-icon name="delete-o" size="45rpx" bindtap="searchDelete" />
    </view>
    <view wx:else class="search_btn"><text bindtap="deleteAll">全部删除 </text>|<text class="search_btn_active" bindtap="finishClick"> 完成</text></view>
  </view>
  <view class="search_list" wx:for="{{searchList}}" wx:key="index">
    <van-tag class="search_list_tag" size="large" plain wx:if="{{isShow}}" bindtap="giveInput" data-index="{{index}}">{{item}}</van-tag>
    <van-tag class="search_list_tag" size="large" closeable wx:else  plain  bindtap="tagClick" data-index="{{index}}">{{item}}</van-tag>
  </view>
</view>

search.wxss

page {
  background-color: rgba(204, 204, 204, 0.596078431372549);
}
.search_color{
  color: #fff;
}
.search_history {
  display: flex;
  justify-content: space-between;
  margin: 20rpx;
}

.search_list {
  margin: 20rpx;
}

.search_list_tag {
  float: left;
  margin: 10rpx;
}
.search_btn{
  font-size: 13px;
}
.search_btn_active{
  color:rgb(49, 121, 255)
}


.margin_top{
  margin-top:10px;
}
.job_list {
  margin: 0 auto;
  box-sizing: border-box;
  margin-bottom: 20rpx;
  width: calc(100% - 20rpx);
  /* border-radius: 10rpx; */
  background-color: #fff;
  height: 280rpx;
  padding-left: 10rpx;
  padding-right: 10rpx;
  box-sizing: border-box;
}

.job_list_top {
  display: flex;
  justify-content: space-between;
}

.job_list_top_left_image {
  width: 140rpx;
  height: 140rpx;
}

.job_list_top_right {
  width: 560rpx;
}

.job_flex {
  display: flex;
  justify-content: space-between;
}

.job_list_top_right_out {
  margin-top: 20rpx;
}

.job_list_top_right_job {
  font-size: 34rpx;
  height: 48rpx;
  line-height: 48rpx;
  width: 440rpx;
  Text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.job_list_top_right_zhaopin {
  font-size: 24rpx;
  text-align: center;
  height: 48rpx;
  line-height: 48rpx;
  color: #429EFD;
  border: 2rpx solid #429EFD;
  width: 90rpx;
}

.job_list_top_right_name {
  font-size: 28rpx;
}

.job_list_top_right_xinzi {
  color: #429EFD;
}

.job_list_top_center {
  margin-top: 20rpx;
}

.job_list_top_bottom {
  margin-top: 10rpx;
  color: rgba(51, 51, 51, 0.690196078431373);
}

.job_list_footer {
  margin-top: 20rpx;
  font-size: 28rpx;
  color: rgba(51, 51, 51, 0.690196078431373);
}

.job_list_footer_time_color {
  color: #FFA535;
}

.job_list_footer_applied {
  color: #fff;
  background-color: rgba(153, 153, 153, 1);
  width: 132rpx;
  height: 52rpx;
  border-radius: 30rpx;
  text-align: center;
  line-height: 52rpx;
}

search.json

{
  "usingComponents": {
    "van-search": "@vant/weapp/search/index",
    "van-icon":"@vant/weapp/icon/index",
    "van-tag":"@vant/weapp/tag/index"
  },
  "navigationBarTitleText": "搜索"
}

util.js

//节流,防止短时间内点击多次导致页面跳转多次
const throttle = (fn, delay) => {
  let oldDate = Date.now();
  return function () {
    let args = arguments;
    let newDate = Date.now();
    let that = this;
    if (newDate - oldDate > delay) {
      fn.apply(that, args);
      //倘若时间差大于延长时间 就更新一次旧时间
      oldDate = Date.now();
    }
  }
}

module.exports = {
  throttle: throttle,
}
  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coderdwy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值