首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Show hidden mat-当所有其他内容被过滤掉时的选项

Show hidden mat-当所有其他内容被过滤掉时的选项
EN

Stack Overflow用户
提问于 2021-11-18 17:24:43
回答 1查看 29关注 0票数 1

因此,我有一个任务,我必须动态地创建多个<mat-select>,这取决于从后端返回的"tag类型“的数量。此外,<mat-select>用数据(标签)填充。用户可以创建一个新的tag类型,这意味着需要创建一个新的<mat-select><mat-select>也有一个过滤器。这就是我的问题所在,当过滤器过滤掉所有选项时,我需要显示一个用于创建新标记(否则隐藏)的默认<mat-option>。似乎什么都不起作用,我尝试将(change)="onFilterChange()"放在<mat-select>标签上,并尝试检查<mat-select>选项的长度(从@ViewChild('select')访问),如果是将布尔值切换为true,以显示用于创建标签的默认选项。不过,这并不管用。此外,(selectionChange)根本帮不上我的忙,因为它在过滤选项时不会触发。任何帮助或见解都是非常感谢的。代码如下:

代码语言:javascript
复制
<div class="col-md-6">
    <form *ngIf="preferencesFormGroup" [formGroup]="preferencesFormGroup">
      <mat-form-field *ngFor="let tagType of tagTypes$ | async">
        <mat-select placeholder="{{tagType.name}}" multiple #select [formControlName]="tagType.name">
          <mat-select-trigger>
            <mat-chip-list>
              <mat-chip *ngFor="let tag of preferencesFormGroup.controls[tagType.name].value" [removable]="true"
                (removed)="onTagsRemoved(tagType.name, tag)">
                {{ tag }}
                <mat-icon matChipRemove>cancel</mat-icon>
              </mat-chip>
            </mat-chip-list>
          </mat-select-trigger>
          <div class="search-wrapper">
            <input matInput placeholder="Search tags" [formControlName]="tagType.name + 'Search'" type="text"
              id="searchInput">
            <a mat-icon-button id="resetButton" (click)="clearFilter(tagType.name)">
              <mat-icon aria-label="Icon-button for clearing the input" id="clearIcon" [inline]="true">clear</mat-icon>
            </a>
          </div>
          <hr id="hrSearch">
          <mat-option
          *ngFor="let tag of tagType.tags | contains : preferencesFormGroup.controls[tagType.name+'Search'].value"
          [value]="tag.name">{{tag.name}}</mat-option>
          <mat-option *ngIf="noMoreOptions" (click)="createNewTag(tagType.name)"><i>Didn't find the tag you want? Click here to create your own.</i></mat-option>
        </mat-select>
      </mat-form-field>
    </form>
    <div class="row" style="align-items: center;">
      <div class="col-md-6">
        <div class="row" *ngIf="addDropDownActive" style="align-items: center;">
          <div class="col-md-8">
            <mat-form-field>
              <input matInput placeholder="Add new preference" type="text" [(ngModel)]="newPreference">
            </mat-form-field>
          </div>
          <div class="col-md-4">
            <button mat-button mat-stroked-button color="accent" (click)="createDropdown()"
              [disabled]="(newPreference === null) || (newPreference === '')">Save</button>
          </div>
        </div>
      </div>
      <div class="col-md-6">
        <button mat-button (click)="switchVisible()" color="accent" class="float-right">{{preferenceBtn}}</button>
      </div>
    </div>

    <div class="row mt-3">
      <div class="col-md-6">
        <button mat-button mat-stroked-button class="float-left" color="accent"
          (click)="goToPreviousStep()">Back</button>
      </div>
      <div class="col-md-6">
        <button mat-button mat-raised-button class="float-right" color="accent" (click)="goToNextStep()">Next</button>
      </div>
    </div>
  </div>

和.ts文件:

代码语言:javascript
复制
@Component({
  selector: 'tours-tour-tag',
  templateUrl: './tour-tag.component.html',
  styleUrls: ['./tour-tag.component.css']
})
export class TourTagComponent implements OnInit {

  tagTypes$: Observable<TagType[]>;
  preferencesFormGroup: FormGroup;
  addDropDownActive = false;
  newPreference: string = null;
  preferenceBtn = 'Create new preference';
  filterPropertySearch = '';
  noMoreOptions = false;

  @ViewChild('select') matSelect: MatSelect;

  constructor(private store: Store) { }

  ngOnInit(): void {
    this.tagTypes$ = this.store.select(TagTypeSelectors.selectAllTagTypes);
    this.tagTypes$.subscribe((tagTypes) => { this.initForm(tagTypes) });
  }

  onTagsRemoved(tagTypeName: string, tag: string) {
    const tags = this.preferencesFormGroup.controls[tagTypeName].value as string[];
    this.removeFirst(tags, tag);
    this.preferencesFormGroup.controls[tagTypeName].setValue(tags);
  }

  private removeFirst<T>(array: T[], toRemove: T): void {
    const index = array.indexOf(toRemove);
    if (index !== -1) {
      array.splice(index, 1);
    }
  }

  switchVisible() {
    this.addDropDownActive = !this.addDropDownActive;
    if (this.addDropDownActive) {
      this.preferenceBtn = 'Close';
    } else {
      this.preferenceBtn = 'Create new preference';
    }
    this.newPreference = null;
  }

  initForm(tagTypes: TagType[]) {
    this.preferencesFormGroup = new FormGroup({});
    tagTypes.forEach((tagType: TagType) => {
      this.preferencesFormGroup.addControl(tagType.name, new FormControl([]));
      const searchFormControl = new FormControl([]);
      searchFormControl.setValue('');
      this.preferencesFormGroup.addControl(tagType.name + 'Search', searchFormControl);
    });
    console.log(this.preferencesFormGroup);
  }

  createDropdown() {
    const newTagType: TagType = { id: null, code: null, name: this.newPreference, tags: null };
    this.store.dispatch(TagTypeActions.createTagType({ tagType: newTagType }));
    this.switchVisible();
  }

  clearFilter(tagTypeName: string): void {
    const searchControlName = tagTypeName.concat('Search');
    this.preferencesFormGroup.controls[searchControlName].setValue('');
  }

  createNewTag(tagTypeName: string) {
    console.log('open modal component');
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-18 21:42:38

问题是变量"noMoreOption“-really你需要一个布尔值数组,而不仅仅是一个简单的变量-。但比创建数组更好的是,让这个角度为你工作。

你可以用<ng-container *ngIf="what-ever as items">的方式让它成为using *ngIf to store a variable

因此,您可以使用类似(*)的代码

代码语言:javascript
复制
 <ng-container *ngIf="tagType.tags | contains : 
                       preferencesFormGroup.controls[tagType.name+'Search'].value 
                as items">
    <mat-option *ngFor="let tag of items"
              [value]="tag.name">{{tag.name}}
    </mat-option>
    <mat-option *ngIf="items.length==0"
                (click)="createNewTag(tagType.name)">
           <i>Didn't find the tag you want? Click here to create your own.</i>
    </mat-option>
<ng-container>

(*)假设您的管道contains在不匹配时返回一个空数组,而不是null

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70024160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档