【问题标题】:using formGroup for 2 filters将 formGroup 用于 2 个过滤器
【发布时间】:2018-10-30 21:42:29
【问题描述】:

我正在使用表单从价格过滤器中过滤相关产品:

<form [formGroup]="myForm">
    <select name="Price" formControlName="filterProduct">
      <option *ngFor="let k of PriceFilter; let i = index;"
        [ngValue]="getValue(i)">
        {{ getValue(i).displayText }}
      </option>
    </select>
</form>
       <ul>
        <li *ngFor="let product of filteredProducts">
            <img src={{product.ProductImage}}>
            <p>store: {{ store?.StoreName }}</p>
            <p>Product Price: {{ product.Price }}</p>
          <p>Product Title: {{ product.ProductTitle }}</p>

        </li>
      </ul>

.ts:

myForm: FormGroup;
constructor(private _storeService:StoreService,private fb: FormBuilder) {
    this.myForm = this.fb.group({
      filterProduct: ['']
    })}
getValue(index) {
    if(index === 0)
      return { 
        lower: 0, 
        displayText: this.PriceFilter[index].DisplayText, 
        upper: this.PriceFilter[index].Value 
      };
    else {
      return { 
        lower: this.PriceFilter[index - 1].Value, 
        upper: this.PriceFilter[index].Value,
        displayText: `${this.PriceFilter[index - 1].DisplayText} - ${this.PriceFilter[index].DisplayText}`
      };
    }
  }
ngOnInit() {
    this._storeService.getProducts()
   .subscribe(data =>{
   this.myForm.get('filterProduct').valueChanges.subscribe(
        value => {
          console.log(value);
          this.filteredProducts = [...this.Stores[0].Products.filter(product => product.Price >= value.lower && product.Price <= value.upper )]
        }
      )

       });

PriceFilter 看起来像:

      PriceFilter = [
{
      "TagId": 20,
      "Type": "Budget",
      "Value": 25,
      "Values": null,
      "DisplayText": "$25",
      "Order": null
    },
    {
      "TagId": 21,
      "Type": "Budget",
      "Value": 50,
      "Values": null,
      "DisplayText": "$50",
      "Order": null
    }]

我有一个看起来非常相似的性别过滤器,如果性别过滤器和产品具有相同的标签 id,而不是价格范围,我想要显示相关产品的过滤器,如何在相同的情况下添加另一个过滤器形式? gender

【问题讨论】:

  • 您能详细说明您的问题吗?您是否在问如何向表单添加另一个控件?或者如何在过滤器语句中添加“OR”子句?我在这里有一篇关于过滤的博文:blogs.msmvps.com/deborahk/filtering-in-angular
  • 哇!很有用。我在问如何在我的过滤器语句中添加“OR”子句,这样我就不需要编写两种不同的方法(或每个过滤器的许多方法)谢谢! @DeborahK
  • 那么希望这篇博文能回答你的问题?
  • @DeborahK 不完全是..我从同一个 JSON 文件中获取产品和过滤器,我没有为产品/过滤器构建类/组件..(现在我相信更好)我只想使用我用于另一个过滤器的表单

标签: angular typescript angular4-forms


【解决方案1】:

首先在Stores[0].Products 中为产品添加性别属性,因为它将用于过滤:

Products = [
  {
    .....
    Gender: 'Boy'
  },
  {
    ....
    Gender: 'Both'
  }
]

比我假设你想在页面上选择性别,所以你应该在你的模板中为性别添加单独的&lt;select&gt;,所以在表单中的 priceFilter 选择器下面添加这个代码:

<select name="Gender" formControlName="gender">
  <option *ngFor="let k of GenderFilter;"
    [ngValue]="k">
    {{ k.displayText }}
  </option>
</select>

要处理更改,您需要将此选择控件添加到表单组:

this.myForm = this.fb.group({
      filterProduct: [''],
      gender: [''] 
})}

现在您还必须订阅该控件的 valueChanges,与价格过滤器相同:

ngOnInit() {
  this._storeService.getProducts()
    .subscribe(data =>{
      this.myForm.get('filterProduct').valueChanges.subscribe(
        value => {
          console.log(value);
          this.filteredProducts = [...this.Stores[0].Products
            .filter(product => 
               product.Price >= value.lower
               && product.Price <= value.upper)]
        }
     ) 
     this.myForm.get('gender').valueChanges.subscribe( value =>
         this.filteredProducts = [...this.Stores[0].Products
            .filter(product => product.Gender === value.displayText]
     )
   });

【讨论】:

  • 非常感谢您的回答!我只是不清楚,除了 PriceFilter 我还有另一个过滤器 GenderFilter 看起来像:“GenderFilter”:[ { "TagId": 3, "Type": "Gender", "Value": 3, "Values": null, "DisplayText": "Boy", "Order": 1 }, { "TagId": 4, "Type": "Gender", "Value": 4, "Values": null, "DisplayText": "Girl", "Order": 2 } ] 过滤器不同,但产品中的 tagId,请参阅两个过滤器(如果适合,则会出现相同的产品过滤器-价格或性别)
  • Value 属性在 GenderFilter 实例中代表什么?
  • 在GenderFilter中的值表示TagID(如果产品有标签id:4并且genderFilter中的“boy”是TagId 4,则值为4,所以如果选择“boy”这个产品会出现)在 PriceFilter 中,值是产品的 PRICE :)
  • 如果你只是告诉我,this.Stores[0].Products 中的产品如何,看起来我将编辑我的答案,以便它使用 GenderFilter
  • [this.Stores[0].Products 过滤后的控制台)[i.stack.imgur.com/xAYvr.png](Json 文件)[i.stack.imgur.com/BapB1.png]非常感谢!
猜你喜欢
  • 2019-09-17
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 2020-09-03
  • 2011-11-01
  • 1970-01-01
相关资源
最近更新 更多