fire angular pipe event when button is clicked

bhaskar reddy

I want to implement angular 5 search pipe when the button is clicked.

I have a input box which will receive custom search value and it filters different keys values from array of objects. I have implemented angular pipe when typed input text, it searches but i want to trigger this action when user input and search button is clicked only. Is there anyway we could achieve this. I am stuck at implementing click event

itemsearch.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'itemsearch'
})
export class ItemsearchPipe implements PipeTransform {

  public transform(value, keys: string, term: string) {

if (!term) {
  return value;
}
return (value || []).filter(item => keys.split(',').some(key => 
 item.hasOwnProperty(key) && new RegExp(term, 'gi').test(item[key])));

} }

 itemcomponent.ts


<div *ngFor="let item of items | itemsearch:'name, age':query"> 
{{item.name}}
         {{item.age}} </div>
</div>
Smokey Dawson

What you can do is remove the pipe operator from the *ngFor and move the logic into your component

component.ts

import { ItemsearchPipe } from ...

search() {
   // you can do whatever you want here, return the value or store it in a variable
   this.items = this.ItemsearchPipe.transform(value, keys, term); 
}

then in your html

<div *ngFor="let item of items">
  <!-- ... -->
</div>
<button (click)="search()"></button>

when using the pipe operator it basically calls the pipe function on every value change, when you move the logic into your component you can control when the pipe function is called

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Fire an event when menu is clicked

How to have an event fire when a rectangle is clicked

javafx: How to bind the Enter key to a button and fire off an event when it is clicked?

.NET MAUI `Button.OnClicked` Event Does Not Fire When Clicked / Tapped

Debugging event when button is clicked

SQL Errors Fire when Non-Related Button is Clicked

angularjs button event not firing when clicked

how to send event when one button clicked

Add event when all button clicked

Angular 2 event when any [routerLink] is clicked

When a button is clicked (click) event should execute after <a href> event

Textbox lostfocus event doesn't fire when using a default button

How to fire an event when a radio-button (input) is checked?

not highlighting when click the button on left-edge of the screen, but fire event

change colour of a mat button when clicked in angular

Page not refreshed when clicked delete button in angular

Create button spinner when clicked in angular 5

stateChangeStart event in Angular doesn't fire when refreshing page

Event to fire when an angular *ngIf statement evaluates in template

Trigger an event when next month button clicked on fullcalendar not working

How to call an event when browser back button is clicked

Event error when clicked each delete button in Android ListView

How to write a click event on MediaElement when pause button clicked?

How trigger event listening for shapes on a layer when button clicked?

Disable button event when embedded mat icon is clicked

Custom Button touchUpInside event not fire

Programmatically fire button click event?

How to delete parent node when child node fire clicked event in JavaScript?

Angular - Make a button active when it is clicked and inactive when an other button in the group is clicked