1

I'm trying to figure how can you filter given products based on the material with a checkbox ? Should i be using pipes ? Should i be using only filter ?

Product-list.HTML

<div class="category-container">
<div class="wrapper">
    <h2>Cotton</h2>
    <div class="form-check">
      <input type="checkbox">
      <label for="defaultCheck1" class="form-check-label">
        Cotton
      </label>
    </div>
  </div>
</div>

<div class="container">
  <div class="row align-items-start">
    <div *ngFor="let product of products"  class="col-6">
      <app-product-card [product]="product" ></app-product-card>
    </div>
  </div>
</div>

Product-list.component.ts

import { Component, OnInit, Input } from '@angular/core';
import { ProductService } from 'src/app/_services/product.service';
import { Product } from 'src/app/_models/product';

@Component({
  selector: 'app-product-list',
  templateUrl: './product-list.component.html',
  styleUrls: ['./product-list.component.css']
})
export class ProductListComponent implements OnInit {
  @Input() product: Product;
  products: Product[];

  constructor(private productService: ProductService) { }

  ngOnInit() {
    this.loadProducts();
  }

  loadProducts() {
    this.productService.getProducts().subscribe((products: Product[]) => {
      this.products = products;
    }, error => {
      console.log(error);
    });
  }
}

1 Answer 1

1

Use Filter to filter on the basis of the checkbox event.

loadProducts() {
   this.productService.getProducts().subscribe((products: Product[]) => {
      this.products = products;
      let a = this.products.filter(e => e.filtercondition == checkboxInput);
      this.products = a;
   }, error => {
      console.log(error);
   });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! The only problem i face is that it always seems to be filtered. Maybe im doing the checkboxInput event wrong ? const a = this.products.filter(e => e.material === this.cotton); this.products = a;

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.