【问题标题】:Angular 4 Forms --> Search Results from InputAngular 4 Forms --> 从输入中搜索结果
【发布时间】:2018-06-16 11:37:17
【问题描述】:

所以我有点问题。我必须为旅行社创建一个搜索页面。在此示例中,我正在预订航班。我不知道如何根据表单 ahhhhh 的输入显示每个结果。我一直在努力,但我已经耗尽了一天的精神能量。

这里有一些代码:

HTML

<!-- Search Form -->
<form class="" action="index.html" method="post">

  <h1> Let's Go. </h1>
  <hr>

  <select class="" name="">
    <option value="">Select means of Travel</option>
    <option value="">Flight</option>
  </select>

  <label for="Travel Start Date">Travel Start Date</label>
  <input type="date" name="" value="Travel Start Date">

  <label for="Travel End Date">Travel End Date</label>
  <input type="date" name="" value="Travel End Date">

  <select class="" name="">
    <option value="">Departure Location</option>
    <option value="Atlanta">Atlanta</option>
  </select>

  <select class="" name="">
    <option value="">Arrival Location</option>
    <option value="San Francisco">San Francisco</option>
  </select>

  <select class="" name="">
    <option value="">Product Class</option>
    <option value="">Economy</option>
  </select>

  <input style="width: 100px; background-color: green; color: #eef; height: 40px; border-radius: 50px; margin: 0 auto; margin-top: 50px;"
         type="submit" name="" value="Submit" onclick="">

</form>

<!-- Results -->

<div class="result">
  <ul>
    <li *ngFor = 'let group of displayItems'>
      <div class="flight-card">

        <div class="availability">
          <h5> Availability: {{group.availability}} </h5>
        </div>

        <div class="price">
          {{ group.price.symbol }}
          {{ group.price.amount }}
          <p>{{ group.price.currency }}</p>
        </div>

        <div class="arrival">
          <h5> Arrival City: </h5>{{group.arrival.city}} <br>
          <h5> Arrival Airport Name: </h5>{{group.arrival.airport.name}} <br>
          <h5> Arrival Time: </h5>{{group.arrival.city}} <br><br>
        </div>

        <hr>

        <div class="depature">
          <h5> Depature City: </h5>{{group.departure.city}} <br>
          <h5> Departure Airport Name: </h5>{{group.departure.airport.name}} <br>
          <h5> Departure Time: </h5>{{group.departure.time}} <br><br>
        </div>

      </div>
    </li>
  </ul>
</div>

(在根应用程序中) 组件.Ts

    import { Component } from '@angular/core';
import { Http, Response, RequestMethod, RequestOptions, Headers } from '@angular/http';
import { NgIf } from '@angular/common';
import { Form } from '@angular/forms';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  result;
  errorFromSubscribe;
  errorFromCatch;
  displayItems;



  // Inject HttpClient into your component or service.
 constructor(private http: Http) {}

 ngOnInit(): void {
   // Making the HTTP Request
   this.http
    // Get API data
    .get('https://api.myjson.com/bins/1gb9tf')
    // Subscribe
    .subscribe( (res: Response) => {
      // Parses the response to JSON.
      this.result = res.json();
      this.displayItems = this.result['results'];
      console.log(this.result);
    }, error => {
        console.log(error)
        this.errorFromSubscribe = error;
      });


 }

  title = 'app';
}

【问题讨论】:

    标签: html angular forms uisearchbardisplaycontrol


    【解决方案1】:

    您需要使用管道进行过滤,通过应用特定逻辑来过滤搜索项

      <li *ngFor="let group of displayItems  | filter: term">
    

    和管道如下,

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
      name: 'filter'
    })
    export class FilterPipe implements PipeTransform {
     transform(items: any, term: any): any {
        if (term === undefined) return items;
    
        return items.filter(function(item) {
          for(let property in item){
    
            if (item[property] === null){
              continue;
            }
            if(item[property].toString().toLowerCase().includes(term.toLowerCase())){
              return true;
            }
    
           }
          return false;
        });
      }
    
    }
    

    DEMO

    【讨论】:

    • 非常感谢。您至少为我自己提供了解决方案。只是在使用这种方法或多个输入时遇到了一些问题
    猜你喜欢
    • 2021-10-15
    • 2016-12-16
    • 2020-03-09
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    相关资源
    最近更新 更多