Angular 2's Multi-select dropdown feature allows users to select multiple options

Recently, I encountered an issue with customizing CSS for angular2-multiselect-dropdown. I found the solution in this link: https://www.npmjs.com/package/angular2-multiselect-dropdown. I have included my code below. Any assistance in resolving this matter would be greatly appreciated. Thank you in advance.

I followed the instructions provided in the link but unfortunately, it did not work for me. Here is a snippet of the HTML Page:

<div class="col-lg-10 col-md-10 col-sm-10 col-xs-10">
<angular2-multiselect [data]="itemList" [(ngModel)]="selectedItems" [settings]="settings" (onSelect)="onItemSelect($event)"
(onDeSelect)="OnItemDeSelect($event)" (onSelectAll)="onSelectAll($event)" (onDeSelectAll)="onDeSelectAll($event)">

To customize the CSS, the following code should be added to the CSS file:

.inputField {
border: 0;
outline: 0; 
background: transparent; 
border-bottom: 1px solid #7C7C81;
width: 100%;
margin-bottom: 10px; 
}
.inputField .c-token{
background: #38d574 !important;
}
.inputField .pure-checkbox label::before {
border-color: #38d574 !important;
}
.inputField .pure-checkbox input[type="checkbox"]:checked + 
label[_ngcontent-c1]:before {
background: #38d574 !important;
}

.inputField .c-btn {
border: 0 !important;
outline: 0 !important;  
background: transparent !important; 
border-bottom: 1px solid #7C7C81 !important;
width: 100% !important;
margin-bottom: 10px !important; 
}

For the .ts file:

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

@Component({
selector: 'dropdown',
templateUrl: './dropdown.component.html',
styleUrls: ['./dropdown.component.css']
})
export class DropdownComponent implements OnInit {

itemList = [];
selectedItems = [];
settings = {};

constructor() { }

ngOnInit() {

this.itemList = [
 { "id": 1, "itemName": "India" },
  { "id": 2, "itemName": "Singapore" },
  { "id": 3, "itemName": "Australia" },
  { "id": 4, "itemName": "Canada" },
  { "id": 5, "itemName": "South Korea" },
  { "id": 6, "itemName": "Brazil" }
];

this.settings = {
  text: "Select Countries",
  selectAllText: 'Select All',
  unSelectAllText: 'UnSelect All',
  classes: "myclass inputField"
};
}
onItemSelect(item: any) {
console.log(item);
console.log(this.selectedItems);
}
OnItemDeSelect(item: any) {
console.log(item);
console.log(this.selectedItems);
}
onSelectAll(items: any) {
console.log(items);
}
onDeSelectAll(items: any) {
console.log(items);
}

}

Answer №1

Consider implementing a custom class for your angular component, you're on the right track :)

The issue lies within your styling. The custom class specified in the configuration is applied to the angular2-multiselect component, so ensure your styles are structured like this:

.inputField {
    color: #ccc;
}
.inputField .c-token{
    background: #38d574 !important;
}
.inputField .pure-checkbox label::before {
    border-color: #38d574 !important;
}
.inputField .pure-checkbox input[type="checkbox"]:checked + 
 label[_ngcontent-c1]:before {
    background: #38d574 !important;
}

This example can be found in the documentation at:

To modify input styles, add this code to your scss file:

.inputField .c-btn {
    border: 0;
    outline: 0; 
    background: transparent; 
    border-bottom: 1px solid #7C7C81;
    width: 100%;
    margin-bottom: 10px; 
}

Add your styles to the global "app.css" file imported directly in the HTML view. If attaching styles in your component via styleUrls, familiarize yourself with hierarchical styles in Angular2 and use /deep/. To apply styles to your component, include :host /deep/ selectors to override all components within your DropdownComponent:

:host /deep/ .inputField .c-btn {
    border: 0;
    outline: 0; 
    background: transparent; 
    border-bottom: 1px solid #7C7C81;
    width: 100%;
    margin-bottom: 10px; 
}

For quick testing, use this code in your component:

styles: ['
    :host /deep/ .inputField .c-btn {
        border: 0;
        outline: 0; 
        background: transparent; 
        border-bottom: 1px solid #7C7C81;
        width: 100%;
        margin-bottom: 10px; 
    }
']

Instead of:

styleUrls: ['./dropdown.component.css']

Learn more about hierarchical component styles in Angular by visiting:

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

Using NodeJS, pull JSON data from a JavaScript file and render it in a route file

I am currently utilizing Nodejs Express to work with a script that generates an array of objects from the Google API. My objective is to integrate this JSON data into my templates. How can I invoke the function within my script file from my route file? Be ...

md-table Using FirebaseListObservable as a DataSource

I am currently working with a FirebaseListObservable and a BehaviorSubject that is listening for an input filter. The goal now is to merge these two entities in order to return an array that has been filtered based on the input value, which will then be us ...

send the value of a variable from a child component to its parent

I have created a typeahead component within a form component and I am trying to pass the value of the v-model from the child component to its parent. Specifically, I want to take the query model from the typeahead component and place it in the company mode ...

Creating an HTML table by populating it with data from a JavaScript array

Imagine you have a JavaScript array structured like this: [ "Heading 1", "Heading 2", "Data 1", "Data 2", "Data 3", "Data 4", ] Your task is to generate an HTML table from this array that looks like the one in this link: https://i.stack.imgur.com/7laUR. ...

Error in Laravel: The source map for js/app.js could not be located, request failed with a status code 404

I recently started using laravel and keep encountering the following error message in the browser console: Source-Map-Error: request failed with status 404 Resource-Address: Source-Map-Address: popper.js.map I'm struggling to pinpoint the ...

Tips for executing and triggering the same function concurrently from various `<LI>` elements

Is there a way to execute the same function concurrently from multiple <LI> elements using javascript or jQuery? I am looking to utilize the same function with varying parameters to create a tabbed browsing experience. I want multiple tabs to load a ...

Is there a way to display an animation when a page loads using jQuery that only plays for index.php and not other_page.php?

How can I trigger an animation on page load, specifically for my index.php page and not all other pages on my website? Is there a jQuery function that targets only the index.php page like this: $('index.php').ready(function()); If not, what i ...

Having trouble modifying the Input with split() in angularJS

I am faced with a nested JSON object that contains an array as one of its properties. Each item in the array is separated by a ';'. My goal is to use ';' as a delimiter to split each array item and make necessary changes. However, I am ...

Ensuring HTML is clean in SQL query using PHP function for sanitization

I am currently working on a PHP page that contains a dropdown form. When an option is selected, it triggers an AJAX script which queries a result from the same MySQL table. Overall, the functionality works as expected. However, I have encountered an issue ...

What exactly is the function of registerServiceWorker in React JS?

Just starting out with React and I have a question about the function of registerServiceWorker() in this code snippet: import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; import registerServi ...

Guide on producing a milky overlay using Vue js

Currently, I am utilizing Vue 3 along with Bootstrap 5 in my project. In my application, there is a button and two input fields. Upon clicking the button, I would like to display a "milky" overlay as shown in this example: https://i.sstatic.net/K21k8.png ...

Does the angular scope binding using the &(ampersand) operator only bind once or repeatedly?

Is the angular scope binding &(ampersand) a one time binding? I see it referred to as a one-way binding, but is it also one-time? Let's say I have: <my-custom-directive data-item="item" /> And my directive is declared as follows: .direct ...

Loop stops after completing one iteration

I am currently working on a program that automatically generates ASCII text based on input numbers. Essentially, when a number is provided as input to the function, it will be displayed as magnified ASCII text. For example, an input of 0123456789 would gen ...

Dynamically switch between doubling Ajax calls in JavaScript

Encountering an issue with a jQuery click function. Triggering the click event on an HTML button dynamically loads styled checkbox and toggle switches based on their stored on/off state in a database. An additional click function is added to each loaded t ...

Using the .ajax() function with jQuery to retrieve data and then using .find() on that data

Currently I am facing an issue while trying to extract the body tag from the result of the .ajax() call. Instead of getting the desired result, I only see undefined logged into the console... This is the section of code causing the problem: $(document).r ...

Tips for handling 429 errors while using axios in a react native application

My React Native app is connected to a MongoDB database using Express and Node.js, with Axios handling client-server communication. The app frequently exchanges data with the database, sometimes up to 4 requests per second when in use. While everything wor ...

Tips for implementing mixins in a Nuxt.js application using the nuxt-property-decorator package

Recently, I worked on a project where I utilized Nuxt.js with TypeScript as the language. In addition, I incorporated nuxt-property-decorator. I'm currently trying to grasp the concept of the 'mixins' property in the code snippet below: mi ...

Utilizing CSS grid layouts to ensure images expand to fit the dimensions of the designated column and

I am currently working with a CSS grid layout that is displaying as follows: .image__gallery-grid { max-height: none; grid-template-columns: repeat(4, minmax(0, 1fr)); grid-template-rows: repeat(2, minmax(0, 1fr)); gap: 2.5rem; dis ...

Conceal the Header on the Login Module

Within my app.component, I have a header and body component. <app-header></app-header> <div class="body"> <router-outlet></router-outlet> </div> I am looking to hide the header element when in the login co ...

What is the process for sending a selected option using AJAX?

Using Ajax within Laravel to save data involves a form with input fields and a select option. Here is the form structure: <form class="forms" method="get" action=""> {{ csrf_field() }} <div class="form-group row ...