微信小程序 搜索框实现模糊搜索(带模拟数据,js,wxml,wxss齐全

简介: 微信小程序 搜索框实现模糊搜索(带模拟数据,js,wxml,wxss齐全

最近在做一个小程序的页面,搜索框困扰了我很久,今天终于把搜索框给做了出来,记录一下过程

我主要使用的就是wx的if,当我输入框用户点击的时候,我前面的显示界面添加上false属性,然后我搜索页面显示出true的属性,至于模糊搜索,我是直接使用的js的匹配,话不多说,直接看成果和代码

代码

wxml

<view style="display: flex; align-items: center;background-color: rgb(71, 189, 254); margin-top: 200rpx;">
    <input placeholder="请输入关键词" focus="{{inputFocus}}" bindfocus="onFocus" bindblur="onBlur" style="border: 1px solid #ccc; padding: 10rpx; border-radius: 10rpx; flex: 1;background-color: rgb(255, 255, 255);" />
    <button wx:if="{{showSearch}}"
      bindtap="quxiao"
      style="margin-left: 10rpx; padding: 5rpx 10rpx;  color: #fff; border-radius: 5rpx; width: 140rpx;background-color: rgb(71, 189, 254);"
    >
      取消
    </button>
  </view>
  <view class="another-container">
    <view wx:if="{{searchResult.length > 0}}">
      <!-- 显示搜索结果列表 -->
      <view class="search-list">
        <view wx:if="{{showSearch && searchResult.length > 0}}">
          <!-- 显示搜索结果列表 -->
          <view class="search-list">
            <view class="article-item" wx:for="{{searchResult}}" wx:key="index" bindtap="onArticleTap" data-articleid="{{item.articleid}}">
              <image class="article-image" src="{{item.image}}"></image>
              <view class="article-content">
                <text class="article-title">{{item.title}}</text>
                <view class="article-time">{{item.releasetime}}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view wx:else>
      <!-- 当搜索结果为空时显示暂无搜索结果 -->
      <view class="no-result">暂无搜索结果</view>
    </view>
  </view>

js

Page({
  /**
   * 页面的初始数据
   */
  data: {
    showSearch: false,
    inputFocus: false,
    dataArray:[
      {
        "articleid": "3490400c-9988-4b57-8f8a-92124cb9bef3",
        "title": "喝酒",
        "releasetime": "2024-2-17",
        "image": "https://pic.imgdb.cn/item/65d04d2a9f345e8d0321731d.jpg",
        "text": "嘿嘿",
        "teacherid": null
      },
      {
        "articleid": "40b3e53f-d2c6-4f11-b1f7-965f6ce54800",
        "title": "检查作业呜呜呜",
        "releasetime": "2024-2-17",
        "image": "https://pic.imgdb.cn/item/65d0312b9f345e8d03cb438a.jpg",
        "text": "好难",
        "teacherid": null
      },
      {
        "articleid": "db100597-07b5-4470-9ed5-8e69b258dd48",
        "title": "笨蛋去上学了不开心",
        "releasetime": "2024-2-17",
        "image": "https://pic.imgdb.cn/item/65ceff799f345e8d03690b4b.jpg",
        "text": null,
        "teacherid": null
      },
      {
        "articleid": "1b275828-aea7-46f9-9912-0668772d0cd5",
        "title": "送的手链好好看❤",
        "releasetime": "2024-2-16",
        "image": "https://pic.imgdb.cn/item/65ceff939f345e8d03695d7d.jpg",
        "text": null,
        "teacherid": null
      },
      {
        "articleid": "9f9edd79-92d0-4758-bdd0-c5a333c653fe",
        "title": "在家玩switch!",
        "releasetime": "2024-2-16",
        "image": "https://pic.imgdb.cn/item/65ceffa99f345e8d0369a7b0.jpg",
        "text": "",
        "teacherid": null
      },
      {
        "articleid": "1aac74b2-67a6-40e7-8967-b665f64df05c",
        "title": "在家也会有彩虹!",
        "releasetime": "2024-2-14",
        "image": "https://pic.imgdb.cn/item/65ceffb69f345e8d0369d156.jpg",
        "text": "",
        "teacherid": null
      },
      {
        "articleid": "eda6872f-8154-47d4-98b4-38057e35d41c",
        "title": "2是去看烟花",
        "releasetime": "2024-2-13",
        "image": "https://pic.imgdb.cn/item/65ceff799f345e8d03690a92.jpg",
        "text": "",
        "teacherid": null
      }
    ]
  },
  onFocus: function() {
    this.setData({
      inputFocus: true,
      showSearch: true,
      isshow:false
    });
  },
  onBlur: function(e) {
    this.setData({
      isshow:false,
      showSearch: true,
      searchResult:null,
      Searchtxt:e.detail.value
    })
    var txt=this.data.Searchtxt;
    var dataArray=this.data.dataArray
    console.log("开始搜索"+txt);
    const searchResult = this.fuzzySearch(dataArray, txt);
    console.log(searchResult);
    this.setData({
      searchResult:searchResult
    })
  },
  quxiao: function() {
    this.setData({
      showSearch: false,
    })
  },
  fuzzySearch(arr, searchText) {
      const filteredArray = arr.filter(item => {
      const titleMatch = item.title.toLowerCase().includes(searchText.toLowerCase());
      const timeMatch = item.releasetime.toLowerCase().includes(searchText.toLowerCase());
      const textMatch = item.text && item.text.toLowerCase().includes(searchText.toLowerCase());
      return titleMatch || timeMatch || textMatch;
    });
    return filteredArray;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})

wxss

.search-list{
  margin-top: 20rpx;
}
.article-list {
  width: 80%;
}
.article-item {
  margin-bottom: 20rpx;
  padding: 20rpx;
  background-color: #fff;
  border-radius: 10rpx;
  box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
  display: flex;
}
.article-item:last-child {
  margin-bottom: 0;
}
.article-image {
  width: 120rpx;
  height: 120rpx;
  margin-right: 20rpx;
  border-radius: 10rpx;
}
.article-content {
  flex: 1;
}
.article-title {
  font-size: 32rpx;
  font-weight: bold;
  margin-top: 10rpx;
}
.article-time {
  color: #999;
  font-size: 24rpx;
  margin-top: 10rpx;
}
.article-text {
  font-size: 28rpx;
  margin-top: 10rpx;
}
.no-result {
  text-align: center;
  font-size: 32rpx;
  color: #999;
  margin-top: 20rpx;
}
相关文章
|
小程序 JavaScript 前端开发
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
4271 1
|
存储 安全 小程序
在微信小程序中使用 Vant 时如何确保数据的安全?
在微信小程序中使用 Vant 时如何确保数据的安全?
266 1
|
12月前
|
JavaScript
JS实现多条件搜索函数
JS封装的多条件搜索
|
JavaScript 前端开发 API
JavaScript中通过array.map()实现数据转换、创建派生数组、异步数据流处理、复杂API请求、DOM操作、搜索和过滤等,array.map()的使用详解(附实际应用代码)
array.map()可以用来数据转换、创建派生数组、应用函数、链式调用、异步数据流处理、复杂API请求梳理、提供DOM操作、用来搜索和过滤等,比for好用太多了,主要是写法简单,并且非常直观,并且能提升代码的可读性,也就提升了Long Term代码的可维护性。 只有锻炼思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
|
小程序 JavaScript 开发工具
微信小程序——全局数据共享
【10月更文挑战第5天】
|
缓存 小程序 API
微信小程序页面导航与路由:实现多页面跳转与数据传递
本文深入探讨微信小程序的页面导航与路由机制,介绍多种页面跳转方式如`wx.navigateTo`、`wx.redirectTo`、`wx.switchTab`等,并讲解通过URL、全局变量和事件传递数据的方法。结合案例实现多页面跳转与数据传递,帮助开发者掌握这一重要技能。
|
缓存 小程序 API
微信小程序网络请求与API调用:实现数据交互
本文深入探讨了微信小程序的网络请求与API调用,涵盖`wx.request`的基本用法、常见场景(如获取数据、提交表单、上传和下载文件)及注意事项(如域名配置、HTTPS协议、超时设置和并发限制)。通过一个简单案例,演示了如何实现小程序与服务器的数据交互。掌握这些技能将帮助你构建功能更丰富的应用。
|
人工智能 JavaScript 网络安全
ToB项目身份认证AD集成(三完):利用ldap.js实现与windows AD对接实现用户搜索、认证、密码修改等功能 - 以及针对中文转义问题的补丁方法
本文详细介绍了如何使用 `ldapjs` 库在 Node.js 中实现与 Windows AD 的交互,包括用户搜索、身份验证、密码修改和重置等功能。通过创建 `LdapService` 类,提供了与 AD 服务器通信的完整解决方案,同时解决了中文字段在 LDAP 操作中被转义的问题。
614 1
|
移动开发 小程序 数据可视化
微信小程序可视化开发工具之动态数据
微信小程序可视化开发工具之动态数据
348 4
|
小程序 JavaScript 开发工具
微信小程序——全局数据共享
微信小程序——全局数据共享

热门文章

最新文章