4

如何在我的 Angular 2 应用程序中通过分组实现多选下拉?我需要像这个问题中链接的图像一样的下拉菜单如何在 Angular2 的 ng-select 中实现分组?. 请帮帮我.....从最近几天开始,我一直坚持这一点

我试过像下面这样的angular-ngselect,但是它:

组件 HTML:

<form [formGroup]="form" class="selector-form">
    <div class="marTop20">
        <ng-select [options]="options1"
                   [multiple]="multiple1"
                   placeholder="Select multiple"
                   formControlName="selectMultiple"
                   (opened)="onMultipleOpened()"
                   (closed)="onMultipleClosed()"
                   (selected)="onMultipleSelected($event)"
                   (deselected)="onMultipleDeselected($event)">
        </ng-select>
    </div>
</form>

ts文件中的组件代码:

export class FilterClientSelectorComponent implements OnInit {
    form: FormGroup;
    multiple1: boolean = true;
    options1: Array<any> = [];
    selection: Array<string>;
    @ViewChild('preMultiple') preMultiple;
    logMultipleString: string = '';

    constructor() {
        let numOptions = 100;
        let opts = new Array(numOptions);
        for (let i = 0; i < numOptions; i++) {
            opts[i] = {
                value: i.toString(),
                label: i.toString(),
                groupname:'a'
            };
        }
        this.options1 = opts.slice(0);
    }
    ngOnInit() {
        this.form = new FormGroup({});
        this.form.addControl('selectMultiple', new FormControl(''));
    }
    private scrollToBottom(elem) {
        elem.scrollTop = elem.scrollHeight;
    }
}

它给了我多个选择下拉列表,但没有分组:

电流输出:

在此处输入图像描述

所需输出:

在此处输入图像描述

4

5 回答 5

2

试试这个带有许多属性和功能的primeng 的多选下拉菜单可能会对您有所帮助。

http://www.primefaces.org/primeng/#/multiselect

http://www.primefaces.org/primeng/#/picklist

于 2016-12-08T17:59:48.833 回答
2

我已经使用材料设计来实现相同的功能..这就是我做到的..在“fileName.component.html”中

 <mat-form-field>
  <mat-select placeholder="Pokemon" [formControl]="pokemonControl" multiple>
    <mat-option>-- None --</mat-option>
    <mat-optgroup *ngFor="let group of pokemonGroups" [label]="group.name"
                  [disabled]="group.disabled">
      <mat-option *ngFor="let pokemon of group.pokemon" [value]="pokemon.value">
        {{pokemon.viewValue}}
      </mat-option>
    </mat-optgroup>
  </mat-select>
</mat-form-field>

不要忘记添加“多个” ..您可以在我的 .html 文件的第二行中看到它。在 “fileName.component.ts”中

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

export interface Pokemon {
  value: string;
  viewValue: string;
}

export interface PokemonGroup {
  disabled?: boolean;
  name: string;
  pokemon: Pokemon[];
}

/** @title Select with option groups */
@Component({
  selector: 'select-optgroup-example',
  templateUrl: 'select-optgroup-example.html',
  styleUrls: ['select-optgroup-example.css'],
})
export class SelectOptgroupExample {
  pokemonControl = new FormControl();
  pokemonGroups: PokemonGroup[] = [
    {
      name: 'Grass',
      pokemon: [
        {value: 'bulbasaur-0', viewValue: 'Bulbasaur'},
        {value: 'oddish-1', viewValue: 'Oddish'},
        {value: 'bellsprout-2', viewValue: 'Bellsprout'}
      ]
    },
    {
      name: 'Water',
      pokemon: [
        {value: 'squirtle-3', viewValue: 'Squirtle'},
        {value: 'psyduck-4', viewValue: 'Psyduck'},
        {value: 'horsea-5', viewValue: 'Horsea'}
      ]
    },
    {
      name: 'Fire',
      disabled: true,
      pokemon: [
        {value: 'charmander-6', viewValue: 'Charmander'},
        {value: 'vulpix-7', viewValue: 'Vulpix'},
        {value: 'flareon-8', viewValue: 'Flareon'}
      ]
    },
    {
      name: 'Psychic',
      pokemon: [
        {value: 'mew-9', viewValue: 'Mew'},
        {value: 'mewtwo-10', viewValue: 'Mewtwo'},
      ]
    }
  ];
}

如果您想了解更多信息,请查看此链接

于 2019-02-11T08:46:50.667 回答
1

您可以使用angular-2-dropdown-multiselect,它也有组选项。

你必须根据这个格式化数据

    // Labels / Parents
myOptions: IMultiSelectOption[] = [
    { id: 1, name: 'Car brands', isLabel: true },
    { id: 2, name: 'Volvo', parentId: 1 },
    { id: 3, name: 'Honda', parentId: 1 },
    { id: 4, name: 'BMW', parentId: 1 },
    { id: 5, name: 'Colors', isLabel: true },
    { id: 6, name: 'Blue', parentId: 5 },
    { id: 7, name: 'Red', parentId: 5 },
    { id: 8, name: 'White', parentId: 5 }
];

https://plnkr.co/edit/3Ett6sL2emzyrM3YiHkP?p=preview

https://github.com/softsimon/angular-2-dropdown-multiselect

于 2017-12-04T04:19:58.127 回答
0

我使用 Angular 最常用的 UI 组件创建了两个演示

使用角度材料,您可以查看此演示 https://stackblitz.com/edit/moa-option-group-multi-select?file=src%2Fapp%2Fselect-optgroup-example.html

使用primeng,您还可以查看此演示 https://stackblitz.com/edit/moa-primeng-multiselect-grouped

于 2021-05-04T15:54:33.420 回答
0

这是 Angular 的用于 Multiselect-dropdwon 的轻量级库。它提供了广泛的功能,如 Group-By、搜索、自定义搜索、虚拟滚动等。您可以参考:https ://cuppalabs.github.io/angular2-multiselect-dropdown/#/groupby

于 2019-12-27T10:48:58.010 回答