Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search filter in angular 6 [closed]

I have two components called list and details, On clicking to particular list-item in list component. It will emit selected/clicked list-item to details component as in below image.

enter image description here

Now i have placed another component called search above the list component like this:

enter image description here

How can i apply filter for the list-items present in the list component? So that i can search the list-items easily.

Stackblitz Link

like image 716
Empty_Soul Avatar asked Jan 23 '19 10:01

Empty_Soul


People also ask

How to perform angular Search Filter on input field using angular?

Angular doesn’t have a search filter pipe, we will make our own custom search pipe to perform Angular search filter on the input field. Let first create a custom pipe using the following command. ng g pipe searchFilter Edit search-filter.pipe.ts file to filter list based on user type text criteria of input field data.

What is pipe operator in angular Search Filter?

This symbol defines the Pipe Operator in angular. Throughout this Angular Search Filter tutorial, you will use the pipe symbol to filter the data. Bootstrap is a robust front-end framework applied to build modern web and mobile applications. It reduces the pain of writing code from scratch for HTML and CSS, most importantly open-source.

How do I use ng2-search-filter?

To use ng2-search-filter we need to import Ng2SearchPipeModule into our module in our app.module.ts file. We also need to import HttpClientModule to retrieve our data using the HTPP GET method. First, we need a variable that can hold the list of data to be filtered.

How to create a search filter for filtering table by column?

The following steps can be used to create a search filter for filtering table by your specific column. 1. Create a Custom Pipe 2. Include Custom Pipe in your Module 3. Modify Component for Data display on HTML view for search fields


2 Answers

You can create a pipe for this.

Here is a working solution

I created a pipe and called it ListFilterPipe

@Pipe({
  name: 'listFilter'
})
export class ListFilterPipe implements PipeTransform {

  transform(list: any[], filterText: string): any {
    return list ? list.filter(item => item.name.search(new RegExp(filterText, 'i')) > -1) : [];
  }

}

And simply use it within *ngFor as follows

<h4>List</h4>
<div class="main-div">
<app-search [(searchModel)]="searchModel"></app-search>
  <div class="list">
    <mat-selection-list  #contact>
      <mat-list-option  *ngFor="let contact of contacts | listFilter: searchModel">
        <a mat-list-item (click)="onSelect(contact)"><span>{{ contact?.name }}</span></a>
      </mat-list-option>
    </mat-selection-list>
  </div>

Also, added and input and output to search.component so that we can update our searchModel

search.component.ts

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {

  @Input() searchModel;

  @Output() searchModelChange: EventEmitter<any> = new EventEmitter();

  constructor() { }

  ngOnInit() {
  }

  updateSearchModel(value) {
    this.searchModel = value;
    this.searchModelChange.emit(this.searchModel);
  }

}

search.component.html

<mat-form-field floatLabel="never">
  <input matInput class="searching-customer" type="text"  placeholder="Search" 
         [ngModel]="searchModel" 
         (ngModelChange)="updateSearchModel($event)">
</mat-form-field>
like image 173
Bunyamin Coskuner Avatar answered Nov 15 '22 09:11

Bunyamin Coskuner


You can pass the List of contacts to Search Comonent For that change in List.component.html

<h4>List</h4>
<div class="main-div">
<app-search [list]="contacts" ></app-search>
  <div class="list">
    <mat-selection-list  #contact>
        <mat-list-option  *ngFor="let contact of contacts">
            <a mat-list-item (click)="onSelect(contact)"><span>{{ contact?.name }}</span></a>
        </mat-list-option>
      </mat-selection-list>
  </div>
</div>  

Inside your search component - get value of input box - filter the list by that input value

Search.component.html

<mat-form-field floatLabel=never >
  <input matInput class="searching-customer" type="text"  placeholder="Search" (input)="search($event.target.value)"  >
</mat-form-field>

search.component.ts

import { Component, OnInit , Input} from '@angular/core';
import { IContact } from '../models';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {

@Input()
public list:  IContact[] ;
searchedList : any;

  constructor() { }

  ngOnInit() {
  }

  // This function will be called on every key press for input text box
  search(value)
  {
    this.searchedList = this.list.filter(
      (val)=> val['name'].includes(value))
    //Searched Data
    console.log(this.searchedList)
  }
}
like image 29
Vishal Hasnani Avatar answered Nov 15 '22 09:11

Vishal Hasnani