When you click on one checkbox, the angular2-multiselect dropdown automatically selects all the boxes

<angular2-multiselect 
         [data]="sortedDataList | OrderBy : 'clientName'" [(ngModel)]="selectedItem[sortedDataList.clientId]" 
         [settings]="dropdownSettings" name="multiSelect"
        
         (onSelect)="onItemSelect($event, sortedDataList.clientId)" 
         (onDeSelect)="OnItemDeSelect($event,sortedDataList.clientId)"
         (onSelectAll)="onSelectAllItems($event)"
         (onDeSelectAll)="onDeselectAllItems($event)" disabled>
    
  <c-item>
     <ng-template let-item="item">
       <label style="color: #333;min-width: 160px;">{{item.clientName}}- {{item.clientId}}</label>
     </ng-template>
</c-item> 

</angular2-multiselect>

sortedDataList represents the list fetched from the backend and everything is functioning correctly, except for the issue with checkboxes in the dropdown. When attempting to select a single checkbox, all checkboxes get selected instead.

Answer №1

If you want to achieve this, use the "ng-multiselect-dropdown" feature.

To install:

npm install ng-multiselect-dropdown

Add it to your module (look at app.module.ts):

import { NgMultiSelectDropDownModule } from 'ng-multiselect-dropdown';
// ...

@NgModule({
imports: [
NgMultiSelectDropDownModule.forRoot()
// ...
]
// ...
})
export class AppModule {}

How to Use:

import { Component, OnInit } from '@angular/core';

export class AppComponent implements OnInit {
dropdownList = [];
selectedItems = [];
dropdownSettings = {};
ngOnInit() {
this.dropdownList = [
  { item_id: 1, item_text: 'Mumbai' },
  { item_id: 2, item_text: 'Bangaluru' },
  { item_id: 3, item_text: 'Pune' },
  { item_id: 4, item_text: 'Navsari' },
  { item_id: 5, item_text: 'New Delhi' }
];
this.selectedItems = [
  { item_id: 3, item_text: 'Pune' },
  { item_id: 4, item_text: 'Navsari' }
];
this.dropdownSettings = {
  singleSelection: false,
  idField: 'item_id',
  textField: 'item_text',
  selectAllText: 'Select All',
  unSelectAllText: 'UnSelect All',
  itemsShowLimit: 3,
  allowSearchFilter: true
};
}
onItemSelect(item: any) {
console.log(item);
}
onSelectAll(items: any) {
console.log(items);
}
}

HTML Code :

<ng-multiselect-dropdown
[placeholder]="'custom placeholder'"
[data]="dropdownList"
[(ngModel)]="selectedItems"
[settings]="dropdownSettings"
(onSelect)="onItemSelect($event)"
(onSelectAll)="onSelectAll($event)"
>
</ng-multiselect-dropdown>

For more information, visit: https://www.npmjs.com/package/ng-multiselect-dropdown

Answer №2

To ensure proper functionality, remember to include the primaryKey in your settings configuration:

this.dropdownSettings = { 
                          singleSelection: false, 
                          text:"Select Countries",
                          selectAllText:'Select All',
                          unSelectAllText:'UnSelect All',
                          enableSearchFilter: true,
                          classes:"myclass custom-class",
                          **primaryKey**:**"your_identifier"**,
                        };   

For more information, please refer to: https://github.com/CuppaLabs/angular2-multiselect-dropdown/issues/252

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

Unable to retrieve the reflective metadata of the current class instance

Is it possible to retrieve the reflect-metadata from an instance of a class? The documentation provides examples that suggest it should be achievable, but when I attempt to do so, I receive undefined as a result. Strangely enough, when I request the metada ...

Error encountered during module parsing: Unexpected token found. To resolve this issue, consider using a suitable loader to process this file format

Currently, I am in the process of learning how to develop .NET Core applications with Angular 4. My current project involves migrating an application from Core 1.1 and Angular 4.1.2 to Core 2.0 and Angular 4.3.6. In the previous version of the project, w ...

Looking for assistance in creating a unique jQuery validation function tailored to your

Looking at my form attribute, it appears as follows: onsubmit="return validateForm(this);" Additionally, I have a jQuery function set up like this: function validateForm(form) { $(form,"input").each(function() { if($(this).length < 1) { ...

The issue with ngModel in Angular is that it fails to update the value within the component

My ngModel doesn't seem to be functioning properly when I use it with a textbox in my application. This is the code snippet from app.component.html: <input type="text" [value]="name" [ngModel]="name"> Name is: {{na ...

Obtaining images from JSON data and displaying them in a modal using AngularJS

Utilizing AngularJS to retrieve a JSON file: { "albumId": 1, "id": 1, "title": "accusamus beatae ad facilis cum similique qui sunt", "url": "http://placehold.it/600/92c952", "thumbnailUrl": "http://placehold.it/150/92c952" }, and showcasing it with ...

Is there a way to automatically refresh random data from the server to the web client without the need for manual browser refresh?

Looking to send random data from the server to the web client without reloading the browser? Well, I've got you covered. Using a combination of web framework express.js, template engine pug.js, socket.io, and jQuery, you can achieve this seamlessly. ...

Leveraging AJAX for fetching files on the identical server

Just starting out with HTML and AJAX programming, so let's give it a shot: I've developed a website that populates a table with content from an external txt file (content.txt). The text file is hosted on a Windows 2003 webserver in the C:\I ...

404 Error: The requested API endpoint seems to have gone missing, as the URL cannot be found

I encountered a 404 not found error when attempting to create a new customer in my angular 10 application. Oddly, the API method works flawlessly in Postman but fails when called from the angular client. The cause of this issue is eluding me Using Postman ...

I'm having trouble getting filters to function properly with the ais-configure Widget in Algolia

I'm working with Angular's ais-configure feature and trying to implement filters in the searchParameters like this: <ais-configure [searchParameters]="{ filters: 'isPrivate:false' }"> The goal here is to filter based on a boolea ...

Transfer the chosen language from the uib-dropdown language selector to the $scope variable

Here is a HTML template that allows the user to select a language: <div class="btn-group" uib-dropdown> <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" uib-dropdown-toggle> ...

What is the best way to view and understand code from imported angular modules within vs-code?

When I [ctrl]-click on a type in VS Code, I can view the following snippet of "code" (from a compiled Angular class/module): export declare class Record extends HashMap { readonly id: number; constructor(id: number); } export declare class HashMa ...

When waiting for proxy leads to access the 'then' property, what should be my next move?

In my code, I am using a special Proxy to imitate a virtual object. The getter of this proxy returns values that are already prepared. After some exploration, I found out that when the proxy is awaited, it triggers the 'then' property of the pro ...

Angular 5 arrays within arrays

I'm currently facing an issue with using ngFor on a nested JSON output. The outer loop is working as expected, but the inner loop is not functioning properly. Below is my code snippet: Typescript file import { Component, OnInit } from '@angula ...

Do class bindings with variables need to be inline when using Vue 3?

Consider the following HTML: <template v-for="(child, index) in group"> <div :class="{'border-pink-700 bg-gray-100 ': selected === child.id}"> <div>Container Content</div> </div> & ...

Harnessing the Power of FormControlName and Labels in Angular 6

In my project using Angular 6 and reactive forms, I have a grid with a Detail button that opens a modal window displaying student information. However, when implementing the HTML for the dialog box as shown below, I encountered an error message stating: No ...

"Users have reported that the Express body-parser feature sometimes results in req.body returning

I have developed a basic Express server that utilizes the body-parser module to access POST parameters. Here is how my application is structured: /index.js: 'use strict'; const express = require('express'); const app = express(); con ...

Using JavaScript's regular expressions to locate a specific string that may span multiple lines

Is there a way to extract a specific substring from a string using regex even if the substring spans across multiple lines? I attempted to use the multiline flag in my regex pattern like this: "foo\nbar".match(/foobar/m) Unfortunately, this returns ...

What is the best approach to preserving and retrieving a Three.js scene?

I've come across loaders for individual objects and elements, but is there a way to save and load entire scenes in three.js? ...

When the JavaScript string retrieved from the database is null, it will be displayed as an empty string

Currently, my Ajax setup involves querying a database on the server with SELECT someColumn FROM someTable. The returned value of someColumn is then updated on the client side by using $('#someElement').text(someColumn); Everything works perfectl ...

The multi-level navigation bar is not displaying properly

I am currently facing an issue with my Mega menu. It displays two levels of menus perfectly fine, but I need to add a third level as shown in the image below. However, when I try to include the third level, it disrupts the design and causes the Grand Child ...