Angular:搜索过滤器RXJS
我实现了它在“输入”按钮中查找的函数 但是我遇到了一个问题,我在那里有一个条件,我希望输入目前为空,以便将其误解并显示我的所有数据。
我希望的是,一旦输入为空,就会向我显示所有数据,如果它不是空的,则它将显示适当的数据,或者不存在然后找不到:
我的接口:
export interface Post{
id:number,
date: Date,
authorName:string,
content:string;
}
我的seeddate:
createDb() {
const posts: Post[] = [
{id:1,authorName:"Daniel",content:"Amazon’s Garage Delivery Service"
,date:new Date('11/12/20')
},
{id:2,authorName:"Omer",content:"Jake From State Farm "
,date:new Date('12/20/21')},
{id:3,authorName:"Lior",content:"The General"
,date:new Date('02/01/22')},
{id:4,authorName:"Tomer",content:"Spotify’s Wrapped "
,date:new Date('11/11/20')},
];
return {posts};
html:
<input
type="search"
(input)="SearchPostsByName($event)"
[(ngModel)]="authorName"
/>
<div *ngFor="let post of validPosts">
<ng-container *ngIf="isExists">
The name of the author is <b>{{ post?.authorName }}</b> with content
<b>{{ post?.content }}</b> and released it on the date
<b>{{ post?.date }}</b>
</ng-container>
</div>
<br>
typescript:
posts: Post[] = [];
authorName = "";
content = "";
date = new Date();
isExists = false;
validPosts: Post[] = [];
Search:string='';
constructor(private postService: PostService) { }
ngOnInit(): void {
this.postService.getPosts().subscribe((posts)=> {
this.posts = posts;
});
}
SearchPostsByName($event : Event) {
console.log($event);
if (this.authorName) {
this.validPosts = this.posts.filter((value) => value.authorName === this.authorName!);
if (this.validPosts.length > 0
) {
console.log('found');
this.isExists = true;
} else {
this.isExists = false;
}
}
}
我的服务:
export class PostService {
private postsUrl = 'api/posts';
constructor(private http: HttpClient) { }
getPosts(): Observable<Post[]> {
return this.http.get<Post[]>(this.postsUrl);
}
addPost(post: Post): Observable<Post> {
let httpOptions = {
headers: new HttpHeaders({'Content-Type':'application/json'})
};
return this.http.post<Post>(this.postsUrl, post, httpOptions)
}
I implement a function that it looks for in the input button by name
But I have a problem I made a condition there that I want the input to be empty at the moment so it will get false and display all my data.
Right now as soon as the INPUT is empty I wrote a message "not found":
What I want it to be is that as soon as the input is empty it will show me all the data and if it is not empty then it will show me the appropriate data or if it does not exist then it will not be found:
My interface:
export interface Post{
id:number,
date: Date,
authorName:string,
content:string;
}
My seedDate:
createDb() {
const posts: Post[] = [
{id:1,authorName:"Daniel",content:"Amazon’s Garage Delivery Service"
,date:new Date('11/12/20')
},
{id:2,authorName:"Omer",content:"Jake From State Farm "
,date:new Date('12/20/21')},
{id:3,authorName:"Lior",content:"The General"
,date:new Date('02/01/22')},
{id:4,authorName:"Tomer",content:"Spotify’s Wrapped "
,date:new Date('11/11/20')},
];
return {posts};
html:
<input
type="search"
(input)="SearchPostsByName($event)"
[(ngModel)]="authorName"
/>
<div *ngFor="let post of validPosts">
<ng-container *ngIf="isExists">
The name of the author is <b>{{ post?.authorName }}</b> with content
<b>{{ post?.content }}</b> and released it on the date
<b>{{ post?.date }}</b>
</ng-container>
</div>
<br>
typescript:
posts: Post[] = [];
authorName = "";
content = "";
date = new Date();
isExists = false;
validPosts: Post[] = [];
Search:string='';
constructor(private postService: PostService) { }
ngOnInit(): void {
this.postService.getPosts().subscribe((posts)=> {
this.posts = posts;
});
}
SearchPostsByName($event : Event) {
console.log($event);
if (this.authorName) {
this.validPosts = this.posts.filter((value) => value.authorName === this.authorName!);
if (this.validPosts.length > 0
) {
console.log('found');
this.isExists = true;
} else {
this.isExists = false;
}
}
}
My service:
export class PostService {
private postsUrl = 'api/posts';
constructor(private http: HttpClient) { }
getPosts(): Observable<Post[]> {
return this.http.get<Post[]>(this.postsUrl);
}
addPost(post: Post): Observable<Post> {
let httpOptions = {
headers: new HttpHeaders({'Content-Type':'application/json'})
};
return this.http.post<Post>(this.postsUrl, post, httpOptions)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信最大的错误是您忘了初始化
有效posts
ngoninit ,这解释了为什么您在开始时没有看到任何帖子:如果您想显示全部空白搜索上的帖子,您可以检查搜索键单词是否为空字符串,如果是这样,则将您的
有效posts
设置为posts
:应该就是这样!
如果您想进一步改进,我还有其他一些建议:
ports
完全好。ISEXISTS
变量即可跟踪如果您有任何有效posts
,因为它是一个数组,则可以检查其长度。想要进一步采用它吗?
onpush
更改检测以提高性能。I believe the biggest mistake is that you forgot to initialize your
validPosts
insidengOnInit
, which explains why you're not seeing any posts in the beginning:If you want to display all the posts on an empty search, you can check if the search key word is an empty string, if so set your
validPosts
toposts
:That's should be it!
Here are some other suggestions that I have if you want to improve this even further:
posts
by the search keyword would be perfectly fine.isExists
variable just to track if you have anyvalidPosts
, since it's an array, you can check its length instead.Want to take it even further?
onPush
change detection to improve performance.我建议您用 keyup 替换输入事件。
在SearchPostsbyName($ event)中,查询 event $ .target.value 检查是否有任何输入:
I suggest you to replace your input event, with keyup.
In SearchPostsByName($event), query event$.target.value to check if there's any input: