Using Angular 8 for Filtering Firebase Data

I need to implement a filter on the titles of my stored data, but I am facing an issue where the structure of the filtered data changes. The original "key" of the data gets deleted by the filter and is replaced with an index instead.

Link to repository : View repository

To see my database structure, click here: Database structure

Below are the visuals showing the data before and after filtering:

Snippet from my component code:

filter(query: string) {
    this.filteredItems = (query) ?
    Object.values(this.items).filter(p=>p.title.normalize("NFD").replace(/[\u0300-\u036f]/g, "")
    .toLowerCase().includes(query.toLowerCase())) :
    this.items; 
}

Template snippet:

<input #query (keyup)="filter(query.value)" type="text" class="form-control search" placeholder="Search...">
<mat-list-item id="item_list" *ngFor="let item of filteredItems | keyvalue ; let i = index" [class.changeColor]="item.key == selectedValue">
  <a (click)="openItem(item, item.key)" *ngIf="item.value.title" matLine><mat-icon [class.yellow]="item.key == selectedValue">code</mat-icon>{{item.value.title}}</a>
  <button mat-icon-button>
    <mat-icon id="edit_item_icon" (click)="onEditItem(item)">edit</mat-icon>
    <mat-icon (click)="openDialog(item.key, item.value.title)">delete_sweep</mat-icon>
 </button>
</mat-list-item>

As a result, when I try to edit an item after applying the filter, it creates a new item...

Answer №1

Iterate over the data and apply a filter using indexOf():

filter(query: string) {
    const result: object = {};

    for (let key in this.items) {
      const data = this.items[key];

      if (this.items.hasOwnProperty(key) && data.title &&
        data.title.normalize("NFD").replace(/[\u0300-\u036f]/g, "").indexOf(query) !== -1) {
        result[key] = data;
      }
    }

    this.filteredItems = Object.keys(result).length ? result : this.items;
}

I highly suggest wrapping the assignment of this.filteredItems in ngZone.run() if it will affect the DOM:

this.ngZone.run(() => {
      this.filteredItems = Object.keys(result).length ? result : this.items;
});

Don't forget to include the service in your constructor with

constructor(private ngZone: NgZone)

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

Exploring Angular2's ability to interpret directive templates using the ng-container

Recently delving into angular2, I ventured into creating dynamic forms and generating fields by following the guide provided in this URL. The result was as expected. The dynamic form component renders each field one by one using ng-container, like shown b ...

Is Reactfire still using mixins even though they are deprecated?

As I navigate through the reactfire github repository, I notice that they are using mixins. I was under the impression that mixins were deprecated. Am I out of touch with the latest updates? ...

Enhancing the Look: A Guide to Angular Styling

As a newcomer to angular design and styling, I find myself facing a challenge in incorporating existing styles into my new project. My predecessor, who has since moved on from the project, had used certain styles that I need to adopt. Despite installing ui ...

The Intersection of Material-UI, TypeScript, and Powerful Autocomplete Features

In my TypeScript project, I'm attempting to develop a Material-UI AutoComplete component that retrieves the input value based on an object's property name -> obj[key] However, when using the prop getOptionLabel, I encountered the following er ...

Execute the function upon clicking

When I click on an icon, I want it to blink. This is the code in my typescript file: onBlink() { this.action = true; setTimeout(() => { this.action = false; }, 1000) return this.action }; Here is how the action is declared in my ...

The parameter type 'Object' cannot be assigned to the parameter type 'JSON' in the HttpClient GET method

Hey there! Currently, I'm deep into developing an Angular 6 + Flask application and I've encountered a bit of a snag: Error TS2345: Argument of type 'Object' is not assignable to parameter of type 'JSON'. This issue arises w ...

Pressing Enter in a Material-UI Dialog does not trigger form submission

I am currently facing an issue with my modal dialog component. I have two buttons within the dialog, with one functioning as a submit button. My goal is to make the 'Enter' key trigger the submit action as well. Below is the code snippet for thi ...

Exploring the possibilities of developing WebComponents within Angular using TypeScript

My Angular application was originally created using the default method with ng new project-name". However, for performance reasons, I had to incorporate single standard WebComponents. The JavaScript code associated with these components is stored in a ...

The console is displaying a null value outside of the block, however, the correct value is returned when

I am having an issue where the first console.log() is not returning the correct value, but the second one is. Can anyone provide assistance with this problem? angular.module('resultsApp', ['ngCookies']) .config(['$qProvider&a ...

Discover the syntax for reading route parameters in Angular

Currently, I am integrating the Paypal API into my project. After confirming a purchase, Paypal redirects to a specified URL. I set the desired URL as "localhost:4200/shop/order". However, when Paypal returns the URL, it appends the token and payerid at th ...

Overriding Bootstrap Modals with PhaserJS Canvas

I am currently working on a simple game using phaserJs with Angular and styling it with bootstrap. The issue I'm facing is that when I display a bootstrap modal and interact with it, it affects the buttons on my phaserJS canvas. Here is an example pr ...

What is the best way to store data in Firebase as a JSON object?

I need your help with storing a JSON string in Firebase. I have to update my app weekly with data, so I decided to store the information in a JSON format. After using an online tool to stringify it, I got a long string that looks like this: " ...

What is the process for deactivating NodeTree (both parent and child elements) in Primeng?

Here is the HTML code I am working with: <p-tree id="tree" *ngIf="node" [value]="node" selectionMode="null" [(selection)]="selectedFile" (onNodeSelect)="nodeSelect($event)" (onNodeUnselect)=& ...

Encountering an error on Ionic 4, Angular 8: Unable to access property 'subscribe' of undefined

I have a project that utilizes Ionic 4 and Angular 8. The project requires integration with a payment gateway service, which I am accomplishing using the Ionic 4 InAppBrowser plugin to open the payment gateway site. This is how I've implemented it: ...

What could be causing the lack of Tailwind CSS intellisense in a Next.js app with TypeScript?

Check out my tailwind.config.ts file I've been trying to figure it out by watching YouTube tutorials, but nothing seems to be working. Even with the tailwind.config.ts file in my Next.js app, it still isn't functioning properly. Perhaps there&ap ...

How can I configure a mocked dependency in Jest to return a specific value in a function?

I am currently working on simulating a mongoose model to facilitate unit testing for an express controller. To keep things simple, I've removed all unnecessary code and provided below the specific scenario I'm dealing with. Here's the snippe ...

Modify the field key within the Cloud Firestore

Is it possible to update the key itself instead of its value in Firestore? For example, changing from key: value to key2: value. Thanks ...

Ways to enable Urql (typescript) to accept Vue reactive variables for queries generated using graphql-codegen

I'm currently developing a Vue project that involves using urql and graphql-codegen. When utilizing useQuery() in urql, it allows for the use of Vue reactive variables to make the query reactive and update accordingly when the variables change. The ...

Can you explain the distinction among the various FormControl initialization techniques?

Can you discern the variance among the following methods: input: new FormControl({}, inputValidatorFn()) input: [{}, inputValidatorFn()] input: formBuilder.control({}, inputValidatorFn()) They appear to function identically. Is there truly no disparit ...

Enhance Vuetify functionality using TypeScript for custom components

I'm facing a challenge with extending a Vuetify component and setting default props in TypeScript. While I had success doing this in JavaScript, I am struggling to do the same in TS. Below is an example of how the Component was implemented in JS: imp ...