【问题标题】:Angular 5 - using filter in select tag with pipeAngular 5 - 在带有管道的选择标签中使用过滤器
【发布时间】:2018-05-02 10:16:55
【问题描述】:

我有一个select html 标记,我想按所选值过滤我的结果列表。我正在使用管道在*ngFor 创建的列表中显示它们。当我添加一个新项目时,选择标签会添加一个新选项,即使还有一个名称相同的选项。我该如何解决这个问题?我希望我的选择标签仅显示唯一值(过滤器???)。我也想设置一个默认值。

截图

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

【问题讨论】:

  • 我不确定我是否完全理解错误。您的示例似乎运行良好,您能否详细说明为什么这不是所需的行为?
  • 如果我添加一个 status: 'stable' 的服务器,我会看到一个更长的选项值列表可供选择,三个“stable”类型,但我只需要一个来应用过滤器。我添加了一个截图

标签: javascript angular typescript filter pipe


【解决方案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"

【讨论】:

    【解决方案2】:

    很难相信,但我基于此示例的代码直到我在输入文本字段中明确添加了具有某些值的“名称”文件后才起作用。过滤过程中没有发生任何事情

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 1970-01-01
      • 2021-03-07
      • 2017-05-14
      • 2018-06-16
      • 1970-01-01
      • 2018-09-22
      • 2012-12-11
      • 2017-11-29
      相关资源
      最近更新 更多