I'm refactoring my code to consolidate multiple functions into one, but I'm having trouble updating an array passed as a parameter. The function itself is functioning correctly, but the array doesn't update within the class.
In this TypeScript class, the commented-out code in the `onCompanyRenterNameChanged` function works fine. However, the new code below the comment isn't updating the `filteredListOfRenters` array that's being passed as a parameter. Even after running it, the function returns the full list instead of the filtered list, and I can't figure out why.
export class FilterDialogComponent implements OnInit {
filteredListOfRenters: Company[];
filteredListOfStatuses: Status[];
filteredListOfCars: Car[];
constructor(...) {
}
ngOnInit() {
this.fillFilteredListsOnInit();
this.selectValueInControl();
}
confirmFilter(data): void {
data.renterId = this.filterFormGroup.get('renterControl').value;
data.statusId = this.filterFormGroup.get('statusControl').value;
data.carId = this.filterFormGroup.get('carControl').value;
this.dialogRef.close({
data
});
}
onCompanyRenterNameChanged(value: string) {
//this.fillFilteredListOfRenterCompanies(value.toLowerCase());
this.fillFilteredList(this.codeListService.listOfRenters, this.filteredListOfRenters, value.toLowerCase());
}
onStatusChanged(value: string) {
this.fillFilteredListOfStatuses(value.toLowerCase());
}
onCarChanged(value: string) {
this.fillFilteredListOfCars(value.toLowerCase());
}
fillFilteredList(codeList: any[], filteredList: any[], filter: string){
if(codeList.length !== 0){
filteredList = codeList.filter((item) => {
if(item.name !== null){
return item.name.toLowerCase().startsWith(filter);
}
})
}
}
fillFilteredListOfRenterCompanies(filter: string) {
if (this.codeListService.listOfRenters.length !== 0) {
this.filteredListOfRenters = this.codeListService.listOfRenters.filter((item) => {
if (item.name !== null)
return item.name.toLowerCase().startsWith(filter);
});
}
}
fillFilteredListOfStatuses(filter: string) {
if (this.codeListService.statuses.length !== 0) {
this.filteredListOfStatuses = this.codeListService.statuses.filter((item) => {
if (item.name !== null)
return item.name.toLowerCase().startsWith(filter);
});
}
}
fillFilteredListOfCars(filter: string) {
if (this.codeListService.cars.length !== 0) {
this.filteredListOfCars = this.codeListService.cars.filter((item) => {
let carName = this.codeListService.getNameOfManufacturerById(item.manufacturerId) + " " + item.model + " " + item.ecv;
if (carName !== null)
return carName.toLowerCase().startsWith(filter);
});
}
}
fillFilteredListsOnInit(){
this.filteredListOfRenters = this.codeListService.listOfRenters;
this.filteredListOfStatuses = this.codeListService.statuses;
this.filteredListOfCars = this.codeListService.cars;
}
}