DEV Community

Cover image for How to filter the data with a common search bar at the top (Angular 8)
Sandeep Balachandran
Sandeep Balachandran

Posted on • Edited on

9

How to filter the data with a common search bar at the top (Angular 8)

Hey there,

Alt text of image

Lets say we have common search bar at the top of our application . We need to filter the available data on the main component according to the keywords typed.

We can make use of

  • Array filter method
  • Component interaction (Child to Parent)

Let's see our Header component (header.html)

<input matInput type="text"  [(ngModel)]="searchword"(input)="searchThis()">
Enter fullscreen mode Exit fullscreen mode

We use

  • ngModel for two way data binding
  • (input) logs the value whenever you change the value of the element.

Header Component (header.ts)


import { Component,Input, OnInit, Output,EventEmitter } from '@angular/core';
@Output() searchcriteria = new EventEmitter<String>();
searchThis() {
    this.searchcriteria.emit(this.searchword)
}
Enter fullscreen mode Exit fullscreen mode

We create a new event emitter in order to emit the value from the header component to body component. searchThis function will trigger the event for every keypress in the search input field.

Let's see our parent component or body component

Parent component (html)

<app-header (searchcriteria)="searchThis($event)"></app-header>
Enter fullscreen mode Exit fullscreen mode

We use event binding to pass the data from header to parent .

Parent Component (ts)

  newArray
  searchThis(data) {
    this.content = this.newArray
    console.log(data)
    if (data) {
      this.content = this.content.filter(function (ele, i, array) {
        let arrayelement = ele.name.toLowerCase()
        return arrayelement.includes(data)
      })
    }
    else {
      console.log(this.content)
    }
    console.log(this.content)
  }
}
Enter fullscreen mode Exit fullscreen mode

newArray is the array contains the data for the component. We assign the data to another array content each time function calls and filter from that array. The filter function changes the existing original array.

The filtered array will be used when we call the function if we change the original array. We need fresh array with full data each time when the function calls.

Let me know how you can improve this.

Until next time,

alt text

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (2)

Collapse
 
jwp profile image
JWP

Sandeep; Nice design, the event model is tops. Check out my article on Angular Event component here... dev.to/jwp/the-angular-event-servi...

Collapse
 
sandeepbalachandran profile image
Sandeep Balachandran

Great Article. Enjoyed it.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay