The filter becomes ineffective once I remove the input value

Check out this HTML table containing an input field that filters plans.

https://i.stack.imgur.com/UfIw2.png

I input the value => 1

The filter successfully works

https://i.stack.imgur.com/CsQXh.png

Removing the value (1) displays all recordings, totaling to 4 recordings

https://i.stack.imgur.com/dXaIe.png

I try entering the value 4

https://i.stack.imgur.com/hmQWi.png

After deleting the value 4, I notice there are only 2 recordings ????

https://i.stack.imgur.com/1Hvv0.png

I am puzzled by this issue?

todo.component.ts

filterByPlan() {
    if (this.planFilter) {
      this.tarificationTitre = this.todoService.PLN_PLC.filter((item) => {
        return item.PLN_SVM.some((svm) => {
          return svm.PLN.some(
            (plan) => plan.PLAN === parseInt(this.planFilter)
          );
        });
      }).map((item) => {
        item.PLN_SVM = item.PLN_SVM.filter((svm) => {
          return svm.PLN.some(
            (plan) => plan.PLAN === parseInt(this.planFilter)
          );
        });
        return item;
      });
    } else {
      this.tarificationTitre = this.todoService.PLN_PLC;
    }
  }

todo.service.ts

export class TodoService {

   PLN_PLC = [
     // The detailed data for different plans from various places

  ];

  constructor() {}
  
}

The filtering operation is based on the variable PLAN

If you have any insights or ideas, please share as I am eager to understand and resolve the issue. Also, check out the illustration in action on Stackblitz.

Answer №1

Try using this updated function:

  filterByPlan() {
if (this.planFilter) {
  this.tarificationTitre = JSON.parse(JSON.stringify( this.todoService.PLN_PLC)).filter((item:any) => {
    return item.PLN_SVM.some((svm:any) => {
      return svm.PLN.some(
        (plan:any) => plan.PLAN === parseInt(this.planFilter)
      );
    });
  }).map((item:any) => {
    item.PLN_SVM = item.PLN_SVM.filter((svm:any) => {
      return svm.PLN.some(
        (plan:any) => plan.PLAN === parseInt(this.planFilter)
      );
    });
    return item;
  });
} else {
  this.tarificationTitre = this.todoService.PLN_PLC;
}

}

I made some modifications to ensure correct data manipulation without altering the main array in todo service directly.

Give it a try and see if it works as expected.

Answer №2

In the function filterByPlan(), it appears that you are replacing the PLN_SVM objects for each location specified in the service with values that meet the current filter criteria.

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

Interacting Angular 4 Modules communicate with one another

If I have two modules, one for Customers and one for Orders, structured like this: /customers /customers-list.component.html /customers-list.component.ts /customers-details.component.html /customers-details.component.ts /customers-crea ...

Consolidate various arrays of objects while eliminating duplicate items based on an optional property

Imagine having multiple arrays like these: const arr1 = [ { "id": "1", "type": "sales" }, { "id": "2", "type": "finance" } ] const arr2 = [ { "type": "s ...

Is moduleId a reserved word in Angular2 / CLI?

After downloading the newest CLI version, I initiated a fresh test project. angular2 version: "^2.3.1" "@angular/compiler-cli": "^2.3.1", "typescript": "~2.0.3" Within AppComponent, there is this constructor: export class AppComponent ...

Angular: Implementing asynchronous data retrieval for templates

I am currently facing the following issue: I need to create a table with entries (Obj). Some of these entries have a file attribute. When an entry has a file attribute (entry.file), I need to make a backend call to retrieve the URL of that file: public g ...

When a reaction function is triggered within a context, it will output four logs to the console and

My pokemon API application is encountering some issues. Firstly, when I attempt to fetch a pokemon, it continuously adds an infinite number of the same pokemon with just one request. Secondly, if I try to input something again, the application freezes enti ...

Struggling with importing aliases in TypeScript for shadcn-ui library

I am facing a challenge with resolving TypeScript path aliases in my project. I have set up the tsconfig.json file to include path aliases using the "baseUrl" and "paths" configurations, but alias imports are not functioning as intended. My goal is to imp ...

Validate uniqueness of input in database using Angular's async validator

My <input> element allows users to enter a value that should be unique in the database. I'm looking for a way to validate this input dynamically on the front-end, and display an error message if the value is already in the database. ...

Integrating TypeScript into an established create-react-app project

Struggling to integrate TypeScript into an existing create-react-app? I've always added it at the beginning of a project using create-react-app my-app --scripts-version=react-scripts-ts, but that's not working this time. The only "solution" I co ...

Resolving Recursive Problems in Clarity's Tree View Design

Recently, I encountered an issue while using the Clarity Design Angular project with the Tree View recursive template provided in version 0.10.0-alpha. Check out this link for reference selectableRoot = { "@name": "A1", "selected": false, ...

Error in NextJS: Attempting to access a length property of null

Does anyone have insights into the root cause of this error? warn - Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/basic-features/fast-refresh#how-it-works TypeError: Cannot read properties of null (reading 'lengt ...

Angular Material datetime picker with an option for transparent background

After upgrading my angular from version 15 to 16, I encountered a strange issue with the material date time picker. The GUI part of the picker appeared half transparent as shown in this image: Has anyone else faced this kind of problem with the material d ...

Passing parent HTML attributes to child components in Angular 2

Is there a way to pass HTML attributes directly from parent to child without creating variables in the parent's .ts class first? In the sample code below, I am trying to pass the "type=number" attribute from the parent to the app-field-label component ...

Pass the identical event to multiple functions in Angular 2

On my homepage, there is a search form with an input box and select dropdown for users to search for other users by location or using html5 geolocation. When a user visits the page for the first time, they are prompted to allow the app to access their loca ...

Converting JavaScript object data to x-www-form-urlencoded: A step-by-step guide

I am trying to convert a JavaScript object into x-www-form-urlencoded. Is there a way to achieve this using Angular 2? export class Compentency { competencies : number[]; } postData() { let array = [1, 2, 3]; this.comp.competencies ...

Struggling to transmit data to material dialog in Angular2+

I am facing an issue with my Material Dialog not working properly. Can anyone point out what I might be missing? product-thumbnail.ts I will use this to trigger the dialog export class ProductThumbnailComponent implements OnInit { @Input() product: Pr ...

Retrieve information from various MongoDB collections

Greetings! I currently have a database with the following collections: db={ "category": [ { "_id": 1, "item": "Cat A", }, { "_id": 2, "item": "Cat B" ...

Experiencing issues during the execution of "ng serve"

The angular project is named "PaymentApp". When running "ng serve", numerous errors are displayed instead of the default angular template. The message "Cannot GET /" is being shown. Attached images for reference: https://i.stack.imgur.com/faysG.png http ...

Learn how to retrieve data outside of the .subscribe function in an Angular 2 polling service

// I'm facing an issue where I am unable to assign values from outside the subscribe function to any variable. In my current project, I am fetching JSON content using the http.post() method and storing it in a variable. However, I need to access this ...

Is it possible to assign a type conditionally depending on the value of a boolean?

While grappling with this issue, the title question arose in my mind: How can I handle the situation where the library function getResponse returns { a: number } for Android phones and { b: number } for iOS phones? The code snippet below initially led to ...

Error with the type of CanvasGradient in the NPM package for converting text to image

I attempted to generate an image using a specific text by utilizing npm's text-to-image package, but encountered an error during typescript compilation. The errors I encountered upon running the typescript compilation command are related to files with ...