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

Separating Angular code into distinct components

My page contains two input fields: one is for email and the other is a text field. Currently, everything is functioning correctly. However, I now want to split the component into two parts. For example, I have a 'basic-info' component currently. ...

Refrain from using inline JavaScript code within your

Here is the basic structure of my code: <a href="javascript:void(0);" onClick="viewServices(1)">Services</a> <a href="javascript:void(0);" onClick="viewServices(43)">Services</a> <a href="javascript:void(0);" onClick="viewServic ...

Retrieve information from Angular 2 response

In my code, I am working with 1 component and 1 service. Here is the component element : ... constructor(private requestService : RequestService) { } ngOnInit() { this.a = this.requestService.send_request(urlWebAPI); console.log(this.a); } ... ...

Press on any two table cells to select their content, highlight it, and save their values in variables

I have a table retrieved from a database that looks like this (the number of rows may vary): |Player 1|Player 2| ------------------- |Danny |Danny | |John |John | |Mary |Mary | My goal is to select one name from each Player column and sto ...

Retrieving information from a local JSON file in Vue.js using the jQuery $.getJSON() method

Currently, I am in the process of developing a demo application using Vuejs which involves extracting map data from a local .json file. The extracted data is then used to obtain specific information like latitude and longitude values that are necessary for ...

Discover an element using Python Selenium

Can someone assist me in finding a way to click on an image using Python's Selenium? I am trying to locate a div with the class "A" that contains another div with the class "B", and then, click on the img within the div with the class "D" <di ...

Once the form has been submitted, proceed to open a new page following processing

Trying to remove an entry or offer from the database and then return to the page with all entries/offers. Attempted using Javascript, but it successfully deletes the item without redirecting to the overview page. Is this the correct approach with Javascri ...

Enhance the appearance of an HTML select tag for optimal viewing on iOS Safari browser for

I have customized three dropdown lists using CSS. I set the style as follows: .dropdown{ font-size: 20px; background: white; border:none; position: relative; } While the dropdowns appear fine in Chrome, there seems to be a slight differen ...

Issue with Component not displaying properly upon refreshing

I'm currently using react.js and I have a button with an onClick event. My goal is to reload the page after clicking the button and then display a specific component on the page. However, I've encountered an issue where the component doesn't ...

Dealing with intricate query parameters in Express.Js

Currently, I am working on developing REST APIs using Express.js. One particular express route that I have set up is as follows: /api/customer I have incorporated multiple query parameters into this route, such as: /api/customer?name=jake /api/customer?c ...

Encountering an "ionic 4 frame-ancestors *" error while attempting to watch a Twitter video

Currently, I am in the process of developing a news app using Ionic 4. I recently tackled the challenge of embedding tweets in Twitter cards successfully. However, a new issue has arisen. When a tweet includes a Youtube video, everything works perfectly ac ...

"Can someone provide guidance on extracting the ID from a JSON value and storing it in a

Hey there, I have come across this jQuery code snippet: var fbuid = zuck; var fbjson = $.getJSON("https://graph.facebook.com/"+fbuid); I am wondering how to extract the id directly from the JSON response into a variable : { id: "4", first_name: "Mark", ...

Angular JS causing text alignment issues in table cells when viewed on mobile devices

I have created a web application that requires both desktop and mobile views, utilizing Angular JS. Everything is functioning as expected except for the text alignment within tables. I attempted using <td align="left">, but it did not produce any cha ...

How can I best fill the HTML using React?

After attempting to follow various React tutorials, I utilized an API to fetch my data. Unfortunately, the method I used doesn't seem to be very efficient and the code examples I found didn't work for me. I am feeling quite lost on how to proper ...

Showing a loading animation inside an HTML element

I have a main webpage that contains several buttons. Each button, when clicked, loads a specific target page as an object within a div on the main page. The "target" refers to the page that will be displayed within the object. <script> .... chec ...

"Create a div element with resizable capabilities, similar to a

Is it possible to resize elements like divs in browsers without using jQuery or other libraries like draggable or resizable? I know text-areas can be resized by default, but I'm curious if this functionality can be extended to other elements through p ...

Code error TS2345 occurs when assigning the argument of type '{ headers: HttpHeaders; }' to a parameter of type 'RequestOptionsArgs'. This indicates a mismatch in the type of data being passed, causing an

Upon running ionic serve, these are the results that I am encountering. My setup consists of Ionic4 version with Angular 8. While executing the command, this error appears: src/app/home/home.page.ts:60:77 - error TS2345: Argument of type '{ headers ...

Utilize Javascript ES6 to Sort Through Data in a Table

I require assistance with my project using Material UI and React. There are two key implementations that I need: I am looking to create a filtering system for all rows in each column as shown in the attached image. Additionally, I need a button that can a ...

Efficiently sending a cookie with an Axios POST Request

My request is not receiving a cookie even after trying various solutions like withCredentials. I have pasted the most recent code here, can anyone spot what might be missing? var cookie_for_data = "token=test"; var host = "http://localh ...

What is the reason why the "@wordpress/scripts" package does not produce the *.asset.php file when running "npm run build"?

I have been creating custom Gutenberg blocks using npm, webpack, and @wordpress/scripts. Everything was running smoothly until I encountered an issue with the block.json file. In order to use the block.json file, I discovered that I needed a block.asset.ph ...