Move the FormControl declaration inside the component

This commit is contained in:
mauriziovitale84
2016-10-11 10:43:35 +01:00
parent c65ebbcb29
commit 1783e0d2c9
3 changed files with 23 additions and 15 deletions

View File

@@ -50,7 +50,7 @@
</div> </div>
</div> </div>
<br><br> <br><br>
<button type="submit" class="mdl-button mdl-js-button mdl-button--fab" (click)="createReport()" > <button type="submit" class="mdl-button mdl-js-button mdl-button--fab" (click)="showReport()" >
<i class="material-icons">refresh</i> <i class="material-icons">refresh</i>
</button> </button>
<div *ngIf="debug"> <div *ngIf="debug">

View File

@@ -20,8 +20,7 @@ import { AlfrescoTranslationService } from 'ng2-alfresco-core';
import { AnalyticsService } from '../services/analytics.service'; import { AnalyticsService } from '../services/analytics.service';
import { ReportModel, ReportQuery, ParameterValueModel, ReportParameterModel } from '../models/report.model'; import { ReportModel, ReportQuery, ParameterValueModel, ReportParameterModel } from '../models/report.model';
import { Chart } from '../models/chart.model'; import { Chart } from '../models/chart.model';
import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { FormGroup, FormBuilder } from '@angular/forms';
import * as moment from 'moment';
@Component({ @Component({
moduleId: module.id, moduleId: module.id,
@@ -63,12 +62,8 @@ export class AnalyticsComponent implements OnInit, OnChanges {
} }
ngOnInit() { ngOnInit() {
let today = moment().format('YYYY-MM-DD');
this.reportForm = this.formBuilder.group({ this.reportForm = this.formBuilder.group({
dateRange: this.formBuilder.group({ dateRange: new FormGroup({})
startDate: [today, Validators.required],
endDate: [today, Validators.required]
})
}); });
} }
@@ -86,33 +81,36 @@ export class AnalyticsComponent implements OnInit, OnChanges {
(res: ReportModel) => { (res: ReportModel) => {
this.reportDetails = res; this.reportDetails = res;
if (this.reportDetails.hasParameters()) { if (this.reportDetails.hasParameters()) {
this.retriveParameterOptions(this.reportDetails.definition.parameters); this.retrieveParameterOptions(this.reportDetails.definition.parameters);
} else {
this.onSuccess.emit(res);
} }
this.onSuccess.emit(res);
}, },
(err: any) => { (err: any) => {
this.onError.emit(err);
console.log(err); console.log(err);
this.onError.emit(err);
}, },
() => console.log('Login done') () => console.log('retrive done')
); );
} }
private retriveParameterOptions(parameters: ReportParameterModel[]) { private retrieveParameterOptions(parameters: ReportParameterModel[]) {
parameters.forEach((param) => { parameters.forEach((param) => {
this.analyticsService.getParamValuesByType(param.type).subscribe( this.analyticsService.getParamValuesByType(param.type).subscribe(
(opts: ParameterValueModel[]) => { (opts: ParameterValueModel[]) => {
param.options = opts; param.options = opts;
this.onSuccess.emit(this.reportDetails);
}, },
(err: any) => { (err: any) => {
console.log(err); console.log(err);
this.onError.emit(err);
}, },
() => console.log(`${param.type} options loaded`) () => console.log(`${param.type} options loaded`)
); );
}); });
} }
public createReport() { public showReport() {
this.analyticsService.getReportsByParams(this.reportDetails.id, this.reportParamQuery).subscribe( this.analyticsService.getReportsByParams(this.reportDetails.id, this.reportParamQuery).subscribe(
(res: Chart[]) => { (res: Chart[]) => {
this.reports = res; this.reports = res;

View File

@@ -16,7 +16,7 @@
*/ */
import { Component, Input, Output, EventEmitter, ViewChild, ElementRef } from '@angular/core'; import { Component, Input, Output, EventEmitter, ViewChild, ElementRef } from '@angular/core';
import { AbstractControl, FormGroup, FormBuilder } from '@angular/forms'; import { AbstractControl, FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
import { WidgetComponent } from './../widget.component'; import { WidgetComponent } from './../widget.component';
import * as moment from 'moment'; import * as moment from 'moment';
@@ -78,6 +78,16 @@ export class DateRangeWidget extends WidgetComponent {
} }
initForm() { initForm() {
let today = moment().format('YYYY-MM-DD');
let startDateControl = new FormControl(today);
startDateControl.setValidators(Validators.required);
this.dateRange.addControl('startDate', startDateControl);
let endDateControl = new FormControl(today);
endDateControl.setValidators(Validators.required);
this.dateRange.addControl('endDate', endDateControl);
this.dateRange.setValidators(dateCheck); this.dateRange.setValidators(dateCheck);
this.dateRange.valueChanges.subscribe(data => this.onGroupValueChanged(data)); this.dateRange.valueChanges.subscribe(data => this.onGroupValueChanged(data));
} }