arrays - 在多个值离子后过滤
问题描述
我正在一个在线图书馆上做一个项目,我有一个模态页面,允许您通过 3 个输入、标题、作者和年份来搜索书籍。如何通过这 3 个输入过滤书籍?
我的 modal.html:
<ion-content padding>
<ion-list>
<ion-item>
<ion-label color="primary" >Title</ion-label>
<ion-input placeholder="Insert the title" [(ngModel)]="searchTerm" name="titlebook" ></ion-input>
</ion-item>
<ion-item>
<ion-label color="primary">Author</ion-label>
<ion-input placeholder="Insert the Author" name="authorbook" ></ion-input>
</ion-item>
<ion-item>
<ion-label color="primary">Year</ion-label>
<ion-input type="number" placeholder="Insert the Years" name="anno"></ion-input>
</ion-item>
</ion-list>
<ion-buttons text-center>
<button ion-button color="secondary" (click)="searchTitle()">save
</ion-buttons>
我的 json 示例(100 本书):
{
"id":"3",
"author": "Dante Alighieri",
"country": "Italy",
"imageLink": "images/the-divine-comedy.jpg",
"language": "Italian",
"link": "https://en.wikipedia.org/wiki/Divine_Comedy\n",
"pages": 928,
"title": "The Divine Comedy",
"year": 1315
},
解决方案
这是一个简单的方法:
在您的三个输入上绑定一个更改事件,如下所示<ion-input (ionChange)="filterItems()"></ion-input>
:
将您的filterItems()
功能添加到您的模态控制器:
filterItems() {
this.books.filter((item) => {
// the condition inside here, decides whether the item will be shown or not
return item.title.toLowerCase().includes(this.title.toLowerCase) && item.author.toLowerCase().includes(this.author.toLowerCase());
});
}
现在您正在匹配标题和作者。当然,你需要将你ion-input
的 's 与 ngModel 绑定。
*ngFor
您可以使用模板输出您的书籍。
关心!当输入为空时,该函数将返回 false。也许您需要根据需要稍微更改条件。
推荐阅读
- javascript - 如何将 Promise 的结果从节点传递到 HTML 模板?
- java - Java二叉搜索树:min和max之间所有节点键的总和
- c# - 我们如何将材料添加到 Google Classroom API Coursework 对象?
- python - Python smtplib 接受日历事件
- c++ - 当仅在一行上提示输入一次时,如何确保用户输入名字和姓氏?
- html - 可以使用 style="float: right;" 的元素吗 与一系列“float:left;”中的第一个对齐 元素?
- javascript - setInterval(function(), 5) 不适用于 iOS 或任何移动设备
- javascript - 使用 React Hooks 的倒数计时器
- javascript - Discord.js 使用 404 获取 5 条特定消息完全失败
- scala - 如何从 Spark Dataframes 中的列中获取常量值