如何自定义垫选择选项组以允许角度嵌套值

时间:2020-07-23 12:03:59

标签: javascript html css angular angular-material

我正在自定义角度材质的选择/自动完成功能,以允许嵌套下拉列表。

在这里,我想让一个父母带很多孩子。如果我展开特定的父级下拉菜单,则只有该下拉菜单的子级才能展开或折叠。同样,应在同一情况下选择复选框事件。

我面临两个问题。

  1. 搜索/自动完成功能不起作用。
  2. 选中父级复选框应选中与其关联的所有子级。

有人可以帮我我在这里想念的东西吗?

这是我的代码。 STACKBLITZ

参考:

https://stackblitz.com/edit/angular-evacck-qubgyy

https://stackblitz.com/angular/eboprqqnooy

export class SelectCustomTriggerExample {
  toppings = new FormControl();
   isExpandCategory: boolean = false;
  toppingList: string[] = ['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage', 'Tomato'];

  states = new FormControl();

  expandDocumentTypes(category) {
    console.log("expanding dropdown", category);
    this.isExpandCategory = !this.isExpandCategory;
  }

  stateList: StateGroup[] = [{
    letter: 'A',
    names: ['Alabama', 'Alaska', 'Arizona', 'Arkansas']
  }, {
    letter: 'C',
    names: ['California', 'Colorado', 'Connecticut']
  }, {
    letter: 'D',
    names: ['Delaware']
  }, {
    letter: 'F',
    names: ['Florida']
  }, {
    letter: 'G',
    names: ['Georgia']
  }, {
    letter: 'H',
    names: ['Hawaii']
  }, {
    letter: 'I',
    names: ['Idaho', 'Illinois', 'Indiana', 'Iowa']
  }, {
    letter: 'K',
    names: ['Kansas', 'Kentucky']
  }, {
    letter: 'L',
    names: ['Louisiana']
  }, {
    letter: 'M',
    names: ['Maine', 'Maryland', 'Massachusetts', 'Michigan',
      'Minnesota', 'Mississippi', 'Missouri', 'Montana']
  }, {
    letter: 'N',
    names: ['Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
      'New Mexico', 'New York', 'North Carolina', 'North Dakota']
  }, {
    letter: 'O',
    names: ['Ohio', 'Oklahoma', 'Oregon']
  }, {
    letter: 'P',
    names: ['Pennsylvania']
  }, {
    letter: 'R',
    names: ['Rhode Island']
  }, {
    letter: 'S',
    names: ['South Carolina', 'South Dakota']
  }, {
    letter: 'T',
    names: ['Tennessee', 'Texas']
  }, {
    letter: 'U',
    names: ['Utah']
  }, {
    letter: 'V',
    names: ['Vermont', 'Virginia']
  }, {
    letter: 'W',
    names: ['Washington', 'West Virginia', 'Wisconsin', 'Wyoming']
  }];
}
<mat-form-field appearance="fill">
  <mat-label>Toppings</mat-label>
  <mat-select [formControl]="states" multiple>
    <mat-select-trigger>
      {{states.value ? states.value[0] : ''}}
      <span *ngIf="states.value?.length > 1" class="example-additional-selection">
        (+{{states.value.length - 1}} {{states.value?.length === 2 ? 'other' : 'others'}})
      </span>
    </mat-select-trigger>

  <mat-optgroup *ngFor="let group of stateList">
          <div>
          <mat-checkbox [checked]="group.selected" (change)="toggleSelection(user)" 
            (click)="$event.stopPropagation()">
                    {{group.letter}}
                </mat-checkbox>
          <button mat-button (click)="expandDocumentTypes(group)">
            <mat-icon>keyboard_arrow_down</mat-icon>
          </button>
          </div>  
          <mat-option *ngFor="let name of group.names" [value]="name"
             [ngClass]="isExpandCategory ? 'list-show' : 'list-hide'">
            {{name}}
          </mat-option>
  </mat-optgroup>

  </mat-select>
</mat-form-field>

选择值后,其外观应如下所示(+1或2 ..其他)。

enter image description here

1 个答案:

答案 0 :(得分:1)

这是您的2分和3分的答案。 https://stackblitz.com/edit/angular-f5mizr-9fpccz

  1. 扩展特定的父项下拉列表时,所有子项都在扩展。 解决
  2. 检查父级复选框应选择与其关联的所有子级。 完成

对于第1点,您可以使用NullValueHandling实现相同的解决方案,因为使用mat-autocomplete无法实现自动完成