1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
import { FormBuilder, FormGroup, Validators, FormArray, FormControl, } from '@angular/forms'
public fb = new FormBuilder() public form: FormGroup = this.fb.group({ all: [''], list: this.fb.array([]), beizhu: [''], status: [''], })
public addItem = (title, id) => { this.title = title const formArray: FormArray = this.form.get('list') as FormArray const formGroup: FormGroup = this.fb.group({ ID: [id], title: [title], zongjine: [0, Validators.required], qzcd: [0, Validators.required], bzcd: [0, Validators.required], lwcd: [0, Validators.required], bxcd: [0, Validators.required], }) formArray.push(formGroup)
this.formValueChange() }
public getItemTotal = () => { const formArray: FormArray = this.form.get('list') as FormArray if (formArray.controls.length) { formArray.controls.map((group: FormGroup) => { const insuranceOrder: FormControl = group.get('bxcd') as FormControl const laborOrder: FormControl = group.get('lwcd') as FormControl const qzOrder: FormControl = group.get('qzcd') as FormControl const teamOrder: FormControl = group.get('bzcd') as FormControl const total: FormControl = group.get('zongjine') as FormControl const sum: number = ( insuranceOrder.value + laborOrder.value + qzOrder.value + teamOrder.value).toPrecision(4) total.setValue(sum) }) } }
public getAllTotal = (formArray: FormArray, formControlName: string) => { let sum: number = 0 if (formArray.controls.length) { formArray.controls.map((group: FormGroup) => { const formControl: FormControl = group.get( formControlName ) as FormControl sum += formControl.value }) }
switch (formControlName) { case 'bxcd': this.all.bxcd = sum break case 'lwcd': this.all.lwcd = sum break case 'qzcd': this.all.qzcd = sum break case 'bzcd': this.all.bzcd = sum break case 'zongjine': this.all.total = sum break default: break } }
public formValueChange = () => {
const formArray: FormArray = this.form.get('list') as FormArray if (formArray.controls.length) { formArray.controls.map((group: FormGroup) => { const insuranceOrder: FormControl = group.get('bxcd') as FormControl const laborOrder: FormControl = group.get('lwcd') as FormControl const qzOrder: FormControl = group.get('qzcd') as FormControl const teamOrder: FormControl = group.get('bzcd') as FormControl const total: FormControl = group.get('zongjine') as FormControl insuranceOrder.valueChanges.subscribe(() => { this.getItemTotal() this.getAllTotal(formArray, 'bxcd') }) laborOrder.valueChanges.subscribe(() => { this.getItemTotal() this.getAllTotal(formArray, 'lwcd') }) qzOrder.valueChanges.subscribe(() => { this.getItemTotal() this.getAllTotal(formArray, 'qzcd') }) teamOrder.valueChanges.subscribe(() => { this.getItemTotal() this.getAllTotal(formArray, 'bzcd') }) total.valueChanges.subscribe(() => { this.getAllTotal(formArray, 'zongjine') }) }) } }
|