argos/dmp-frontend/src/app/form/fields/autocomplete-remote/autocomplete-remote.compone...

98 lines
1.9 KiB
TypeScript
Raw Normal View History

2017-11-10 10:53:01 +01:00
import { Component, OnInit,Input, Output, EventEmitter, forwardRef} from '@angular/core';
import { Validators, ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
2017-11-08 14:33:14 +01:00
import { ServerService } from '../../../services/server.service';
2017-11-08 14:51:16 +01:00
import { UUID } from 'angular2-uuid';
2017-11-08 14:33:14 +01:00
import { NguiAutoCompleteModule } from '@ngui/auto-complete';
2017-11-08 11:35:56 +01:00
declare var $: any;
@Component({
selector: 'autocomplete-remote',
templateUrl: './autocomplete-remote.component.html',
2017-11-10 10:53:01 +01:00
styleUrls: ['./autocomplete-remote.component.css'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => AutocompleteRemoteComponent),
multi: true
}
]
})
2017-11-08 11:35:56 +01:00
2017-11-10 10:53:01 +01:00
export class AutocompleteRemoteComponent implements OnInit, ControlValueAccessor {
2017-11-10 10:53:01 +01:00
id : string = UUID.UUID();
2017-11-08 14:33:14 +01:00
@Input() label : string;
@Input() url : string;
2017-11-10 11:24:20 +01:00
selectedValue : string;
2017-11-08 11:35:56 +01:00
2017-11-08 14:33:14 +01:00
values : any[] = new Array();
2017-11-08 11:35:56 +01:00
2017-11-08 14:33:14 +01:00
query : string = "";
2017-11-08 11:35:56 +01:00
2017-11-10 10:53:01 +01:00
constructor(private serverService :ServerService) {
}
2017-11-08 14:33:14 +01:00
ngOnInit() {
2017-11-10 12:03:28 +01:00
this.updateByQuery(this.query); //just to trigger the first call
2017-11-08 11:35:56 +01:00
}
2017-11-08 14:33:14 +01:00
updateByQuery(query : string){
2017-11-10 11:54:15 +01:00
this.serverService.getThroughProxy(this.url, query).subscribe(
response => {
this.values.length = 0; //clear array; -- this is quite a fast and memory efficient way
response.data.forEach(element => {
this.values.push(element.attributes.name);
});
},
error => {
console.log(error);
}
);
2017-11-10 10:53:01 +01:00
}
onChange: any = () => { };
onTouched: any = () => { };
get value() {
this.value = this.selectedValue;
return this.selectedValue;
}
set value(val) {
this.selectedValue = val;
this.onChange(val);
this.onTouched();
2017-11-08 11:35:56 +01:00
}
2017-11-10 10:53:01 +01:00
registerOnChange(fn) {
this.onChange = fn;
}
registerOnTouched(fn) {
this.onTouched = fn;
}
writeValue(value) {
if (value) {
this.value = value;
}
}
2017-11-08 11:35:56 +01:00
}
2017-11-10 10:53:01 +01:00