Tips for adding a "Select All" feature to a dropdown list?

Currently, I have a dropdown list with a filter for IN and OUT values. The functionality is working as expected:

<select class="form-select" style="max-width: 100px" [ngModel]="selectedBrand" (ngModelChange)="onChangeType($event)">
    <option [value]="'IN'">IN</option>
    <option [value]="'OUT'">OUT</option>
</select>

In my TypeScript file:

  public selectedBrand: any;
  public onChangeType(type: any) {
    this.selectedBrand = type;
    console.log(this.selectedBrand);

    this.filteredCustomer = this.customerTransferts.filter(
        (item) => item.type === this.selectedBrand
    );
    console.log(this.filteredCustomer);
  }

Now, I want to enhance the functionality by adding a SELECT ALL option to display all elements of either IN or OUT.

I'm seeking guidance on what modifications are needed in my code to achieve this.

Thank you in advance for your assistance.

Answer №1

If you want to remove the filter, you can create a function like this:

resetFilter() {
    this.selectedBrand = '';
    this.filteredCustomer = this.customerTransferts
}

You can trigger this function with a button click or any other method you prefer.


Alternatively, you can add another option in your dropdown menu and handle it in the onChangeType method.

<select class="form-select" style="max-width: 100px" [ngModel]="selectedBrand" (ngModelChange)="onChangeType($event)">
    <option [value]="'ALL'">ALL</option>
    <option [value]="'IN'">IN</option>
    <option [value]="'OUT'">OUT</option>
</select>
public onChangeType(type: any) {
    if (type === 'ALL') {
      this.filteredCustomer = this.customerTransferts;
      return;
    }

    this.selectedBrand = type;
    console.log(this.selectedBrand);

    this.filteredCustomer = this.customerTransferts.filter(
        (item) => item.type === this.selectedBrand
    );
    console.log(this.filteredCustomer);
  }

It's recommended to use two-way binding for selectedBrand by changing ngModel to [(ngModel)].

<select class="form-select" style="max-width: 100px" [(ngModel)]="selectedBrand" (ngModelChange)="onChangeType($event)">

This way, the selectedBrand property will stay in sync automatically - both ways.

As a result, you can eliminate this.selectedBrand = type from the onChangeType() function.

public onChangeType(type: any) {
    if (type === 'ALL') {
      this.filteredCustomer = this.customerTransferts;
      return;
    }

    this.filteredCustomer = this.customerTransferts.filter(
        (item) => item.type === type
    );
  }

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

What is the process for creating a hover linear wipe transition using CSS/JS?

It's worth noting that I can't simply stack one image on top of the other because I'll be dealing with transparent images as well. preview of linear wipe ...

PAYEE_ACCOUNT_RESTRICTED: Restrictions have been placed on the merchant account

Currently working on integrating a PayPal payment gateway into an Angular website. Testing with sandbox client ID and secret ID has been successful, but upon switching to live credentials, I encountered the following error: Uncaught Error: PAYEE_ACCOUNT_RE ...

The dropdown feature in Bootstrap 5 seems to be malfunctioning in Angular 12

I am facing issues while trying to implement the Bootstrap 5 dropdown in Angular 12. After installing all required packages and adding them to the angular.json file, I still cannot get it to work properly. Even after copying the example directly from the ...

Building secure and responsive routes using next.js middleware

After setting up my routes.ts file to store protected routes, I encountered an issue with dynamic URLs not being properly secured. Even though regular routes like '/profile' were restricted for unauthenticated users, the dynamic routes remained a ...

Error message: ngRepeat does not allow duplicate elements in an array

Upon review, I discovered this particular piece of code: <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <body> <script> var app = angular.module("myS ...

Using React and TypeScript, open the initial tab from a mapped array with an accordion component

{accordion.map(accordionItem => ( <AccordionItem key={accordionItem.title} text={accordionItem.text} title={accordionItem.title} ></AccordionItem> ...

Retrieve the non-empty attributes of a JSON object

I have a function in Typescript that extracts specific values from a JSON data object, some of which may be empty. How can I retrieve only certain data fields? Here is the function: let datosCod; for (let get in Object.keys(transfConsData)) { co ...

Button Fails to Respond on Second Click

I have a button that triggers a JavaScript function. This function, in turn, initiates two sequential AJAX calls. Upon completion of the first call, it performs some additional tasks before proceeding to the second AJAX call. The button functions correctl ...

A function that handles HTTP request and response

In order to decrypt data, I need to retrieve the encrypted response by hitting a specific URL and then use that encrypted data along with a key to decrypt it. Initially, I attempted to fetch the data response using POSTMAN, but all I got back was a series ...

Can anyone recommend a reliable continuous integration pipeline for StrongLoop and GitHub integration?

How can you effectively develop websites using strongloop and github/bitbucket, ensuring smooth transitions from development to testing to production? I understand the key components of a successful workflow, but I'm interested in hearing about strat ...

Create a hierarchical tree structure using a string separated by dots

I am struggling with organizing a tree structure. :( My goal is to create a tree structure based on the interface below. export type Tree = Array<TreeNode>; export interface TreeNode { label: string; type: 'folder' | 'file'; ...

HighCharts velocity gauge inquiry

I recently integrated a highcharts speedometer with PHP and MYSQL on my website. Everything seemed to be working smoothly until I added the JavaScript code for the speedometer, causing it not to display. There are no error messages, just a blank screen whe ...

Issue with Angular 7 ngZone causing undefined error

I've been struggling to display a 3D object using three.js. Every time I attempt to start the animation loop (within ngAfterViewInit), I keep encountering the same error: TypeError: Cannot read property 'ngZone' of undefined In an effort t ...

javascript - Include new parameter in object literal and retrieve

I have a base object that I define using an object literal: var obj = { key1 : 'value1', key2 : 'value2' } Now, I want to pass this object to a function and extend it like this: myFunction( obj + { key3 : 'value3' ...

What could be causing BeautifulSoup to overlook certain elements on the page?

For practice, I am in the process of developing an Instagram web scraper. To handle dynamic webpages, I have opted to utilize Selenium. The webpage is loaded using: driver.execute_script("return document.documentElement.outerHTML") (Using this javascript ...

Having trouble obtaining search parameters in page.tsx with Next.js 13

Currently, I am in the process of developing a Next.js project with the Next 13 page router. I am facing an issue where I need to access the search parameters from the server component. export default async function Home({ params, searchParams, }: { ...

Would it be unwise to send an AJAX post request every two seconds?

Is it frowned upon or risky to use an AJAX $.post call (with jQuery) to a php file in order to update a specific parameter or number? $.post(file.php, {var:var}, function(data){ // do something }, json); In this scenario, only one user on a single page w ...

Error: Attempting to access 'props' property of undefined when clicking on a button within a React application leads to a TypeError

I'm currently working on implementing two buttons that will enable navigation to different pages within my React app. However, I encountered an error when attempting to use the button labeled "home." TypeError: Cannot read properties of undefined (rea ...

Switching between filters using AngularJS toggle buttons

Currently, we are in the process of converting a jQuery application into an Angular application using JavaScript. The existing jQuery app functions, but we aim to transform it into a full-fledged app utilizing the Angular framework. The main concept behin ...

Alternative solution to avoid conflicts with variable names in JavaScript, besides using an iframe

I am currently using the Classy library for object-oriented programming in JavaScript. In my code, I have implemented a class that handles canvas operations on a specific DIV element. However, due to some difficulties in certain parts of the code, I had t ...