• 微信小程序:仅前端实现对象数组的模糊查询


    效果

     

    核心代码

    //对数组进行过滤,返回数组中每一想满足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. })

  • 相关阅读:
    别再把Tableau、PowerBI吹上天了,在中国根本用不起来,看看为啥
    设计模式之策略模式
    jmeter压测
    Java处理数据成为树状结构
    解析ASEMI代理瑞萨R7S721031VCFP#AA1芯片及其优势
    更轻便使用Siri!iOS 17让你用Siri的效率倍增
    Spark第一课
    云计算与数字化转型的关系,终于有人讲明白了
    2022年11月21日13:32:00——T5——JS对象与Date日期函数
    Python---for循环中的两大关键字break和continue
  • 原文地址:https://blog.csdn.net/weixin_46001736/article/details/134381482