简体   繁体   English

角材质选择多个显示值

[英]Angular Material select multiple displayed value

Is it possible to change displayed value in Angular Material multiple select? 是否可以在“角度材质”多重选择中更改显示值? How can I do this eg Products:(0/12) depends on number of selected items. 我该怎么做,例如Products:(0/12)取决于所选项目的数量。

Use a <mat-select-trigger> for that. 为此使用<mat-select-trigger> For example: 例如:

<mat-form-field>
  <mat-select placeholder="Toppings" [formControl]="toppings" multiple>
    <mat-select-trigger>
      {{toppings.value ? toppings.value[0] : ''}}
      <span *ngIf="toppings.value?.length > 1" class="example-additional-selection">
        (+{{toppings.value.length - 1}} {{toppings.value?.length === 2 ? 'other' : 'others'}})
      </span>
    </mat-select-trigger>
    <mat-option *ngFor="let topping of toppingList" [value]="topping">{{topping}}</mat-option>
  </mat-select>
</mat-form-field>

See the demo here: https://stackblitz.com/angular/qkjbojyxebly?file=app%2Fselect-custom-trigger-example.html 在此处查看演示: https : //stackblitz.com/angular/qkjbojyxebly?file=app%2Fselect-custom-trigger-example.html

In addition to the answer above, to set the selection to an initial value, this is how you'd extend select-custom-trigger-example.ts in the given stackblitz demo: 除了上述答案之外,要将选择设置为初始值,这是在给定的stackblitz演示中扩展select-custom-trigger-example.ts的方法:

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

/** @title Select with custom trigger text */
@Component({
  selector: 'select-custom-trigger-example',
  templateUrl: 'select-custom-trigger-example.html',
  styleUrls: ['select-custom-trigger-example.css'],
})
export class SelectCustomTriggerExample implements OnInit {
  toppings = new FormControl();

  toppingList: string[] = ['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage', 'Tomato'];

  ngOnInit()
  {
    // initalSelection could be the result from a service call (database, backend, etc.):
    let initalSelection : string [] = ["Onion","Pepperoni","Sausage"];

    // set initalSelection as inital value:
    this.toppings.setValue(initalSelection);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM