javascript - 如何显示搜索结果
问题描述
我的 Angular 应用程序上有一个搜索页面。它搜索属性(房屋,公寓等)。用户通过一个表单选择他们的标准,这个表单触发我的组件中的一个函数,该函数通过一个服务从我的 API 中访问一个端点。
数据保存在我的组件中,目前我正在将其记录到控制台。
在页面上显示结果的最佳方式是什么。
这是搜索页面html。
<div class="container mt-4">
<div class="card">
<form [formGroup]="searchForm" (ngSubmit)="search()">
<div class="card-header">Search for a Property</div>
<div class="card-body">
<!-- County and Town Label-->
<div class="row">
<div class="col-md-6">
<label class="ml-1" for="county">County</label>
</div>
<div class="col-md-6">
<label for="town">Town</label>
</div>
</div>
<div class="row">
<!-- County Column -->
<div class="col-md-6">
<select class ="form-control" id="county" formControlName="county" >
<option *ngFor="let county of counties" [value]="county.value">
{{county.display}}
</option>
</select>
</div>
<div class="col-md-6">
<select class="form-control" formControlName="town">
<option *ngFor="let town of towns" [value]="town.value">
{{town.display}}
</option>
</select>
</div>
</div>
<!-- Bed and Bath Label-->
<div class="row mt-md-4">
<div class="col-md-3">
<label class="ml-1" for="min-bedrooms">Min Bed</label>
</div>
<div class="col-md-3">
<label for="max-bedrooms">Max Bed</label>
</div>
<div class="col-md-3">
<label class="ml-1" for="min-bathrooms">Min Bath</label>
</div>
<div class="col-md-3">
<label for="max-bathrooms">Max Bath</label>
</div>
</div>
<div class="row">
<div class="col-md-3">
<select class="form-control" formControlName="min_bedrooms">
<option *ngFor="let room of rooms" [value]="room.value">
{{room.display}}
</option>
</select>
</div>
<div class="col-md-3">
<select class="form-control" formControlName="max_bedrooms">
<option *ngFor="let room of rooms" [value]="room.value">
{{room.display}}
</option>
</select>
</div>
<div class="col-md-3">
<select class="form-control" formControlName="min_bathrooms">
<option *ngFor="let room of rooms" [value]="room.value">
{{room.display}}
</option>
</select>
</div>
<div class="col-md-3">
<select class="form-control" formControlName="min_bathrooms">
<option *ngFor="let room of rooms" [value]="room.value">
{{room.display}}
</option>
</select>
</div>
</div>
<div class="row mt-md-4">
<div class="col-md-3">
<label class="ml-1" for="min-rent">Min Price</label>
</div>
<div class="col-md-3">
<label for="max-rent">Max Price</label>
</div>
<div class="col-md-3">
<label class="ml-1" for="type">Selling Type</label>
</div>
<div class="col-md-3">
<label class="ml-1" for="type">Property Type</label>
</div>
</div>
<div class="row">
<div class="col-md-3">
<select class="form-control" formControlName="min_price">
<option *ngFor="let price of prices" [value]="price.value">
{{price.display}}
</option>
</select>
</div>
<div class="col-md-3">
<select class="form-control" formControlName="max_price">
<option *ngFor="let price of prices" [value]="price.value">
{{price.display}}
</option>
</select>
</div>
<div class="col-md-3">
<select class="form-control" formControlName="selling_type">
<option *ngFor="let type of sellingTypes" [value]="type.value">
{{type.display}}
</option>
</select>
</div>
<div class="col-md-3">
<select class="form-control" formControlName="property_type">
<option *ngFor="let type of propertytypes" [value]="type.value">
{{type.display}}
</option>
</select>
</div>
</div>
<div class="row mt-md-4">
<div class="col-md-4">
<button type="submit" class="form-control btn btn-primary">
Search
</button>
</div>
</div>
</div>
</form>
</div>
</div>
这是链接到 HTML 的组件
export class PropertySearchComponent implements OnInit {
searchForm: FormGroup;
searchParams: any = {};
property: Property;
constructor(private advertService: AdvertService, private alertify: AlertifyService, private formBuilder: FormBuilder) { }
ngOnInit() {
this.createSearchForm();
}
createSearchForm() {
this.searchForm = this.formBuilder.group({
county: ['', Validators.nullValidator],
town: ['', Validators.nullValidator],
min_bedrooms: ['', Validators.nullValidator],
max_bedrooms: ['', Validators.nullValidator],
min_bathrooms: ['', Validators.nullValidator],
max_bathrooms: ['', Validators.nullValidator],
min_price: ['', Validators.nullValidator],
max_price: ['', Validators.nullValidator],
selling_type: ['', Validators.nullValidator],
property_type: ['', Validators.nullValidator],
});
}
search() {
this.searchParams.county = (Object.assign({}, this.searchForm.value));
this.advertService.propertySearch(this.searchParams).subscribe(data => {
this.property = data;
console.log(this.property);
}, error => {
console.log(error);
});
}
在上面的 search() 函数中,属性搜索结果保存在 this.property 中。
这是我的服务中从组件调用的函数。
propertySearch(model: any): Observable<Property> {
return this.http.post<Property>(environment.apiUrl + 'search', model);
}
我应该如何去显示保存在 this.property 中的结果。
解决方案
您需要添加一个包含您的字段的表。
并像这样显示,我假设您的属性变量具有这些 id、名称、电子邮件、网站。
<table class="table table-striped">
<thead>
<tr>
<th style="width: 20%">
ID
</th>
<th style="width: 50%">
Name
</th>
<th style="width: 10%">
Email
</th>
<th style="width: 20%">
Website
</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of property">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.email}}</td>
<td>{{item.website}}</td>
</tr>
</tbody>
</table>
推荐阅读
- javascript - 有什么方法可以知道 npm 包是否具有特定于操作系统的构建要求?
- python - 如何跨 Slurm 集群上的多个节点运行 MPI Python 脚本?错误:警告:无法在 2 个节点上运行 1 个进程,将 nnodes 设置为 1
- laravel - 如何在 laravel-backpack 中将 ckeditor 更新到 v5?
- machine-learning - GridSearchCV 未在 colab 中获取 mean_test_score
- python - 为什么我的游戏延迟发生在我想要的之前?
- sql - 根据其他行中的其他值选择一行的值
- php - 是否有一个内置的 PHP 函数可以在单个语句中简单地提取一个正则表达式匹配?
- linux - 为什么当我在我的 EC2 实例上启动 SSM 代理时,我得到“单元被屏蔽。”?
- git - 团队成员如何将新的 Bitbucket 分支推送到他们分叉的主仓库?
- c - 使变量具有与#define 相同的类型/效果?