Ways to keep information hidden from users until they actively search for it

Currently, I have a custom filter search box that is functioning correctly. However, I want to modify it so that the data is hidden from the user until they perform a search. Can you provide any suggestions on how to achieve this?

Below is the code I am using:

  1. searchFilter.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
import { Pincodes } from '../classes/pincodes';

@Pipe({
  name: 'searchfilter'
})
export class SearchfilterPipe implements PipeTransform {

  transform(Pincodes: Pincodes[], searchValue: string): Pincodes[] {
    if (!Pincodes || !searchValue) {
      return Pincodes
    }
    return Pincodes.filter(pincode =>
      pincode.taluka.toLowerCase().includes(searchValue.toLowerCase()) ||
      pincode.village.toLowerCase().includes(searchValue.toLowerCase()) ||
      pincode.district.toLowerCase().includes(searchValue.toLowerCase()) ||
      pincode.pincode.toString().toLowerCase().includes(searchValue.toLowerCase())
    )
  }

}

  1. register-page.component.ts
import { Component, OnInit } from '@angular/core';
import { Pincodes } from '../classes/pincodes';
import { MetaDataService } from '../services/meta-data.service';
import { FormBuilder, FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { startWith } from 'rxjs-compat/operator/startWith';
import { FormGroup } from '@angular/forms';
import { DomSanitizer } from '@angular/platform-browser';
@Component({
  selector: 'app-register-page',
  templateUrl: './register-page.component.html',
  styleUrls: ['./register-page.component.css']
})
export class RegisterPageComponent implements OnInit {
  observeData: Pincodes[]
  modifiedText: any;
  PinSelected: any = {}
  searchValue: string

  constructor(private _metaDataAPI: MetaDataService) {

  }

  ngOnInit() {
    this._metaDataAPI.getData().subscribe(data => {
      this.observeData = data;
    });

  }

  onPincodeSelected(val: Pincodes) {
    console.log("value" + val);
    this.customFunction(val)

  }
  customFunction(val: Pincodes) {
    // this.modifiedText = "The Selected Pin :  " + val.pincode +
    //  " and selected District is " + val.village
    this.modifiedText = "Selected Pin : " + val.pincode + "\n" +
      "District : " + val.district + "\n" +
      "Taluka : " + val.taluka


    console.log("Modified Text: " + this.modifiedText);
    console.log("Pincode Selected: " + this.PinSelected);
    console.log("observeData Selected: " + this.observeData);

  }


}

  1. register-page.component.html
<div class="wrapper">
  <div class="main_content">
    <div class="header">
      <strong><b>Tell us more about you</b></strong>
    </div>
    <div class="info">
      <h3>Basic Details</h3>


    <div class="form-group row col-md-4 mb-3">
      <label for="search" class="col-sm-2 col-form-label">Pincode*</label>
      <div class="col-sm-6">
        <input
          type="text"
          [(ngModel)]="searchValue"
          class="form-control"
          id="search"
        />
      </div>
    </div>
    <!-- Table started -->
    <table class="table">
      <thead class="thead-dark">
        <tr>
          <th scope="col">Pincode</th>
          <th scope="col">Village</th>
          <th scope="col">Taluka</th>
          <th scope="col">District</th>
          <th scope="col">State</th>
          <th scope="col">Tick</th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let pin of observeData | searchfilter: searchValue">
          <td scope="col">{{ pin.pincode }}</td>
          <td scope="col">{{ pin.village }}</td>
          <td scope="col">{{ pin.taluka }}</td>
          <td scope="col">{{ pin.district }}</td>
          <td scope="col">{{ pin.state }}</td>
          <td scope="col">
            <mat-checkbox class="example-margin"></mat-checkbox>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

https://i.stack.imgur.com/3axL5.png

I aim to hide these details until a search query is initiated by the user, as currently all data is being displayed upfront.

Answer №1

UPDATE YOUR PIPE WITH THE FOLLOWING CODE :-

import { Pipe, PipeTransform } from '@angular/core';
import { Addresses } from '../classes/addresses';

@Pipe({
  name: 'customfilter'
})
export class CustomfilterPipe implements PipeTransform {

  transform(addresses: Addresses[], searchInput: string): Addresses[] {
    if (!addresses || !searchInput) {
      return [];
    }
    return addresses.filter(address =>
      address.street.toLowerCase().includes(searchInput.toLowerCase()) ||
      address.city.toLowerCase().includes(searchInput.toLowerCase()) ||
      address.state.toLowerCase().includes(searchInput.toLowerCase()) ||
      address.zip.toString().toLowerCase().includes(searchInput.toLowerCase())
    )
  }

}

If either the input data or the search value is missing, we will return an empty array.

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

Dynamically populate dropdown menu with values extracted from dataset

I'm currently working on a dropdown menu that needs to be updated dynamically using the data set below: this.additionalPercentages = this.offer.offerData.wellbeing.retirementPackages[0].additionalVoluntaryContributionPercentages; When I console this ...

Having trouble getting Tinymce to appear on the screen

I am currently attempting to install TinyMCE for use with my text editor in order to provide the user with a text box similar to the one on Stack Overflow. However, I am encountering an issue where it is not displaying as expected. In the header of my ind ...

Change icons in Ionic 5 when selecting a tab

How can I change my tab icons to outline when not selected and filled when selected? The Ionic 5 Tabs documentation mentions a getSelected() method, but lacks examples on its usage. I plan to utilize the ionTabsDidChange event to detect tab clicks, then ...

The functionality of the Protractor right click feature is smooth, however, there seems to be an issue with selecting

Even though I can locate the button within the context menu, I am facing difficulty in clicking it. The code mentioned below is successfully able to click the button, but an error message pops up indicating: Failed: script timeout (Session info: chr ...

using Angular and RxJS to filter out an element from an array

One of the functions in my service is a delete function. This function calls an API that returns either true or false. If the response is true, I then proceed to find the index of the item in my array, splice it out, and return the updated array. Here&apos ...

Issue with Primeng table: row grouping width does not work properly when scrollable is enabled

Currently, I am implementing a Primeng table within an Angular project. Below is the code snippet showcasing how the table is being utilized: <p-table [value]="cars" dataKey="brand" [scrollable]="'true'" scrollHeight="400px"> <ng-te ...

Hover over to reveal the button after a delay

I'm struggling with implementing a feature in my Angular code that displays a delete button when hovering over a time segment for 2 seconds. Despite trying different approaches, I can't seem to make it work. .delete-button { display: none; ...

What are the steps to correct a missing property in an established type?

Currently, I am in the process of converting an existing Node.js + express application from plain JS to TypeScript. Although I understand why I am encountering this error, I am unsure about the correct approach to resolve it. The type "Request" is coming f ...

conditionally trigger one observable in rxjs before the other

I am in need of assistance or guidance regarding a challenge I am facing with rxjs that I cannot seem to resolve. In essence, my goal is to trigger an observable and complete it before the original one is triggered. Scenario: I am currently working on a ...

Refreshing Datatable in Angular 2 - Trouble with dtInstance.then error

I'm currently working on an Angular 2 application where I have a component that includes both a dropdown list and a datatable. The requirement is to display details in the table based on the name selected from the dropdown list. HTML - <div> ...

Breaking down arrays in Typescript

Let's say I have an array like this: public taskListCustom: any=[ {title: 'Task 1', status: 'done'}, {title: 'Task 2', status: 'done'}, {title: 'Task 3', status: 'done'}, {title: 'Task ...

Error in typescript: The property 'exact' is not found in the type 'IntrinsicAttributes & RouteProps'

While trying to set up private routing in Typescript, I encountered the following error. Can anyone provide assistance? Type '{ exact: true; render: (routerProps: RouterProps) => Element; }' is not compatible with type 'IntrinsicAttribu ...

There is no value inputted into the file

I'm facing a small issue while trying to retrieve the value from my input of type="file". Here is the code snippet: <tr ng-repeat="imagenDatos in tableImagenesPunto | filter: busquedaDatosPunto " > <td>PNG</td> <td>{{imag ...

"Learn how to extract the image URL from the configuration file (config.json) within the assets folder, and then seamlessly display it within

In my Angular project, I have a configuration file located in the assets folder: { "brandConfig": "brand1", "brand1": {"urlPath": "http://192.168.168.60:8081/mantle-services", " ...

The latest update of WebStorm in 2016.3 has brought to light an error related to the experimental support for decorators, which may undergo changes in forthcoming

Hello, I recently updated to the latest WebStorm version and encountered this error message: Error:(52, 14) TS1219:Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' ...

Struggling with a TypeORM issue while attempting to generate a migration via the Command Line

Having some trouble using the TypeORM CLI to generate a migration. I followed the instructions, but when I run yarn run typeorm migration:generate, an error pops up: $ typeorm-ts-node-commonjs migration:generate /usr/bin/env: ‘node --require ts-node/regi ...

The Angular 2 Final Release is encountering an issue where it is unable to locate the module name with the

Recently, I made the transition to Angular 2 Final Release from RC 4 and encountered an issue with an error message cannot find name 'module' in my code: @Component({ selector: 'dashboard', moduleId: module.id, templateUrl: ...

The attribute interface overload in Typescript is an important concept to

Consider a scenario where there are multiple payload options available: interface IOne { type: 'One', payload: { name: string, age: number } } interface ITwo { type: 'Two', payload: string } declare type TBoth = IOne ...

Is there a tool that can automatically arrange and resolve TypeScript dependencies, with or without the use of _references.ts file?

Currently, I am working on understanding the new workflow for "_references.ts" and I feel like there is something missing when it comes to using multiple files without external modules in order to produce correctly ordered ".js" code. To start with, I took ...

What are the steps for encountering a duplicate property error in TypeScript?

I'm currently working with typescript version 4.9.5 and I am interested in using an enum as keys for an object. Here is an example: enum TestEnum { value1 = 'value1', value2 = 'value2', } const variable: {[key in TestEnum]: nu ...