【详细】小程序搜索功能实现

news/2024/7/20 2:34:53 标签: 小程序

搜索功能是app,小程序中非常常见的一个需求,作为常见的需求,如何高效地完成该需求的实现,就异常关键。那么小程序中如何从0到1实现搜索功能呢?请看本文

效果展示

历史记录

跳转到详情页面(详情页面开发链接https://blog.csdn.net/m0_37218227/article/details/107297203 )

 

 

搜索页wxml

<searchbar bindsearchinput="onSearchInputEvent"></searchbar>

<view class='history-list-group' wx:if="{{histories && !subjects}}">
  <view class='history-title'>
    <view class='title'>历史记录</view>
    <view class='clear' bindtap="onClearEvent">清除</view>
  </view>
  <navigator wx:for="{{histories}}" wx:key="{{item.id}}" url='/pages/detail/detail?type=movie&id={{item.id}}' class='history-group'>{{item.title}}</navigator>
</view>

<view class='item-list-group'>
  <view wx:for="{{subjects}}" class='item-group' wx:key="{{item.id}}" bindtap="onItemTapEvent" data-id="{{item.id}}" data-title="{{item.title}}">
    <image src='{{item.pic.normal}}' class='thumbnail'></image>
    <view class='info-group'>
      <view class='title'>{{item.title}}</view>
      <view class='rate-year'>{{item.rating.value}}分/{{item.year}}</view>
    </view>
  </view>
</view>

搜索页wxss

.item-list-group{
  padding: 10rpx 20rpx;
}

.item-list-group .item-group{
  padding: 10rpx 0;
  border-bottom: 1px solid #e4e4e4;
  display: flex;
}

.item-group .thumbnail{
  width: 80rpx;
  height: 100rpx;
  margin-right: 20rpx;
}

.item-group .info-group{
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.info-group .title{
  font-size: 32rpx;
}

.info-group .rate-year{
  font-size: 28rpx;
  color: #7b7b7b;
}

.history-list-group{
  padding: 10rpx 20rpx;
}

.history-list-group .history-title{
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0;
  background: #f9f9f9;
  font-size: 28rpx;
  color: #9e9e9e;
}

.history-list-group .history-group{
  font-size: 32rpx;
  padding: 20rpx 0;
  border-bottom: 1px solid #e4e4e4;
}

js文件

// pages/search/search.js
import {network} from "../../utils/network.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'searched',
      success: function(res) {
        var data = res.data;
        that.setData({
          histories: data
        })
      },
    })
  },

  onSearchInputEvent: function(event){
    var that = this;
    var value = event.detail.value;
    if(!value || value === ""){
      that.setData({
        subjects: null
      });
      return;
    }
    network.getSearch({
      q: value,
      success: function(subjects){
        that.setData({
          subjects: subjects
        })
      }
    })
  },

  onItemTapEvent: function(event){
    var that = this;
    var id = event.currentTarget.dataset.id;
    var title = event.currentTarget.dataset.title;
    var histories = that.data.histories;
    var isExisted = false;
    if(histories){
      for(var index=0;index<=histories.length;index++){
        var movie = histories[index];
        if(movie.id === id){
          isExisted = true;
          break;
        }
      }
    }
    if(!isExisted){
      if(!histories){
        histories = [];
      }
      histories.push({title:title,id:id});
      wx.setStorage({
        key: 'searched',
        data: histories,
        success: function () {
          console.log("保存成功!");
        }
      })
    }
    
    wx.navigateTo({
      url: "/pages/detail/detail?type=movie&id="+id,
    });
  },

  onClearEvent: function(event){
    wx.removeStorage({
      key: 'searched',
      success: function(res) {
        console.log("删除成功!");
      },
    });
    this.setData({
      histories: null
    });
  }
})

 

 


http://www.niftyadmin.cn/n/855464.html

相关文章

【详细】VSCode插件离线安装方法

简介 VSCode是一款非常常用的编辑器软件&#xff0c;在VSCode社区中有很多巧妙的插件&#xff0c;但有时候&#xff0c;我们开发环境不能上网&#xff0c;为了方便办公&#xff0c;又不得不需要这类插件的安装&#xff0c;这就涉及到了相关插件的离线安装的问题。 显然&#…

TCP/IP、 IXP/SPX、 NetBEUI、 AppleTalk协议

TCP/IP、 IXP/SPX、 NetBEUI、 AppleTalk的认识: TCP/IP协议&#xff0c;或称为TCP/IP协议栈&#xff0c;或互联网协议系列。 它包含了一系列构成互联网基础的网络协议。这些协议最早发源于美国国防部的DARPA互联网项目。TCP/IP字面上代表了两个协议: TCP传输控制协议和IP互联网…

【超详细】数据库事务隔离探究

前言 提到事务&#xff0c;你肯定不陌生&#xff0c;和数据库打交道的时候&#xff0c;我们总是会用到事务。最经典的例子就是转账&#xff0c;你要给朋友小王转100块钱&#xff0c;而此时你的银行卡只有100块钱。 转账过程具体到程序里会有一系列的操作&#xff0c;比如查询…

震后首次漫步锦里有感

震后首次漫步锦里有感 ——代腾飞 2008年6月27日 于成都 饭后漫步锦里边 锦城宫外回震前 昔日佳景依旧在 只是多了几分闲

【详细】IntelliJ IDEA: 无法创建Java Class文件

有时候IDEA新建一个项目&#xff0c;可能导致编辑器不能直接创建java文件&#xff0c;见下图&#xff0c;于是我尝试了以下方法&#xff1a; &#xff08;1&#xff09;选择 File——>Project Structure——>Project Settings——>Modules 如图新项目&#xff0c;new…

【详细】Win10 安装MySQL 5.7 详细教程

由于项目需要MySQL5.7 版本的数据库&#xff0c;翻了一圈网上基本上没啥靠谱的安装5.7 MySQL的教程&#xff08;老帖子下载链接都有问题&#xff09;&#xff0c;基本上以MySQL 8 为主&#xff0c;因此决定自己写一篇关于MySQL5.7版本的安装教程&#xff0c;特此记录学习 第一…

【详细】Java 判断一个字符串是否为合法ip

判断字符串是否为合法ip几乎是每年校招面试必考的内容 下面我就用Java代码来解析一下这个考点 /*** FILENAME : Judge* Author : HangLi* Data : 2020/10/27 9:03* Description : judge ip**/ public class Judge {public static void main(String[] args){// 定义正则表达式…

【详细】手把手教你安装配置Grafana环境

随着业务的越发复杂&#xff0c;对软件系统的要求越来越高&#xff0c;这意味着我们需要随时掌控系统的运行情况。因此&#xff0c;对系统的实时监控以及可视化展示&#xff0c;就成了基础架构的必须能力。 这篇博客&#xff0c;介绍下开源的可视化套件grafana的安装及其功能特…