Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. [ ] Check all
  2. ------------------------------------------
  3. [ ] This is email subject #1
  4. [ ] This is email subject #2
  5. [ ] ...
  6.  
  7. <form [formGroup]="form">
  8. <div><input formControlName="toggleAll" type="checkbox"></div>
  9.  
  10. <div>
  11. <ul formArrayName="checkMailList">
  12. <li *ngFor="let mail of mails; let i=index">
  13. <input type="checkbox" [formControlName]="i">
  14. <div>mail.subject</div>
  15. </li>
  16. </ul>
  17. </div>
  18. </form>
  19.  
  20. @Component({ ... })
  21. export class MailListComponent implements OnChanges {
  22. @Input() mails: Mail[];
  23.  
  24. private get checkMailList(): FormArray { return this.form.get('checkMailList') as FormArray; }
  25.  
  26. constructor() {
  27. this.form = new FormGroup({
  28. checkMailList = new FormArray([]);
  29. });
  30. }
  31.  
  32. ngOnChanges(changes: SimpleChanges) {
  33. if (!changes['mails'] || !changes['mails'].currentValue) {
  34. return;
  35. }
  36.  
  37. // remove array first from form, as we will get mails again when anything updates
  38. if (this.checkMailList.length > 0) {
  39. this.form.removeControl('checkMailList');
  40. this.form.addControl('checkMailList', new FormArray([]));
  41. }
  42.  
  43. this.mails.forEach(m => {
  44. this.checkMailList.push(new FormControl());
  45. });
  46.  
  47. this.form
  48. .valueChanges
  49. .pluck('toggleAll')
  50. .distinctUntilChanged()
  51. .subscribe(selectAll => {
  52. if (selectAll) {
  53. this.checkMailList.setValue(this.mails.map(_ => true));
  54. } else {
  55. this.checkMailList.reset();
  56. }
  57. });
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement