Using the pipe, filtering will not be activated by clicking

In order to find the perfect products, I need to utilize the myfilters filter. Unfortunately, this is not currently working as expected.

Here is a snippet of the store's HTML:

<h2>store</h2>
<select>
  <option *ngFor="let g of GenderFilter">{{g.DisplayText}}</option>
</select>
<select>
  <option *ngFor="let p of PriceFilter">{{p.DisplayText}}</option>
</select>

<tr *ngFor="let P of products | filer : p | orderBy: 'PriceFilter'">
  <td>{{p.DisplayText}}</td>
</tr>

Filter Pipe Code:

export class FilerPipe implements PipeTransform {
  transform(items: any[], term): any {
    console.log('term', term);

    return term 
        ? items.filter(item => item.ProductTags.indexOf(term) !== -1)
        : items;
  }
}

Order by Pipe Code:

transform(items: Array<string>, orderBy: string): Array<string> {
  if (items !== undefined) {
    items.sort((a: any, b: any) => {
      if (a[orderBy] < b[orderBy]){
        return -1;
      } else if (a[orderBy] > b[orderBy]) {
        return 1;
      } else {
        return 0;   
      }
    });
  }
  return items;
}

The key to displaying the right products lies in ensuring they have the same "tag" as the filter applied.

Answer №1

since p does not exist outside

<select>
 <option *ngFor="let p of PriceFilter">{{p.DisplayText}}</option>
</select>

to capture it in your next ngfor filter, you need to find a different way to retrieve the value of the user-selected p and pass it to your filter pipe.

Edit based on your comment

<h2>store</h2>
 <select [(ngModel)]="selectedOption" name=Gender>
  <option *ngFor="let g of GenderFilter">{{g.DisplayText}}</option> 
 </select>
 <select [(ngModel)]="selectedOptionPrice" name=Price>
  <option *ngFor="let p of PriceFilter">{{p.DisplayText}}</option> 
 </select>

You can separate the filter from ngfor by placing ngFor in an ng-container and invoking your filter function within your tr tag.

<ng-container *ngFor="let P of products | orderBy: 'PriceFilter'">
 <tr *ngIf=filter(selectedOptionPrice)>
  <td>{{selectedOptionPrice.DisplayText}}</td>
 </tr>
</ng-container>

Include your filter pipe as a filter() function in your .ts file for better specificity than using a pipe.

.TS file

filter(price) {
 return price 
    ? this.items.filter(item => item.ProductTags.indexOf(price) !== -1) : this.items;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Optimal method for transmitting an image as a JSON object from an SQL database using C#

I have a scenario in C# where I am retrieving an image as a byte array. My goal is to send this image as JSON, but currently the code returns it as a memorystream. var note = reader["Note"].ToString(); var price = (decimal)rea ...

Issue Loading Angular 2: Preflight Response Invalid (redirect) Error

Utilizing Angular 2 alongside a Laravel PHP Framework for my API, the Angular 2 project runs on http://localhost:4200/, while the Laravel PHP Framework operates on http://localhost:80/laravel/public. Upon executing the following code snippet within my Ang ...

Initiate error404.json.twig - Error Triggered

I am currently working on setting up a customized JSON error template in Symfony. However, I am facing an issue where the HTML version of the error template is being returned instead of the JSON version. Despite setting Accept and Content-Type to "applicat ...

Is the "Angular is not functioning" error message the result of a missing import?

Encountering an 'is not a function' error in Angular 7 / TypeScript 3, using an EventEmitter. This issue has been extensively discussed (a b), but solutions have not garnered many upvotes (a b c d e f). I am close to resolving it, but need s ...

Tips on changing a JSON array into individual objects

Below is the JSON data that I received: {"data":{"0":{"0":"1","1":"Test1","2":"Test2","DT_RowId":"row_1"}}} I am looking to eliminate the outer level index. The desired output is: {"data":[{"0":"1","1":"Test1","2":"Test2","DT_RowId":"row_1"}]} My goal ...

Exploring ways to extract a specific value from a JSON file using grep?

I am facing a challenge with my JSON file and its content which looks like this: [ { "id":"54545-f919-4b0f-930c-0117d6e6c987", "name":"Inventory_Groups", "path":"/Groups", "subGroups":[ { ...

What is the best way to retrieve TemplateRef from an Angular component?

TS Component ngOnInit() { if(somecondition) // The line of code that is causing issues this.openModal(#tempName); } HTML Component <ng-template #tempName> Content goes here! </ng-template> this.openModal(#tempNa ...

What are some ways to leverage the window object within Angular 2?

I attempted to include the following code in order to access a window object in angular 2: @Component({ selector: 'app-slider', templateUrl: './slider.component.html', styleUrls: ['./slider.compo ...

Leveraging Angular2 within Angularjs framework

Can Angular2 be integrated with AngularJS? For instance, is there a way to have a button in an AngularJS application that, when clicked, shows an Angular2 form? What would be the best approach for this scenario? Would it be better to host them on separat ...

Using getter functions and Visual Studio for TypeScript

In my TypeScript classes in Visual Studio, I have been implementing getter functions. I find that using getter functions helps to clean up my code, although there is one issue that I would like to address. class Foo { doWork(){ console.log(this.bar ...

Exploring unnamed objects in JSON responses using RXJS in Angular 5

Currently, the situation is as follows: I am required to dynamically populate a mat-table after receiving a response from an API. Within the JSON response, there are anonymous JSON objects in the "files" array. "files": [ { "title": "dummy", ...

Using a forEach loop within an Else statement is ineffective

I've encountered an issue while trying to merge two arrays and create a new one. It seems that my forEach loop inside the else statement is returning undefined. I'm unsure if I made a mistake in my approach or if forEach is not meant to be used w ...

Having trouble with correctly implementing Laravel pagination

I'm currently working on fetching product data from an API controller and displaying it on a blade page. I've managed to retrieve the data, but I'm facing issues with pagination as it's not directing to the correct page and isn't f ...

Retrieving information from BlobStorage using Service.ts and triggering the NgOnit function

Within the service.ts module, I am fetching data from Azure Blobstorage and attempting to read its contents. service.ts import { Injectable } from '@angular/core'; import { environment } from '../../environments/environment'; import { ...

return to the original secured page based on the most recent language preference

I'm facing an issue with a logical redirection that needs to redirect users to the previous protected page after login. The login functionality is implemented using my custom login page and Google Credentials. Additionally, I have set up a multilingu ...

Leveraging logstash for removing XSSI prefix from JSON output

I'm facing a straightforward issue that's puzzling me. I'm attempting to retrieve Gerrit data using Logstash through a rest API. My configuration with http_poller is giving me the correct response, so I feel like I'm close to solving it ...

Manipulate an image with PHP and jQuery to rotate it, then store the edited image in a specified destination directory

This is not just a question, but an opportunity to share knowledge with many like-minded individuals. After spending hours working on rotating images dynamically using PHP and jQuery, I have come up with a solution that I believe will benefit others in the ...

Tips for determining if a class has been declared in Typescript

When it comes to checking if a class (or any other element) exists in the global scope, Javascript offers a convenient method: typeof SomeUndeclaredOne === 'undefined' However, this approach doesn't translate well into TypeScript as it trig ...

Discovering errors within a reactive form in Angular using a loop to iterate through the FormArray

I am currently utilizing Angular CLI version 16.2.1. As I progress through a course, I am working with reactive forms. In a list of 'Recipes', I aim to include a Recipe with various inputs and a collection of ingredients. However, I am encounter ...

Exploring the world of command line testing with Angular2, Typescript, Karma, and Jasmine

If you're looking to run unit tests for Angular2 using Jasmine from the command line with Karma, you might have encountered some challenges. Various configurations have been tried, but none seem to work seamlessly when combining Angular2, SystemJs, Ty ...