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

效果

核心代码

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

let result = array.filter(item => {

return item.name.includes(query);

});

完整代码

wxml

html 复制代码
<input type="text" placeholder="请输入名称" placeholder-style="color:black" bindconfirm="search" />
<view class="all">
  <view class="item_all" wx:for="{{info}}" wx:key="index">
    <view class='position'>
      <view class="content">
        <view class="vv_1">序号:{{item.id}}</view>
        <view class="vv_1">名称:{{item.name}}</view>
        <view class="vv_1">年龄:{{item.age}}</view>
      </view>
    </view>
  </view>
</view>

wxss

css 复制代码
/* 搜索框 */
input {
  background-color: rgb(212, 212, 212);
  padding: 2%;
  margin-bottom: 5%;
}

/* 列表 */
.all {
  margin-bottom: 20%;
}

.item_all {
  /* border: 1px solid black; */
  margin-bottom: 3%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.position {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 95%;
  border-radius: 10px;
  background-color: #fff;
  box-shadow: 2px 2px 2px gainsboro;
}

.content {
  padding: 5%;
}

.vv_1 {
  word-break: break-all;
  padding: 2px 0;
}

js

javascript 复制代码
Page({
  data: {
    //完整数据
    fixed_info: [{
      id:1,
      name:'张三',
      age:23
    }, {
      id:2,
      name:'李四',
      age:26
    }, {
      id:3,
      name:'王五',
      age:24
    }, {
      id:4,
      name:'张晓',
      age:21
    }], 
    //展示数据
    info:[],
  },
  //刚进入页面执行的操作
  onLoad(options) {
    this.setData({
      info:this.data.fixed_info
    })  
  },
  //搜索框回车事件
  search(event) {
    //始终保持查询的数据是完整的数组数据
    this.setData({
      info:this.data.fixed_info
    })
    let query = event.detail.value; // 要查询的关键词
    let array = this.data.info;//设置查询的数组
    let result = array.filter(item => {
      return item.name.includes(query);
    });
    this.setData({
      info:result
    })
  },
})
相关推荐
东方小月6 小时前
从零开发一个 Coding Agent(四):使用状态机校验大模型事件流
前端·人工智能·后端
Csvn7 小时前
🧩 ESM vs CJS 混用的 7 个「天坑」——从 TypeScript 编译到 Node 与浏览器
前端
Csvn7 小时前
🎯 Web 性能 API 集合:Performance Observer 的 5 个冷门妙用
前端
Csvn7 小时前
深入 React 闭包陷阱:从根源上理解并根治 stale closure
前端
whyfail7 小时前
前端学 Spring Boot(8):接口为什么越用越慢?
前端·spring boot·后端
用户059540174468 小时前
LangChain 记忆测试踩坑实录:这两个坑让我排查了 4 小时
前端·css
程序员黑豆8 小时前
鸿蒙应用开发:@Monitor 装饰器使用教程
前端·harmonyos
SamChan908 小时前
在Web应用中集成PDF多语言翻译功能:PDFTranslator API实战指南
前端·python·ai·pdf·yapi·机器翻译
kyriewen9 小时前
AI Agent 9秒删光了生产数据库——我给自己的项目做了5个紧急检查
前端·ai编程·claude
IT_陈寒9 小时前
JavaScript的this又双叒叕让我怀疑人生了
前端·人工智能·后端