Enhance Your Search Functionality with an Angular Pipe

I created a custom pipe that filters the search field and displays a list of items based on the search text. Currently, it only filters by companyDisplay, but I want to also filter by companyCode and companyName.

JSON

[{
    companyDisplay: "ABC",
    companyName: "EFG",
    companyCode: "1234"
}]

Search Pipe

import { Pipe, PipeTransform } from '@angular/core'; // search text sorting
@Pipe({
    name: 'searchCompanyPipe'
})
export class SearchPipe implements PipeTransform {
  transform(items: any[], searchText: any): any[] {
    if (!items) { return []; }
    if (!searchText) { return items; }
    searchText = searchText.toLowerCase();
    return items.filter(i => {
      return (i.companyDisplay || i.companyCode).toLowerCase().includes(searchText);
    });
  }
}

HTML

<li *ngFor="let data of companyInfo | searchPipe:searchValue">

The companyInfo variable contains the JSON data.

Answer №1

One issue that arises is with the code snippet below:

(i.companyDisplay || i.companyCode)

This piece of code will always give back companyDisplay as long as it's not falsy. To properly handle this, you should consider using something similar to what's shown below:

return i.companyDisplay.toLowerCase().includes(searchText) || i.companyCode.toLowerCase().includes(searchText);

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

Issue with PassportJS and Express 4 failing to properly store cookies/session data

I have a situation with my Express 4 app using Passport 0.3.2. I've set up a passport-local strategy, and it's successfully retrieving the user information when the /session endpoint is provided with a username and password. The issue arises whe ...

Vue.js Element UI form validation - showcasing errors returned by server

Utilizing Vue.js and Element UI libraries for my current project, I have implemented front-end validation with specific rules. However, I now also require the ability to display backend errors for the current field. When the form is submitted and an error ...

Sending data to another page in React Native can be achieved by passing the values as parameters

I am currently working on passing values from one page to another using navigation. I have attempted the following code: this.props.navigation.navigate('welcome', {JSON_ListView_Clicked_Item:this.state.email,})) in the parent class where I am s ...

Stopping form submission on a jQuery form

I am in the process of implementing a password control feature on a login form using jQuery and ajax. This is the current script I have: $(document).ready(function() { $("#login-form").submit(function(e) { var csrftoken = getCookie('csr ...

Executing multiple API calls concurrently using callback functions in node.js

Waiting for the completion of tasks from two API callback functions is essential in order to utilize data from both functions. I am looking for a way to parallel execute these functions, but have been struggling with implementing async.parallel. If there ...

Tips for handling the rejection of a promise within a callback function in Node.js

How can we effectively incorporate a catch statement within a rejectionCallback function of a promise? Is it feasible or advisable to structure the code in this manner? Would the Promise object need to be passed into the rejection function in such a scena ...

Implement handleTextChange into React Native Elements custom search bar component

I need help with passing the handleTextChange function in the SearchBarCustom component. When I try to remove onChangeText={setValue} and add onchange={handleTextChange}, I am unable to type anything in the search bar. How can I successfully pass in the ...

Trouble with incrementing JavaScript dictionary values within a nested for loop

I am currently working on analyzing shark attack data with d3.js using a csv file named shark.csv. I have encountered a problem while implementing a nested for-loop in my code. The csv file contains information about shark attacks categorized by continent ...

Struggling to retrieve the ID from the API within the Angular and .NET Core component

Currently, I am working on a test project to enhance my knowledge of Angular. However, I have encountered an issue where the student's id fetched from the service is null. To handle the data, I have implemented a StudentController. Below is a snippet ...

What is the correct way to set up child page routes with query parameters in the app-routing.module.ts file for Angular?

const routes: Routes = [ { path: '', component: DraftAnalysisHomeComponent }, { path: 'nfldraftanalysis', component: DraftAnalysisHomeComponent }, { path: 'nfldraftanalysis/averagegrades', component: AverageGrade ...

Utilize the ngClass directive in conjunction with ngFor loop functionality

Currently, I am working on rendering a list of elements using the *ngFor directive in Angular. However, I have encountered an issue where only certain parts of the text within the list items should be bold based on specified requirements. I attempted to ac ...

Isn't AJAX all about the same origin policy?

Despite my confusion surrounding the same domain origin policy and jQuery AJAX, I have noticed that when I make a GET request to a URL using jQuery, I am able to successfully retrieve the results. This goes against what I understood about the restriction ...

Executing a Vue method from the main.js file within a Vue.js document

Why can't I call my method in App.vue? Shouldn't the div with id='App' in the App file allow me to access methods within it? Main.js new Vue({ render: h => h(App), methods:{ gesamt:function () { return 'Hello&a ...

Troubleshooting: Issues with Adding a New Row in Datatables using JQuery

CSS : <div class="datatable-header"> <button type="button" name="add" id="add" class="float-right btn btn-info">Add</button> </div> <div class="table-responsive"> <table ...

Tips for maintaining the state in a React class component for the UI while navigating or refreshing the page

Is there a way to persist the selection stored in state even after page navigation? I have heard that using local storage is a possible solution, which is my preferred method. However, I have only found resources for implementing this in functional compone ...

"Using Node.js to create a simulated mobile environment: a step-by-step guide

I am seeking a way to replicate the functionalities of a mobile browser using Node.js. This means that all mobile browser features should be accessible on the client-side, even if they are simulated. The goal is for webpages to believe they are being load ...

Experiencing issues with receiving null values in formData when using React hooks

Hello everyone, I am currently experiencing some challenges while utilizing React functional components hooks with formData. The issue I'm facing is that I am receiving null data in formData even though I am using useState hooks. Instead of getting th ...

Vue + TypeScript prop type issue: "'Foo' is intended as a type, but is being treated as a value in this context."

As a newcomer to TypeScript and the Vue Composition API, I encountered an error that left me puzzled: I have a component that requires an api variable as a prop, which should be of type AxiosInstance: export default defineComponent({ props: { api: A ...

How to prevent links from being affected by the Gooey effect in D3

Issue: When applying the Gooey effect, the links are also affected, resulting in a teardrop shape instead of a circle. The code snippet includes a dragged() function that allows users to detach node 1 from node 0 and reconnect them by dragging. The code s ...

Displaying data stored in a database using JSON format with Ember

I seem to be facing a challenge once again. Let me elaborate on what I am trying to achieve. Within the teammembers template, I aim to display information about Team Members and their details from a specific team by joining 3 tables. Here is an example o ...