javascript - Search filter with regex stuck my search filter and It does work without refresh? -
i searching patient through search filter.it worked in simple case when search sabir\\
stuck , after removing searchtext sabir\\
not work.it doesn't generate request next time.how handle sabir\\
in angular2. in advance here code snippet url work , not work? work "http://172.16.1.101:8080/openmrs/ws/rest/v1/patient?q=sabir&v=full" not work "http://172.16.1.101:8080/openmrs/ws/rest/v1/patient?q=\\&v=full"
i using function handling url.
validurl(str):boolean { var pattern = new regexp('^(https?:\/\/)?'+ // protocol '((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|'+ // domain name '((\d{1,3}\.){3}\d{1,3}))'+ // or ip (v4) address '(\:\d+)?(\/[-a-z\d%_.~+]*)*'+ // port , path '(\?[;&a-z\d%_.~+=-]*)?'+ // query string '(\#[-a-z\d_]*)?$','i'); // fragment locater if(!pattern.test(str)) { return false; } else { return true; } }
service method searchpatient
searchpatient(term: string): observable<patient[]> { if (!term) { return observable.of<patient[]>([]); } const search_patient_url = `${this.webapiurl}${this.methodname}?q=${term}&v=full`; console.log(search_patient_url); this.apputilityservice.validurl(search_patient_url); console.log(search_patient_url); return this.http.get(search_patient_url, this.options) .map(response => response.json().results patient[]) .do(data => console.log('patients', data)) .catch(this.handleerror);
}
and here component code handling search term.
search(terms: string): void { this.searcherror = null; if(terms.length <= 2) { this.patient_results = null; return; } if(terms) this.loading = true; this.patient_search_terms.next(terms); } loading:boolean = false; ngoninit(): void { this.patient_search_terms .debouncetime(300) // wait 300ms after each keystroke before considering term .switchmap(term => { /*if(!this.apputilityservice.validurl(term)) { console.log("invalid url"); term = null; } */ return term // switch new observable each time term changes // return http search observable ? this.patientservice.searchpatient(term) // or observable of empty patients if there no search term : observable.of(null)}) .catch(error => { // todo: add real error handling console.log(error); this.loading = false; return observable.of(null); }).subscribe(res=>{ this.loading = false; if(!res || res.length === 0) { this.searcherror = "no result found"; this.patient_results = null; return; } this.searcherror = null; this.patient_results = observable.of(res); }) this.searchform.valuechanges.subscribe(data=>{ this.searchtext = data; if(!this.searchtext || this.searchtext == "") { this.clearsearch(); return; } this.search(this.searchtext); }); } clearsearch():void { this.patient_results = null; this.loading = false; this.searcherror = null; this.searchtext = null; } searchkeyup(event):void { if(!this.searchtext || this.searchtext == "") this.clearsearch(); } }
Comments
Post a Comment