0

Hello I am trying to make a custom pipe filter which filters the data when checkbox is checked. I have a list of job locations which I want to filter when the certain job location is checked. I am providing the code which is related to the issue/problem.

Pipe code -

@Pipe({
  name: 'checkcity'
})
export class CheckcityPipe implements PipeTransform {

  transform(check: any, checked: any): any {
    let [loc] = checked;
    console.log('checked',checked);
    return checked
            ? check.filter(city => {return city.location }) 
            : check;
  }

}

Input Checkbox -

<input type="checkbox" value="" [(ngModel)]="checked" name="checked"> Bangalore

Data to filter -

<div *ngFor="let joblist of jobList | checkcity: checked">
    {{joblist.location}}
</div>
7
  • are you getting any error, share the log in that case. Commented Sep 14, 2017 at 7:59
  • What is let [loc] = checked; supposed to do`? Commented Sep 14, 2017 at 8:00
  • What does return city.location return? Is it supposed to filter all elements where city.location contains a value? Commented Sep 14, 2017 at 8:01
  • @Manu, hi I am not getting any error Commented Sep 14, 2017 at 8:57
  • @GünterZöchbauer, hi I was experimenting, so you can remove that/ignore that, also I am not getting error, that is the problem Commented Sep 14, 2017 at 8:58

1 Answer 1

1

You should rename local var as joblist and the source as jobLists they should not be same. Can you try with the below code

@Pipe({
  name: 'checkcity'
})
export class CheckcityPipe implements PipeTransform {

  transform(check: any, checked: any): any {
    //let [loc] = checked;
    console.log('checked',checked);
    return checked
            ? check.filter(city =>  city.location == checked) 
            : check;
  }

}

<input type="checkbox" value="Bangalore" [(ngModel)]="checked" name="city"> Bangalore

<div *ngFor="let joblist of jobLists | checkcity: checked">
    {{joblist.location}}
</div>
Sign up to request clarification or add additional context in comments.

9 Comments

Hello, I tried this, its not working and not giving me any error, but is saying that checklist undefined. I have put the list and checkbox filter in different component, will it affect.
@Akshay what is checklist? you have to import the pipe class in your list component class
it is better if you can put the code in separate files in the question for better understanding of others
sorry, it is checked undefined, I have imported the pipe class wherever required, else it would have given me an error. But there is no error coming.
@Akshay have you declared a property as checked in your component class? updated the name of checkbox.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.