|
| 1 | +import {AfterContentInit, AfterViewInit, Component, Inject, OnDestroy, OnInit, ViewChild} from '@angular/core'; |
| 2 | +import { |
| 3 | + ForeignFieldLookupComponent, ForeignFieldLookupConfig, ForeignFieldLookupResult, |
| 4 | + ForeignFieldLookupComponentData |
| 5 | +} from 'django-angular-dynamic-forms'; |
| 6 | +import { |
| 7 | + MAT_DIALOG_DATA, MatDialogRef, MatPaginator, MatSort, MatTableDataSource, PageEvent, |
| 8 | + Sort |
| 9 | +} from '@angular/material'; |
| 10 | +import {FormBuilder, FormGroup} from '@angular/forms'; |
| 11 | +import {Observable} from 'rxjs/Observable'; |
| 12 | +import {debounceTime, distinctUntilChanged, mergeMap, tap} from 'rxjs/operators'; |
| 13 | +import {map} from 'rxjs/operators'; |
| 14 | +import 'rxjs/observable/merge'; |
| 15 | +import {of as observableOf} from 'rxjs/observable/of'; |
| 16 | +import {combineLatest} from 'rxjs/observable/combineLatest'; |
| 17 | +import {HttpClient} from '@angular/common/http'; |
| 18 | +import {Subscription} from 'rxjs/Subscription'; |
| 19 | + |
| 20 | +@Component({ |
| 21 | + selector: 'app-foreign-selector', |
| 22 | + template: |
| 23 | + `Write a few letters of the city name and then select the city. |
| 24 | + You can also |
| 25 | + <button mat-button (click)="clear()">clear</button> the previous value: |
| 26 | +
|
| 27 | + <mat-table [dataSource]="dataSource" matSort> |
| 28 | +
|
| 29 | + <ng-container matColumnDef="name"> |
| 30 | + <mat-header-cell *matHeaderCellDef mat-sort-header> |
| 31 | + <form [formGroup]="form"> |
| 32 | + <mat-input-container> |
| 33 | + <input formControlName="query" matInput placeholder="City name filter"> |
| 34 | + </mat-input-container> |
| 35 | + </form> |
| 36 | + </mat-header-cell> |
| 37 | + <mat-cell *matCellDef="let row"> {{row.name}}</mat-cell> |
| 38 | + </ng-container> |
| 39 | +
|
| 40 | + <ng-container matColumnDef="zipcode"> |
| 41 | + <mat-header-cell *matHeaderCellDef mat-sort-header> ZIP Code</mat-header-cell> |
| 42 | + <mat-cell *matCellDef="let row"> {{row.zipcode}}</mat-cell> |
| 43 | + </ng-container> |
| 44 | +
|
| 45 | + <ng-container matColumnDef="comment"> |
| 46 | + <mat-header-cell *matHeaderCellDef mat-sort-header> Comment</mat-header-cell> |
| 47 | + <mat-cell *matCellDef="let row"> {{row.comment}}</mat-cell> |
| 48 | + </ng-container> |
| 49 | +
|
| 50 | + <ng-container matColumnDef="action"> |
| 51 | + <mat-header-cell *matHeaderCellDef mat-sort-header></mat-header-cell> |
| 52 | + <mat-cell *matCellDef="let row"> |
| 53 | + <button mat-button (click)="select(row)">select this</button> |
| 54 | + </mat-cell> |
| 55 | + </ng-container> |
| 56 | +
|
| 57 | + <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row> |
| 58 | + <mat-row *matRowDef="let row; columns: displayedColumns;"> |
| 59 | + </mat-row> |
| 60 | + </mat-table> |
| 61 | +
|
| 62 | + <mat-paginator [pageSizeOptions]="[5, 10]" [length]="itemCount"></mat-paginator> |
| 63 | + `, |
| 64 | + styles: [ |
| 65 | + `table { |
| 66 | + max-width: 300px; |
| 67 | + width: 100%; |
| 68 | + border-collapse: collapse; |
| 69 | + }` |
| 70 | + ] |
| 71 | +}) |
| 72 | +export class ForeignSelectorComponent implements OnInit, ForeignFieldLookupComponent, AfterViewInit, OnDestroy { |
| 73 | + public form: FormGroup; |
| 74 | + public displayedColumns = ['name', 'zipcode', 'comment', 'action']; |
| 75 | + public dataSource = new MatTableDataSource<any>([]); |
| 76 | + public itemCount = 0; |
| 77 | + |
| 78 | + @ViewChild(MatPaginator) paginator: MatPaginator; |
| 79 | + @ViewChild(MatSort) sort: MatSort; |
| 80 | + private subscription: Subscription; |
| 81 | + |
| 82 | + |
| 83 | + constructor(public dialogRef: MatDialogRef<ForeignFieldLookupComponent>, |
| 84 | + @Inject(MAT_DIALOG_DATA) public data: ForeignFieldLookupComponentData, |
| 85 | + formBuilder: FormBuilder, |
| 86 | + private http: HttpClient) { |
| 87 | + |
| 88 | + this.form = formBuilder.group({query: []}); |
| 89 | + } |
| 90 | + |
| 91 | + ngAfterViewInit(): void { |
| 92 | + |
| 93 | + console.log(this.paginator, this.sort); |
| 94 | + |
| 95 | + const filter$ = Observable.merge( |
| 96 | + this.form.valueChanges.pipe( |
| 97 | + map((val) => val.query), |
| 98 | + debounceTime(500), |
| 99 | + ), |
| 100 | + observableOf('') |
| 101 | + ); |
| 102 | + |
| 103 | + const paginator$ = Observable.merge( |
| 104 | + this.paginator.page, |
| 105 | + // default |
| 106 | + observableOf({ |
| 107 | + pageIndex: 0, |
| 108 | + pageSize: 5 |
| 109 | + })) as Observable<PageEvent>; |
| 110 | + |
| 111 | + const sort$ = Observable.merge( |
| 112 | + this.sort.sortChange, |
| 113 | + // default |
| 114 | + observableOf({ |
| 115 | + active: '', |
| 116 | + direction: '' |
| 117 | + })) as Observable<Sort>; |
| 118 | + |
| 119 | + this.subscription = combineLatest(filter$, paginator$, sort$).pipe( |
| 120 | + distinctUntilChanged(), |
| 121 | + map((x) => ({ |
| 122 | + filter: x[0], |
| 123 | + paginator: x[1], |
| 124 | + sort: x[2] |
| 125 | + })), |
| 126 | + // normally you would use a service here ... |
| 127 | + mergeMap((opts) => this.http.get<any>(this.data.config.autocompleteUrl, { |
| 128 | + params: { |
| 129 | + query: opts.filter, |
| 130 | + pageIndex: opts.paginator.pageIndex.toString(), |
| 131 | + pageSize: opts.paginator.pageSize.toString(), |
| 132 | + sortBy: opts.sort.active, |
| 133 | + sortDirection: opts.sort.direction |
| 134 | + } |
| 135 | + }) |
| 136 | + ) |
| 137 | + ).subscribe((cities) => { |
| 138 | + this.dataSource = new MatTableDataSource<any>(cities.items); |
| 139 | + this.itemCount = cities.length; |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + ngOnInit() { |
| 144 | + } |
| 145 | + |
| 146 | + ngOnDestroy(): void { |
| 147 | + this.subscription.unsubscribe(); |
| 148 | + } |
| 149 | + |
| 150 | + select(city: any) { |
| 151 | + const result: ForeignFieldLookupResult[] = [ |
| 152 | + { |
| 153 | + formatted_value: city.name, |
| 154 | + key: city.id |
| 155 | + } |
| 156 | + ]; |
| 157 | + this.dialogRef.close(result); |
| 158 | + } |
| 159 | + |
| 160 | + clear() { |
| 161 | + this.dialogRef.close([]); |
| 162 | + } |
| 163 | +} |
0 commit comments