码农知识堂 - 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. })

  • 相关阅读:
    冰冰学习笔记:平衡二叉搜索树
    通过Pyecharts绘制可视化地球竟 然如此简单
    蓝桥每日一题(day 3: 蓝桥587.约数个数)--数学--easy
    Spring学习|使用Spring实现AOP,三种方式:原生Spring API接口、自定义类、注解方式
    高防服务器是怎样进行防御的?
    AQS(AbstractQueuedSynchronizer)框架之ReentrantLock
    使用 Realitykit 和 Swift 构建受 iOS 16 锁屏启发的 AR 体验,使用 ARKit 的人物分割将 AR 内容置于用户身后
    #分库分表-分片算法
    Java设计模式之适配器模式
    科普一下:拍抖音需要什么设备,可能用到的设备合集
  • 原文地址:https://blog.csdn.net/weixin_46001736/article/details/134381482
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号