Guest User

Untitled

a guest
Dec 29th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. private departments: any[] = [
  2. { id: 1, name: 'department1' },
  3. { id: 2, name: 'department2' },
  4. { id: 3, name: 'department3' }
  5. { id: 4, name: 'department4' },
  6. { id: 5, name: 'department5' },
  7. { id: 6, name: 'department6' }
  8. ];
  9.  
  10. private employees: any[] = {
  11. {
  12. name: 'employee1',
  13. department: [2, 3, 4],
  14. email: [ 'bla@gmail.com', 'bar@gmail.com', 'foo@hotmail.com'
  15. ]
  16. }
  17. }
  18.  
  19. stateGroups: StateGroup[] = [{
  20. name:"employee1",
  21. grouping:[{
  22. label: 'email',
  23. names: [ 'bla@gmail.com', 'bar@gmail.com', 'foo@hotmail.com'
  24. ]
  25. },
  26. {
  27. label: 'Department',
  28. names: ['department2', 'department3', 'department4']
  29. }]
  30. }];
  31.  
  32. import {Component, OnInit} from '@angular/core';
  33. import {FormBuilder, FormGroup} from '@angular/forms';
  34. import {Observable} from 'rxjs';
  35. import {startWith, map} from 'rxjs/operators';
  36. export interface StateGroup {
  37. name:string,
  38. grouping: {label:string; names: string[]}[]
  39. }
  40.  
  41. export const _filter = (opt: string[], value: string): string[] => {
  42. const filterValue = value.toLowerCase();
  43.  
  44. return opt.filter(item => item.toLowerCase().indexOf(filterValue) === 0);
  45. };
  46.  
  47. @Component({
  48. selector: 'autocomplete-optgroup-example',
  49. templateUrl: './autocomplete-optgroup-example.html',
  50. styleUrls: ['./autocomplete-optgroup-example.css'],
  51. })
  52.  
  53. export class AutocompleteOptgroupExample implements OnInit {
  54. stateForm: FormGroup = this.fb.group({
  55. stateGroup: '',
  56. });
  57.  
  58. stateGroups: StateGroup[] = [{
  59. name:"employee1",
  60. grouping:[{
  61. label: 'email',
  62. names: [ 'bla@gmail.com', 'bar@gmail.com', 'foo@hotmail.com'
  63. ]
  64. },
  65. {
  66. label: 'Department',
  67. names: ['department1', 'department2', 'department3']
  68. }]
  69. }];
  70.  
  71. private employees: any[] = [
  72. {
  73. name: 'employee1',
  74. department: [2, 3, 4],
  75. mail: [ 'bla@gmail.com', 'bar@gmail.com', 'foo@hotmail.com'
  76. ]
  77. }
  78. ];
  79.  
  80. private departments: any[] = [
  81. { id: 1, name: 'department1' },
  82. { id: 2, name: 'department2' },
  83. { id: 3, name: 'department3' },
  84. { id: 4, name: 'department4' },
  85. { id: 5, name: 'department5' },
  86. { id: 6, name: 'department6' },
  87. { id: 8, name: 'department7' },
  88. { id: 9, name: 'department8' },
  89. ];
  90. </code>
  91. stateGroupOptions: Observable<StateGroup[]>;
  92.  
  93. constructor(private fb: FormBuilder) {}
  94.  
  95.  
  96. <code>
  97. ngOnInit() {
  98. this.stateGroupOptions = this.stateForm.get('stateGroup')!.valueChanges
  99. .pipe(
  100. startWith(''),
  101. map(value => this._filterGroup(value))
  102. );
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. private _filterGroup(value: string): any[] {
  110. if (value) {
  111. return this.stateGroups[0].grouping
  112. .map(group => ({label: group.label, names: _filter(group.names, value)}))
  113. .filter(group => group.names.length > 0);
  114. }
  115.  
  116. return this.stateGroups[0].grouping;
  117. }
  118. }
  119.  
  120.  
  121. <form [formGroup]="stateForm">
  122. <mat-form-field>
  123. <input type="text" matInput placeholder="States Group" formControlName="stateGroup" required [matAutocomplete]="autoGroup">
  124. <mat-autocomplete #autoGroup="matAutocomplete">
  125. <mat-optgroup *ngFor="let group of stateGroupOptions | async" [label]="group.label">
  126. <mat-option *ngFor="let name of group.names" [value]="name">
  127. {{name}}
  128. </mat-option>
  129. </mat-optgroup>
  130. </mat-autocomplete>
  131. </mat-form-field>
  132. </form>
Add Comment
Please, Sign In to add comment