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

Testing vue-router's useRoute() function in Jest tests on Vue 3

Struggling with creating unit tests using Jest for Vue 3 components that utilize useRoute()? Take a look at the code snippet below: <template> <div :class="{ 'grey-background': !isHomeView }" /> </template> &l ...

What is the reason for ng cli-generated components having two beforeEach methods in their spec files?

Whenever I execute the command ng c my-component, a spec file is generated with 2 beforeEach functions included. describe('MyComponent', () => { let component: MyComponent; let fixture: ComponentFixture<MyComponent>; beforeEach(a ...

Error type inferred by TypeScript

I am currently in the process of learning TypeScript, and I encountered some errors in the code below: Error: Unexpected token. A constructor, method, accessor, or property was expected. [ts] Declaration or statement expected. class duckType{ public ...

Issue with Angular standalone component importation causing rendering issue in HTML

Recently, I started working with Angular and I am currently creating a clone using Firebase. While working on this project, Angular is throwing two errors at me: The Component AppComponent is standalone and cannot be declared in an NgModule. Should it b ...

Preserve the checkbox state upon refreshing the page

I am facing an issue with keeping the checkbox state saved after a page reload. Currently, I have stored my unchecked checkboxes in localStorage, but I am unsure about what steps to take next. In simple terms, I want the checkbox to remain unchecked when I ...

PlateJS: Difficulty in inserting images - Screen remains empty when trying to add images using the Image Element

I incorporated the Image Element component using the command npx @udecode/plate-ui@latest add image-element This action added the caption, media-popover, and resizable components to my setup. When referencing Platejs documentation, everything appears as ...

The React Hook Form's useFieldArray feature is causing conflicts with my custom id assignments

My schema includes an id property, but when I implement useFieldArray, it automatically overrides that id. I'm utilizing shadcn-ui Version of react-hook-form: 7.45.0 const { fields, append, remove, update } = useFieldArray<{ id?: string, test?: n ...

Utilizing Custom Validators in Angular to Enhance Accessibility

I'm struggling to access my service to perform validator checks, but all I'm getting is a console filled with errors. I believe it's just a syntax issue that's tripping me up. Validator: import { DataService } from './services/da ...

Beware of the 'grid zero width' alert that may appear when utilizing ag-Grid's sizeColumnsToFit() function on multiple ag-Grids that are shown within a tab menu

Encountering a warning message when resizing ag-Grid and switching between tabs. The warning reads: ag-Grid: tried to call sizeColumnsToFit() but the grid is coming back with zero width, maybe the grid is not visible yet on the screen? A demo of this ...

Steps for deploying an Angular 2 application without the need for a server

I am embarking on a basic Angular 2 project, however, I do not wish to manage it using npm. Is there any alternative method to initiate the project with the following steps: Compile all *.ts files Transfer compiled js-files to a separate directory (not ...

Ways to utilize the useRef method within the useContext hook while implementing Typescript

Struggling to incorporate useRef into my global Next.js useContext function while utilizing TypeScript. Attempted approach and encountered errors: interface tripAttributes{ tripTitle: string } const initialTripState: tripAttributes = { tripTitle ...

Utilizing shared components across a Next.js application within a monorepo

Utilizing a monorepo to share types, DTOs, and other isomorphic app components from backend services (Nest.js) within the same mono repo has presented some challenges for me. In my setup, both the next.js app and nest.js app (which itself is a nest.js mono ...

What could be the reason for the validation failure?

companyprofile.html <form #f="ngForm" name="companyProfileForm" ng-submit="companyFrmSave(objCS, objCSCurrency)" novalidate=""> <div class="row form-group"> <div class="col-md-12" > ...

Error in syntax: The tailwind import statement contains an unrecognized word and will not function correctly

After configuring Tailwind CSS with Next.js, I made changes to the tailwind.config.js file. However, after making these changes, the compilation process failed and resulted in the following error: Error - ./src/assets/styles/global.css:3:1 Syntax error: Un ...

Is there a way to navigate to a specific component selector within an ngFor loop?

I have a scenario where I have multiple components running inside *ngFor on the same page. My goal is to create button links at the top of the page that, when clicked, will scroll to the corresponding component on the page. Below are the code snippets tha ...

Angular 2 - update browsing history by replacing instead of adding to it

Is it possible to replace the history instead of pushing a new one in Angular 2's new router (rc.1)? For instance, suppose I am in a question list (/questions), then open a new modal in a new route (/questions/add). After adding a new question, I nav ...

Display notification if the request exceeds the expected duration

Is there a way to display a message when a request is taking too long? For instance, if the request exceeds 10 seconds in duration, I want to show a message asking the user to wait until it finishes. fetchData(url, requestParams) { return this.restServic ...

What is the correct way to construct an object in TypeScript while still taking types into account?

Hey, I'm having trouble implementing a common JavaScript pattern in TypeScript without resorting to using any to ignore the types. My goal is to write a function that constructs an object based on certain conditions and returns the correct type. Here& ...

Centering on request, Google Maps adjusts its view to focus on

When I select a row, I want to set the map center to the provided coordinates in Primeng. The issue is that while this.options works fine in ngOnInit, it doesn't work when called in the showCords() function. Below is my code: gmap.component.ts im ...

Issue concerning the Bootstrap version, transitioning from Bootstrap 3 to Bootstrap 4

Upon initially installing bootstrap version "bootstrap": "^3.3.7",, everything was functioning properly, except for the inability to utilize a card component. For example: <div class="card" style="width: 18rem;"> <img class="card-img-top" src= ...