码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 微信小程序:仅前端实现对象数组的模糊查询


    效果

     

    核心代码

    //对数组进行过滤,返回数组中每一想满足name值包括变量query的

    let result = array.filter(item => {

          return item.name.includes(query);

    });

    完整代码

    wxml

    1. <input type="text" placeholder="请输入名称" placeholder-style="color:black" bindconfirm="search" />
    2. <view class="all">
    3. <view class="item_all" wx:for="{{info}}" wx:key="index">
    4. <view class='position'>
    5. <view class="content">
    6. <view class="vv_1">序号:{{item.id}}view>
    7. <view class="vv_1">名称:{{item.name}}view>
    8. <view class="vv_1">年龄:{{item.age}}view>
    9. view>
    10. view>
    11. view>
    12. view>

    wxss

    1. /* 搜索框 */
    2. input {
    3. background-color: rgb(212, 212, 212);
    4. padding: 2%;
    5. margin-bottom: 5%;
    6. }
    7. /* 列表 */
    8. .all {
    9. margin-bottom: 20%;
    10. }
    11. .item_all {
    12. /* border: 1px solid black; */
    13. margin-bottom: 3%;
    14. display: flex;
    15. flex-direction: column;
    16. align-items: center;
    17. justify-content: center;
    18. width: 100%;
    19. }
    20. .position {
    21. display: flex;
    22. flex-direction: column;
    23. justify-content: center;
    24. width: 95%;
    25. border-radius: 10px;
    26. background-color: #fff;
    27. box-shadow: 2px 2px 2px gainsboro;
    28. }
    29. .content {
    30. padding: 5%;
    31. }
    32. .vv_1 {
    33. word-break: break-all;
    34. padding: 2px 0;
    35. }

    js

    1. Page({
    2. data: {
    3. //完整数据
    4. fixed_info: [{
    5. id:1,
    6. name:'张三',
    7. age:23
    8. }, {
    9. id:2,
    10. name:'李四',
    11. age:26
    12. }, {
    13. id:3,
    14. name:'王五',
    15. age:24
    16. }, {
    17. id:4,
    18. name:'张晓',
    19. age:21
    20. }],
    21. //展示数据
    22. info:[],
    23. },
    24. //刚进入页面执行的操作
    25. onLoad(options) {
    26. this.setData({
    27. info:this.data.fixed_info
    28. })
    29. },
    30. //搜索框回车事件
    31. search(event) {
    32. //始终保持查询的数据是完整的数组数据
    33. this.setData({
    34. info:this.data.fixed_info
    35. })
    36. let query = event.detail.value; // 要查询的关键词
    37. let array = this.data.info;//设置查询的数组
    38. let result = array.filter(item => {
    39. return item.name.includes(query);
    40. });
    41. this.setData({
    42. info:result
    43. })
    44. },
    45. })

  • 相关阅读:
    API接口如何接入电商平台获取商品实时数据,通过商品ID获取商品名称,主图,价格,颜色规格尺寸,库存,SKU等案例
    虚拟机的发展史:从分时系统到容器化
    【golang】调度系列之P
    12 医疗挂号系统_【 手机登录】
    DOM
    QT5.14.2 视频分帧:QT与FFmpeg的高效结合
    仿闪照功能娱乐微信小程序源码下载-带外卖CPS功能和流量主
    mathtype嵌入到wps中
    备战数学建模38-粒子群算法pso番外篇1(攻坚战2)
    [NAS] Synology (群晖) DSM7.0 使用自定义供应商DDNS
  • 原文地址:https://blog.csdn.net/weixin_46001736/article/details/134381482
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号