What is the process for loading data with no input provided?

I have come across a situation where my HTML table is populated with various account numbers.

https://i.sstatic.net/qJc2E.png

When I input the account number 979545130406, the filter works perfectly fine.

https://i.sstatic.net/Y4Rwk.png

However, the issue arises when I clear the value of the input (979545130406)

https://i.sstatic.net/HK3ol.png

Nothing happens at all. Is there a way to display all the records in this scenario?

I am sharing my code with you and would greatly appreciate your assistance.

template

<form #formulaire="ngForm" (ngSubmit)="onSubmit()">
<div class=" row mb-4 align-items-end ">
   <div class="col-12 col-sm-6 col-md-4 ">
      <div class="mb-2 ">
         <!-- Portefeuille -->
         <h5>Portefeuille</h5>
         <input type="input" class="form-control form-max-width" name="account" [(ngModel)]="account" placeholder="Enter account number" (input)="onInputChange()">
      </div>
   </div>
</div>
</form>

composent

export class OverviewTransfersComponent implements OnInit, OnDestroy {
   private unsubscribe$ = new Subject<void>();

   currentAgency: any;
   transferResponse: TransferResponse;
   account: string;

   constructor(
      private service: OverviewTransfersService,
      private store: Store,
      private modalService: BsModalService,
      private router: Router


   ) {
      this.account = '';
   }

   ngOnInit(): void {
      this.currentAgency = this.store.selectSnapshot(AgencyState.currentAgency);
      if (this.currentAgency) {
         this.OverviewTransfers(this.account);
      }
   }

   ngOnDestroy(): void {
      this.unsubscribe$.next();
      this.unsubscribe$.complete();
   }

   OverviewTransfers(account: string): void {
      this.service.OverviewTransfers(account).pipe(
         takeUntil(this.unsubscribe$)
      ).subscribe(res => {
         if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
            this.transferResponse = res as TransferResponse;
            console.log("data => ", this.transferResponse)
         }
      });
   }

   onSubmit(): void {
      console.log("account:", this.account);
      if (typeof this.account === 'string' && this.account.trim() !== '') {
         this.OverviewTransfers(this.account.trim());
      } else {
         alert("The wallet number field is mandatory");
      }
   }

   onInputChange(): void {
      if (typeof this.account === 'string' && this.account.trim() !== '') {
         this.OverviewTransfers(this.account.trim());
      }
   }

}

EDIT

Service

overviewTransfers(account: string): Observable<ApiResponse> {
  return this.http.post<ApiResponse>(this.api + `/AGHRAF`, {
     DEPO: account,  
     AGENCE: this.store.selectSnapshot(AgencyState.currentAgency).CODE
  });

Answer №1

It seems like your intention is for

OverviewTransfersService.OverviewTransfers
to fetch all records when given an empty string. While you haven't provided the code for this logic, that's where I would expect it to be implemented.

In Overview Transfers Service

overviewTransfers(account: string) {
  if (!string) return // request for all accounts
  return // request for specific account
}

With this approach, the component becomes much simpler. By initializing account as a string in the property declaration, there is no need for additional checks on its value.

Component

export class OverviewTransfersComponent implements OnInit, OnDestroy {
   private unsubscribe$ = new Subject < void > ();

   currentAgency: any;
   transferResponse: TransferResponse;
   account: string = '';

   constructor(
      private service: OverviewTransfersService,
      private store: Store,
      private modalService: BsModalService,
      private router: Router
   ) {}

   ngOnInit(): void {
      this.currentAgency = this.store.selectSnapshot(AgencyState.currentAgency);
      if (this.currentAgency) {
         this.overviewTransfers();
      }
   }

   ngOnDestroy(): void {
      this.unsubscribe$.next();
      this.unsubscribe$.complete();
   }

   overviewTransfers(account: string): void {
      this.service.overviewTransfers(this.account.trim()).pipe(
         takeUntil(this.unsubscribe$)
      ).subscribe(res => {
         if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
            this.transferResponse = res as TransferResponse;
            console.log("data => ", this.transferResponse)
         }
      });
   }

   onSubmit(): void {
      console.log("account:", this.account);
      this.overviewTransfers();
   }

   onInputChange(): void {
      this.overviewTransfers();
   }
}

I have opted to change the function names to camelCase for better clarity and consistency.

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

Updating is not allowed during a current state transition, like the one happening in `render` at the moment

Although this question has been asked many times before, I am still struggling to fix my code. It seems like calling setState in this way is causing an issue. I followed the example code from the material-ui website, so this should be easy, right? class ...

Performing String formatting in JavaScript using an array

I've been utilizing the stringformat library to format strings in my node.js applications. var stringFormat = require('stringformat'); stringFormat.extendString(); In my current project, I'm attempting to pass an array of parameters a ...

Leverage the power of Fluentlenium to effortlessly upload a file using dropzone.js

Currently, I am trying to create a test for file uploading using Fluentlenium and DropZone.js (). Dropzone.js operates within a modal where users can drag and drop files or upload them traditionally. However, the test crashes as soon as the upload button ...

The Ionic framework is encountering difficulties in resolving all parameters for the service

I have created an IncomeTempService: import {incomeService} from "./income.service"; export class incomeTempService { private incomeTmp: {value: number, usage: string, date: string}; private value = null; private usage = null; constructor(priva ...

Is there a way to incorporate a pixel stream within a React component?

Calling all developers experienced in customizing webpages with pixel streams - I need your help! After following Unreal's documentation on pixel streaming (), I successfully set up a pixel stream. I can modify the default stream page, but I'm s ...

Rotate the cylinder according to the normal vector of the tube endpoint

I'm working on creating a curved 3D arrow using three.js. My approach involves using a Tube along a curved path and a Cone shaped cylinder. Here's how they currently look: https://i.sstatic.net/jmVB6.png https://i.sstatic.net/P756z.png My goal ...

How is it possible for the igx-expansion-panel to close when there is a nested angular accordion present?

Currently, I am faced with the challenge of closing the igx-expansion-panel within my Angular project. While everything functions smoothly with a standard panel, things get a bit tricky when dealing with nested angular accordion structures using igx-accord ...

Creating dynamic styles for mat-menu in Angular Material

I'm trying to add a dynamic style to the content of a mat-menu. I know I can use the panelClass to assign a class, but my class needs to be dynamic. Angular provides [ngStyle] or simply [style.attribute] binding for such situations, but unfortunately ...

Statement after post is not yielding any result

Below is a javascript function that I'm struggling with: function loginsubmit() { var url = "../php/loginsubmit.php"; var data = ""; ajaxRequest(url, "POST",data , true, insertNewBody); } This function is responsible for sending an ajax ...

Tips for effectively utilizing node modules that have yet to be installed without encountering any errors

I am looking to create a setup script for my NodeJS application. The script will: Establish system roles for users Create a root category for posts Finally, create a system admin user for the initial login These details will be saved in a database. Ad ...

Serve the JS files in Express separately

I am facing issues with my webapp loading too slowly when using the express.static middleware to serve all my js files. I am considering serving each js file only when needed, such as when serving the html from jade that uses the js file. I have attempted ...

Allow images to be uploaded using the browser-policy package in Meteor

Can anyone help me figure out how to use Sir Trevor JS in Meteor for uploading images without encountering this error message? When attempting to load the image 'blob:http%3A//localhost%3A3000/a28ef7dc-ee51-4290-9941-6b8fc317e685', I am receivin ...

Generating radio buttons dynamically and automatically selecting the correct button using AngularJS

In the questionnaire form, each question is looped over along with its answers to create radio buttons for each answer. The answer object contains a property called answered, which can be true or false depending on whether the answer has been previously ...

Testing Tools for AJAX Integration

Searching for AJAX testing resources. Are there any no-cost tools out there for conducting AJAX tests? Edit 2: Besides Firebug, are there any other tools available? ;) ...

Can ngx permissions in angular be trusted for ACL management?

Currently working with Angular 6 and looking to manage user access control. While the conventional wisdom is to handle ACL in the backend, there are packages like ngx permissions available for managing it on the frontend. Is it secure to utilize these fe ...

Placing text inside a designated element on a different web page upon clicking a button

Imagine a scenario where a user is browsing a website and lands on the 'prices' page. They come across a product that catches their eye and decide to click on the associated button. This action redirects them to the 'contact us' page, w ...

How to pass a JSON object to a component's constructor in Angular 2

In a unique situation, my primary component config.editor.component performs extensive processing and generates various JSON objects. I am content with this approach. The template for this component then proceeds to render another component - api.entry.com ...

show an HTML webpage within a <div> container using AJAX technology

I am trying to include an HTML page named Introduction.html (located in the same folder as x.html) within div1. However, it does not seem to be loading properly. Below is a snippet of the code from x.html x.html <!DOCTYPE html> <h ...

How can you use ui.router to set up one controller as the child of another controller?

To create a nested controller in html, you can simply write the child controller inside the parent controller like this: <div ng-controller="parentCtrl"> <div ng-controller="childCtrl"> When using ui.router, you can specify one state as a c ...

Unable to handle JQuery POST to PHP in success function

I am struggling with a jQuery post function that is supposed to call a PHP script in order to retrieve a value from the database. Although I can see in Firebug that the PHP file is being called and returning a 200 OK status, the success function in my JS ...