沈斌
2017-12-15 f9b157566af34b8dc28ba10b34d025ac04f3168b
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 { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormArray } from '@angular/forms';
 
@Component({
    selector: 'app-advanced-form',
    templateUrl: './advanced-form.component.html'
})
export class AdvancedFormComponent implements OnInit {
    editIndex = -1;
    editObj = {};
 
    form: FormGroup;
    users: any[] = [
        { value: 'xiao', label: '付晓晓' },
        { value: 'mao', label: '周毛毛' }
    ];
 
    constructor(private fb: FormBuilder) {}
 
    ngOnInit() {
        this.form = this.fb.group({
            name: [null, [Validators.required]],
            url: [null, [Validators.required]],
            owner: [undefined, [Validators.required]],
            approver : [null, [Validators.required]],
            time_start : [null, [Validators.required]],
            time_end : [null, [Validators.required]],
            type : [null, [Validators.required]],
            name2 : [null, [Validators.required]],
            summary : [null, [Validators.required]],
            owner2 : [null, [Validators.required]],
            approver2 : [null, [Validators.required]],
            time : [null, [Validators.required]],
            type2 : [null, [Validators.required]],
            items: this.fb.array([])
        });
        const userList = [
            {
                key: '1',
                workId: '00001',
                name: 'John Brown',
                department: 'New York No. 1 Lake Park',
            }, {
                key: '2',
                workId: '00002',
                name: 'Jim Green',
                department: 'London No. 1 Lake Park',
            }, {
                key: '3',
                workId: '00003',
                name: 'Joe Black',
                department: 'Sidney No. 1 Lake Park',
            }
        ];
        userList.forEach(i => {
            const field = this.createUser();
            field.patchValue(i);
            this.items.push(field);
        });
    }
 
    createUser(): FormGroup {
        return this.fb.group({
            key: [ null ],
            workId: [ null, [ Validators.required ] ],
            name: [ null, [ Validators.required ] ],
            department: [ null, [ Validators.required ] ]
        });
    }
 
    //#region get form fields
    get name() { return this.form.controls.name; }
    get url() { return this.form.controls.url; }
    get owner() { return this.form.controls.owner; }
    get approver() { return this.form.controls.approver; }
    get time_start() { return this.form.controls.time_start; }
    get time_end() { return this.form.controls.time_end; }
    get type() { return this.form.controls.type; }
    get name2() { return this.form.controls.name2; }
    get summary() { return this.form.controls.summary; }
    get owner2() { return this.form.controls.owner2; }
    get approver2() { return this.form.controls.approver2; }
    get time() { return this.form.controls.time; }
    get type2() { return this.form.controls.type2; }
    get items() { return this.form.controls.items as FormArray; }
    //#endregion
 
    add() {
        this.items.push(this.createUser());
        this.edit(this.items.length - 1);
    }
 
    del(index: number) {
        this.items.removeAt(index);
    }
 
    edit(index: number) {
        if (this.editIndex !== -1 && this.editObj) {
            this.items.at(this.editIndex).patchValue(this.editObj);
        }
        this.editObj = { ...this.items.at(index).value };
        this.editIndex = index;
    }
 
    save(index: number) {
        this.items.at(index).markAsDirty();
        if (this.items.at(index).invalid) return ;
        this.editIndex = -1;
    }
 
    cancel(index: number) {
        if (!this.items.at(index).value.key) {
            this.del(index);
        } else {
            this.items.at(index).patchValue(this.editObj);
        }
        this.editIndex = -1;
    }
 
    _submitForm() {
        console.log(this.form.value);
        for (const i in this.form.controls) {
            this.form.controls[ i ].markAsDirty();
        }
        if (this.form.invalid) return ;
    }
}