微信小程序:仅前端实现对象数组的模糊查询
微信小程序:仅前端实现对象数组的模糊查询
效果
核心代码
//对数组进行过滤,返回数组中每一想满足name值包括变量query的
let result = array.filter(item => {
return item.name.includes(query);
});
完整代码
wxml
<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
/* 搜索框 */
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
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})},
})
最新文章
- 如何判别判断电脑硬件故障?
- 录像机出现卡顿问题是什么原因呢?
- Git企业开发级讲解(二)
- ChIP在植物领域中的应用
- 【第2章 Node.js基础】2.4 Node.js 全局对象(二) process 对象
- 2013年01月23日 Go生态洞察:使用 go fmt 格式化你的代码 ✨
- 【腾讯云 HAI域探秘】——自行搭建Stable Diffusion模型服务用于生成AI图片
- XSelect清空选中值
- Java Stream 的常用API
- 基于springboot实现小学家校一体“作业帮”系统项目【项目源码】
- Zookeeper篇
- QML16、从 C++ 定义 QML 类型
- ubuntu使用xdebug测试php的性能
- 微信小程序:仅前端实现对象数组的模糊查询
- 【Linux】软硬链接和动静态库
- zookeeper应用之分布式锁
- 安装 Lua 的 HTTP 库