我有一个用例,我希望能够在我的Angular应用程序中搜索在mongodb中注册的用户列表。
为此,我使用自定义管道根据用户的地址过滤用户。但是,它永远不会返回正确的用户。如果我输入字母" W"在搜索字段中,应用程序找到实例' W'发生时,它只返回数据库中的2个第一个注册用户,而不是实际居住在具有' W'包括在内。如果有3个实例,它将返回数据库中的3个第一个用户,依此类推..
我认为这一定是因为我根据索引显示数据库中的每个用户,但我不知道如何显示用户列表。
这是代码:
filter.pipe.ts:
import { Pipe, PipeTransform, Injectable } from "@angular/core";
import { User } from "../entities/user";
@Pipe({
name: 'filter'
})
@Injectable()
export class FilterPipe implements PipeTransform {
//ARGS contain what we write in the field.
transform(items: User[], args: string): any {
if(args && items.length > 0) {
let itemsFound = items.filter(
item =>
item.address &&
item.address.toLowerCase().includes(args.toLowerCase())
);
if(itemsFound && itemsFound.length > 0) {
return itemsFound;
}
return [-1]; //in case none is found.
}
return items; // we could change to return [] if we didnt want to show any users until someone wrote something in
} }
用户-list.component.html:
<div class="container-fluid">
<div class="row">
<div *ngFor="let user of users | filter: search; let i = index" class="e2e-baby">
<div class="col-lg-12 col-md-12 mb-12 mt-4">
<mat-card class="example-card">
<mat-card-header>
<div mat-card-avatar class="example-header-image"></div>
<mat-card-title><b>Babys navn:</b> {{users[i].fullnameBaby}}</mat-card-title>
<mat-card-subtitle><b>Forældrens navn: </b> {{users[i].fullname}} </mat-card-subtitle>
</mat-card-header>
<img mat-card-image src="http://localhost:3000/{{users[i].profilePicture}}" alt="Photo of a Shiba Inu" style="width:400px; height: 350px;">
<mat-card-content>
<p>
{{users[i].infoAboutUser}}
</p>
<p><small>Kan findes her: {{users[i].address}}, {{users[i].zipcode}} </small></p>
</mat-card-content>
<mat-card-actions>
<button mat-button (click)="hire(users[i])">Hyr</button>
<button mat-button>Rate</button>
</mat-card-actions>
</mat-card>
</div>
<!-- end of colcard -->
</div>
</div>
</div>
用户-list.component.ts
import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { NgRedux } from '@angular-redux/store';
import { IAppState } from '../store/store';
import {changeNavbarColor} from '../../assets/javascript/transperentNavbar.js';
import { UsersService } from '../users.service';
import { UsersActions } from '../users.actions';
import { Sitter } from '../entities/sitter';
import { User } from '../entities/user';
import { FilterPipe } from './filter.pipe';
@Component({
selector: 'app-user-list',
templateUrl: './user-list.component.html',
styleUrls: ['./user-list.component.scss'],
})
export class UserListComponent implements OnInit {
private users: User[];
constructor(private ngRedux: NgRedux<IAppState>, private userService: UsersService,
private usersActions :UsersActions) { changeNavbarColor(); }
ngOnInit() {
this.usersActions.getUsers(); //gets all users on the load of the page
this.ngRedux.select(state => state.users).subscribe(u=> {
this.users = u.users;
console.log(this.users);
})
}
hire(user: User){
this.userService.sendNotification(user).subscribe(result => {
console.log(result);
});
console.log("This guy have been hired.")
console.log(user);
}
}
任何知道我如何解决这个问题的人都知道了吗?
答案 0 :(得分:1)
你可以尝试这个解决方案
上创建了一个演示HTML代码
<input type="text" [(ngModel)]="terms" placeholder="Serach any fields"/>
<table border="1" width="100%">
<tr>
<th>Name</th>
<th>Venue</th>
</tr>
<tr *ngFor="let usrData of users | search:terms">
<td>{{usrData.name}}</td>
<td>{{usrData.venue}}</td>
</tr>
</table>
Ts文件代码
users=[{
name:'test 1',
venue:'venue 1'
},{
name:'user',
venue:'305 vijay nagar'
},{
name:'Admin',
venue:'test venue'
},{
name:'user 5',
venue:'254561'
}]
搜索过滤器
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'search'
})
export class SearchPipe implements PipeTransform {
transform(value: any, args?: any): any {
if(!args){
return value;
}
return value.filter((val)=>{
let rVal=(val.name.toLocaleLowerCase().includes(args)) || (val.venue.toLocaleLowerCase().includes(args));
return rVal;
})
}
}