我有一个select
html标记,我想按所选值过滤我的结果列表。我正在使用管道在*ngFor
创建的列表中显示它们。当我添加一个新项目时,select标签会添加一个新选项,即使有一个同名的选项。我怎么能解决这个问题?我希望我的select标签只显示唯一值(Filter ???)。我也想设置一个默认值。
截图:
App.component.html :
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-10 col-md-8 col-sm-offset-1 col-md-offset-2">
<select
name="serverStatus"
id="serverStatus"
[(ngModel)]="filteredStatus">
<option
*ngFor="let server of servers"
value="{{server.status}}">{{server.status}}</option>
</select>
<hr>
<ul class="list-group">
<li
class="list-group-item"
*ngFor="let server of servers | filter:filteredStatus:'status'"
[ngClass]="getStatusClasses(server)">
<span
class="badge">
{{ server.status }}
</span>
<strong>{{ server.name }}</strong> |
{{ server.instanceType | uppercase }} |
{{ server.started | date:'fullDate' | uppercase }}
</li>
</ul>
</div>
App.component.ts :
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
servers = [
{
instanceType: 'medium',
name: 'Production Server',
status: 'stable',
started: new Date(15, 1, 2017)
},
{
instanceType: 'large',
name: 'User Database',
status: 'stable',
started: new Date(15, 1, 2017)
},
{
instanceType: 'small',
name: 'Development Server',
status: 'offline',
started: new Date(15, 1, 2017)
},
{
instanceType: 'small',
name: 'Testing Environment Server',
status: 'critical',
started: new Date(15, 1, 2017)
}
];
filteredStatus = '';
getStatusClasses(server: {instanceType: string, name: string, status: string, started: Date}) {
return {
'list-group-item-success': server.status === 'stable',
'list-group-item-warning': server.status === 'offline',
'list-group-item-danger': server.status === 'critical'
};
}
}
fiter.pipe.ts :
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(value: string[], filterString: string, propName:string): any {
if (value.length === 0 || filterString === '') {
return value;
}
const resultArray = [];
for (const item of value) {
if (item[propName] === filterString) {
resultArray.push(item)
}
}
return resultArray;
}
}
这是一个stackblitz工作示例:https://angular-rwmfbj.stackblitz.io 编辑:https://stackblitz.com/edit/angular-nck1kn
答案 0 :(得分:1)
试试这个
<强> App.component.ts 强>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
servers = [
{
instanceType: 'medium',
name: 'Production Server',
status: 'stable',
started: new Date(15, 1, 2017)
},
{
instanceType: 'large',
name: 'User Database',
status: 'stable',
started: new Date(15, 1, 2017)
},
{
instanceType: 'small',
name: 'Development Server',
status: 'offline',
started: new Date(15, 1, 2017)
},
{
instanceType: 'small',
name: 'Testing Environment Server',
status: 'critical',
started: new Date(15, 1, 2017)
}
];
filteredStatus = '';
serverStatus: {
instanceType: string,
name: string,
status: string,
started: Date
}[] = [];
constructor(){
this.initializeStatus();
//For setting the default value of the select try this
if(this.serverStatus.length != 0)
this.filteredStatus = this.serverStatus[0].status;
}
getStatusClasses(server: {instanceType: string, name: string, status: string, started: Date}) {
return {
'list-group-item-success': server.status === 'stable',
'list-group-item-warning': server.status === 'offline',
'list-group-item-danger': server.status === 'critical'
};
}
//Initalize the status
initializeStatus(){
this.serverStatus = [];
//Container for the current status
let currentStatus = "";
for(let x of this.servers){
if(x.status != currentStatus){
this.serverStatus.push(x);
}
//Equate for reference
currentStatus = x.status;
}
}
}
在 App.component.html
中将*ngFor="let server of servers"
更改为此*ngFor="let server of serverStatus"
答案 1 :(得分:0)
令人难以置信,但是基于此示例的代码直到我在输入文本字段中明确添加带有某些值的“名称”文件后才起作用。过滤过程中没有任何反应