How do I filter a list sorted from Checkbox Values in Angular?

Jeremy Lucas

I've seen a few checkbox ideas and strategies, but can't seem to get this to work in my particular case.

I am newer to Angular but am trying to build a workout webapp. What I am trying to do is use checkboxes to filter which workouts appear in the workout list (workout list is displayed using an ngFor).

Currently, I have a WorkoutListComponent that displays the list using the ngFor (shortened for simplicity):

<div
  *ngFor="let workout of workouts"
  class="workout__list"
>

  <h2 class="workout__item--title">{{ workout.title }}</h2>
  <h3 class="workout__item--specs-heading">{{ workout.type }}</h3>
  <h3 class="workout__item--specs-heading">{{ workout.duration}}</h3>
  <h3 class="workout__item--specs-heading">{{ workout.phase }}</h3>
</div>

Within my ts, I have the following:

...
workouts: any;

constructor(private workoutService: WorkoutService) {
  this.workoutService.getWorkouts().subscribe(res => {
    this.workouts = res;
    console.log("List Comp: ", this.workouts);
  });
}

The workouts: any is coming from my workout.service. This is the same service that holds my mock workout data and runs the function to get and sort workouts. The workout.service is also making an observable with BehaviorSubject so I can subscribe to it in the above WorkoutListComponent. WorkoutService

selectedWorkouts: BehaviorSubject<any>;
  workouts: Workout[] = [
    new Workout( ... dummy text for workouts ... ),
    new Workout( ... dummy text for workouts ... ),
    new Workout( ... dummy text for workouts ... )
  ];
 constructor() {
    this.selectedWorkouts = new BehaviorSubject(this.workouts);
  }

getWorkouts() {
  return this.selectedWorkouts.asObservable();
}

filterWorkouts(phase: any[]) {
  console.log("SERVICE: ", phase);

    // I want the filter to take place here and put the filtered items back into the `this.workouts` array
 }
}

So far what I have tried code that doesn't work, where in the filterWorkouts() function, I take in the phase and specialty, and then filter this.workouts storing it into this.workouts. Since this.workouts is displayed in the list item, the idea is that this would work. Unfortunately it has not worked the way I wanted to. This code is at the bottom of the src/app/features/workouts-page/workoutservice/workout.service.ts in the attached stackblitz.

The way I am currently receiving checkbox data is through my WorkoutFilter component. I am taking the checked property keyvalues and storing them within an object array rather than only using true or false properties as found in this stackoverflow . The WorkoutFilter Component is found in the stackblitz in src/app/features/workouts-page/workouts/workouts-filter.

All I'm trying to do is find the best way to filter this list depending on the checkbox values.

  • The checkbox values are coming through the WorkoutFilter component.

  • The Workout list I want to display after being filtered is the WorkoutList.

  • I am currently using an apply button for the form. When submitted, the data is made into an object with strings to hold the values of the checkboxes. This can be seen in the WorkoutFilter component

    If there is a better method to doing this, maybe even using id's , I am all ears.

Here is the StackBlitz code. I removed a few components such as the header and sidenav to make viewing of the issue easier. Thank you

Jorge Mussato

I recommend when you are working with multiple checkboxes, to use the SelectionModel from angular collections. Try not using reactive forms with multiple checkboxs control for filters like that, SelectionModel makes it so easier.

Here's the stackblitz, hope it helps.

https://stackblitz.com/edit/github-4rezan-yqxkyv

code:

html:

<h4>Phase</h4>
<div class="checkbox">
    <span *ngFor="let phase of phaseOptions">
      <mat-checkbox
        color="warn"
        type="checkbox"
        (click)="phaseSelection.toggle(phase)"
      >
        {{ phase }}
      </mat-checkbox>
    </span>
</div>

<h4>Specialty</h4>
<div class="checkbox">
    <span *ngFor="let specialty of specialtyOptions">
      <mat-checkbox
        color="warn"
        type="checkbox"
        (click)="specialtySelection.toggle(specialty)"
      >
        {{ specialty }}
      </mat-checkbox>
    </span>
</div>
<button (click)="applyFilter()">Apply</button>

ts:

  specialtySelection = new SelectionModel<string>(true);
  phaseSelection = new SelectionModel<string>(true);

  applyFilter() {
    this.workoutService.filterWorkouts(this.phaseSelection.selected,
         this.specialtySelection.selected);
      }

service:

filterWorkouts(phases: string[], specialties: string[]) {
  // do Filter with phases and specialties
  if (phases.length === 0 && specialties.length === 0) {
     this.selectedWorkouts.next(workouts);
  } else {
    const byPhase = (workout) => phases.some(phase => workout.phase === phase);
    const bySpecialty = (workout) => specialties.some(specialty => workout.specialty === specialty);
    const workouts = this.workouts.filter(workout => byPhase(workout) && bySpecialty(workout));
    this.selectedWorkouts.next(workouts);
  }
}

Hope it helped.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I filter values from a Vec and still return a Vec?

分類Dev

How do I filter out values from these JSON objects

分類Dev

How do I filter specific word from a values

分類Dev

How can I do an AND search from a dictionary, sending a list of values?

分類Dev

How do I return 2 values from two arrays when using filter

分類Dev

How do I filter the dates of the current week from a general date list in Javascript or Typescript?

分類Dev

How to do the filter operator from a list of a list in rxjava

分類Dev

How to filter an object array using checkbox with angular?

分類Dev

How do I entirely remove the label from a CheckBox?

分類Dev

After using groupby, how do I get all values from multiple rows into a list?

分類Dev

How do I drop every column that only contains values from a list pandas

分類Dev

Angular: How to get multiple checkbox values

分類Dev

How do I sort in XSL based on another list that have been sorted?

分類Dev

how do I add a checkbox before every list Item, and a delete button after the list item?

分類Dev

How do I save multiple checkbox values in a single column in the database and retrieve it using laravel

分類Dev

How to populate jtable checkbox values from database?

分類Dev

How can I remove zero values from list?

分類Dev

How to get the value from angular material checkbox

分類Dev

How can I order the column values that appear in a Filter list in ag-Grid and AdapTable?

分類Dev

How can I sum values of an array with the same key sorted by category?

分類Dev

How do I enable multiple selection of values from a combobox?

分類Dev

How do I get return values from execute functions in android?

分類Dev

How do I retrieve values from successive lines in perl?

分類Dev

How do I sum values from different rows in the tidyverse?

分類Dev

How do I get data from a radio/checkbox form submission in the Blazor @code block?

分類Dev

How do I prevent a checkbox from looking selected if click handler causes error

分類Dev

How do I correctly filter intermittent "NoneType" values in row-wise apply functions?

分類Dev

How can I output a list of commands sorted by user?

分類Dev

How in Angular Material to set values of "Y" and "N" for component checkbox?

Related 関連記事

  1. 1

    How do I filter values from a Vec and still return a Vec?

  2. 2

    How do I filter out values from these JSON objects

  3. 3

    How do I filter specific word from a values

  4. 4

    How can I do an AND search from a dictionary, sending a list of values?

  5. 5

    How do I return 2 values from two arrays when using filter

  6. 6

    How do I filter the dates of the current week from a general date list in Javascript or Typescript?

  7. 7

    How to do the filter operator from a list of a list in rxjava

  8. 8

    How to filter an object array using checkbox with angular?

  9. 9

    How do I entirely remove the label from a CheckBox?

  10. 10

    After using groupby, how do I get all values from multiple rows into a list?

  11. 11

    How do I drop every column that only contains values from a list pandas

  12. 12

    Angular: How to get multiple checkbox values

  13. 13

    How do I sort in XSL based on another list that have been sorted?

  14. 14

    how do I add a checkbox before every list Item, and a delete button after the list item?

  15. 15

    How do I save multiple checkbox values in a single column in the database and retrieve it using laravel

  16. 16

    How to populate jtable checkbox values from database?

  17. 17

    How can I remove zero values from list?

  18. 18

    How to get the value from angular material checkbox

  19. 19

    How can I order the column values that appear in a Filter list in ag-Grid and AdapTable?

  20. 20

    How can I sum values of an array with the same key sorted by category?

  21. 21

    How do I enable multiple selection of values from a combobox?

  22. 22

    How do I get return values from execute functions in android?

  23. 23

    How do I retrieve values from successive lines in perl?

  24. 24

    How do I sum values from different rows in the tidyverse?

  25. 25

    How do I get data from a radio/checkbox form submission in the Blazor @code block?

  26. 26

    How do I prevent a checkbox from looking selected if click handler causes error

  27. 27

    How do I correctly filter intermittent "NoneType" values in row-wise apply functions?

  28. 28

    How can I output a list of commands sorted by user?

  29. 29

    How in Angular Material to set values of "Y" and "N" for component checkbox?

ホットタグ

アーカイブ