2017-11-17 13 views
0

사용자 유형에 따라 필터를 사용하려고하는 목록이 있습니다. 사용자가 유형을 입력하면 검사중인 문자열을 검색하고 두 문자열을 비교하여 문자열이 발견되면 객체를 배열로 푸시 한 다음 필터링 된 객체 정보를 표시합니다. 내가 가지고있는 문제는 검색 필드가 비어 있으면 원래의 변경되지 않은 개체 배열이 표시되어야한다는 것입니다. 대신, 내가 얻은 것은 마지막으로 성공한 반환 된 검색입니다.목록 필터링이 올바르지 않습니다. 검색 문자열이 비어 있습니다.

HTML

<input type="text" class="in-line filter" id="searchByName" placeholder="Enter a name" (keyup)="filterByName($event.target.value)" /> 
<study-results-table [originalArray]="originalArray"></study-results-table> 

TS

ngOnInit(){ 
    originalArray = new Array<any>(); 
    unfiltered = new Array<any>() 
} 

filterByName(searchString){ 
    this.filtered = new Array<any>();   
    _.each(this.originalArray, (result) => { 
     let name = result.firstName.toLowerCase() +" " +result.lastName.toLowerCase(); 
     if(patientName.includes(searchString.toLowerCase())){ 
      this.filtered.push(result); 
     } 
    }) 

    if(searchString === ""){ 
     this.originalArray= this.unfiltered; 
    } 
    this.originalArray= this.filtered; 
} 

사람이이 문제를 해결하는 방법을 설명 할 수?

답변

1

배열의 filter 기능을 사용하지 않는 이유는 무엇입니까? 다음에 코드를 단순화 할 수 있습니다 :

filterByName(searchString){ 
    if(searchString.trim().length==0){ 
     this.originalArray = this.unfiltered; 
     return; 
    }; 
    this.originalArray = this.unfiltered.filter((result)=>{ 
     let name = result.firstName.toLowerCase() +" " +result.lastName.toLowerCase(); 
     return (name.includes(searchString.toLowerCase())); 
    }); 
} 
+0

을 나는이 질문을 게시 한 후에는 바로 필터로 전환,하지만 내가 통해 반복 할 필요가 있기 때문에 내가 문자를 삭제되었고, 그 때 또 다른 문제로 실행중인 발견 당신이 답안에 보여 주듯이, 변경되지 않은 배열이 아닌. – Robert