course-fields-select.ts 2.21 KB
Newer Older
1
import { Component, OnInit } from '@angular/core';
2 3
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
4 5
import { Injectable } from "@angular/core";
import { CourseFieldsActions } from '../../actions/coursefields.actions';
6
import { DevToolsExtension, NgRedux, select } from 'ng2-redux';
7
import { ICourseFields } from '../../store/coursefields/coursefields.types';
8
import { IAppState } from '../../store/store';
9 10 11 12 13 14 15 16 17 18 19 20 21 22

import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray
} from '@angular/forms';
import {AppSettings} from '../../app.settings';

@Component({
    selector: 'course-fields-select',
    template: `
     <form [formGroup]="formGroup">
        <div formArrayName="formArray">
23
            <div *ngFor="let courseField$ of courseFields$ | async; let i=index">
24
            <div class="row">
25
            <div class="col-md-1">
26 27
                <input type="checkbox" formControlName="{{i}}">
            </div>
28
            <div class="col-md-11 pull-left">
29
                {{courseField$.name}}
30 31 32 33
            </div>
            </div>
            </div>
        </div>
34 35 36 37 38
        <div class="row">
        <div class="col-md-2 col-md-offset-5">
            <button class="btn-primary btn-lg pull-center" (click)="saveSelected()">Συνέχεια</button>
        </div>
        </div>
39
    </form>
40
<!--    <pre>{{formGroup.value | json}}</pre> -->
41 42 43
  `
})
@Injectable() export default class CourseFieldsSelect implements OnInit {
44 45
    private courseFields$: Observable<ICourseFields>;

46 47 48
    public formGroup: FormGroup;
    public cfs = new FormArray([]);

49
    constructor(private http: Http, private fb: FormBuilder, private _cfa: CourseFieldsActions, private _ngRedux: NgRedux<IAppState>) {
50 51 52 53 54 55
        this.formGroup = this.fb.group({
            formArray: this.cfs
        });
    };

    ngOnInit() {
56
        this._cfa.getCourseFields();
57

58 59
        this.courseFields$ = this._ngRedux.select(state => {
            for (let courseField in state.courseFields) {
60
                this.cfs.push(new FormControl('', []));
61 62
            }
            return state.courseFields;
63 64 65 66 67 68
        });

    }

    saveSelected() {
        this._cfa.saveCourseFieldsSelected(this.formGroup.value.formArray);
69
    }
70
}