【问题标题】:Angular 12 with Angular Material trying to filter list based on checkbox checked带有Angular Material的Angular 12尝试根据选中的复选框过滤列表
【发布时间】:2022-10-17 22:32:42
【问题描述】:

我正在尝试根据检查的大陆过滤国家列表。

我相信事情的模板方面是在一个好地方:

ngOnInit() {
  this.filteredCountries = this.countries;
  this.selectedContinents.valueChanges.subscribe(selectedValue => {
    this.filteredCountries = this.countries.filter(country => {
      return this.selectedContinents.value?.includes(country.continent);
    });
  }
}
<mat-form-field class="country-code-dialog-search-container">
  <input matInput (change)="onChangeContinentCheckBox()" placeholder="Filter by Continents" cdkFocusInitial>
  <mat-select placeholder="Filter by Continents" multiple>
    <mat-option *ngFor="let continent of getContinents()">{{continent}}</mat-option>
  </mat-select>
</mat-form-field>

<section class="example-section" *ngFor="let country of filteredCountries">
 <span class="example-list-section">
   <mat-checkbox class="example-margin">
     {{country.name}} ({{country.iso3}}) <span>{{country.emoji}}</span>
   </mat-checkbox>
 </span>
</section>

我见过这样的事情:

<mat-options type="checkbox" *ngFor="let continent of getContinents() | selectedContinents">{{continent}}</mat-options>

在函数中:

onChangeContinentCheckBox() { 
 this.countries.filter('selectedCountries', function(country) {
   return country.continent === /* logic for obtaining checked selection */
 });
}

但这只是破坏了我的应用程序,这意味着 mat-form-field 不再呈现,国家名称、ISO 和表情符号也不再呈现,所以它只是一个空的复选框呈现列表。

所以这是我在 Stackblitz 上的一个例子:

https://stackblitz.com/edit/angular-qpxrkm-6vbpmh?file=src%2Fapp%2Fselect-multiple-example.html,src%2Fapp%2Fselect-multiple-example.ts

【问题讨论】:

  • 请更具体。 “破坏您的应用程序”是什么意思?
  • @E.Maggini,我为打破应用程序的含义添加了一些清晰度。
  • 您需要将您的 mat-select 绑定到一个变量并在您的过滤器中使用它,使用您拥有的代码我看不到您使用的是反应式表单还是模板驱动的表单,对于反应式表单,您需要使用 [formControl]='selectedCountries'] 和用于模板驱动使用[(ngModel)]="selectedCountrie"
  • @Juan,所以你指的是[(value)]="selectedContinent"
  • 在 stackblitz 或其他上创建问题的最小再现对于更快/更准确地获得答案很有用。 stackoverflow.com/help/how-to-ask

标签: javascript angular


【解决方案1】:

稍作修改,您可以将FormControl 仅用于角度材料的select,然后订阅它的valueChanges,在其中您可以过滤countries 数据,查看代码中的cmets

工作示例

https://stackblitz.com/edit/angular-qpxrkm?file=src/app/select-multiple-example.html

import {Component, OnInit} from '@angular/core';
import {FormControl} from '@angular/forms';


@Component({
  selector: 'select-multiple-example',
  templateUrl: 'select-multiple-example.html',
})
export class SelectMultipleExample implements OnInit{
//you need to create the FormControl that will hold the selections made on the dropdown
//this will be used to filter the countries
  selectedContinent = new FormControl('');
  
  continents: string[] = ['America','Antartica'];
  countries: any[]=[{continent:'America', country:'USA'},{continent:'America', country:'Mexico'},{continent:'Antartica', country:'New Zeland'}];
  
  filteredContries : any[] = [];

  ngOnInit(){
  //FormControls have a valueChanges that you can subscribe to and apply your filter
    this.selectedContinent.valueChanges.subscribe(selectedValue=>{
    
      this.filteredContries = this.countries.filter(c=>{
      //FormControls have a `.value` that holds the selected(in this case) values,
      //in this case it's an array of strings, that's why we use the `includes` to look a each 
      //of the countries continent
        return this.selectedContinent.value?.includes(c.continent);
      });
    });
  }
}
<mat-form-field appearance="fill">
  <mat-label>Continents</mat-label>
  <mat-select [formControl]="selectedContinent" multiple>
    <mat-option *ngFor="let continent of continents" [value]="continent">{{continent}}</mat-option>
  </mat-select>
</mat-form-field>

<code>
{{selectedContinent.value|json}}
</code>
<code>
{{filteredContries|json}}
</code>

【讨论】:

  • 大陆不是一个对象数组,只是一个具有键值对的对象,复选框中选择的键必须与this.countries.continent的值匹配
  • 我不能使用FormControl。正如您之前建议的那样,它必须是[(value)]="selectedContinent",因此我无权访问valueChanges,也无权访问value?
  • 你能像我一样检查ngOnInit()吗?我采用了您的解决方案,但是当我单击特定大陆的复选框时,它仍然没有过滤这些国家。
猜你喜欢
  • 2019-09-13
  • 2013-09-16
  • 1970-01-01
  • 2019-05-08
  • 2013-05-20
  • 2019-05-15
  • 1970-01-01
  • 2018-04-20
  • 2021-10-23
相关资源
最近更新 更多