[FIXED] how to set width for p-listbox in angular primeng?

Issue

I am trying to set the p-listbox component width to 10 REM however couldn’t with my changes. please suggest here. I am trying to apply the width on the css class. however, it does NOT apply. However, when I apply on the div with class .ui-listbox-list-wrapper in the chrome dev tools it applies just fine.

dashboard.component.ts

import { Component, OnInit } from '@angular/core';  
import { SocialLoginModule, AuthServiceConfig, AuthService } from 'angular-6-social-login';  
import { Router } from '@angular/router';  
import { SocialUsers } from '../../model/social-users';
class City {
  name:string;
  code:string;
}


@Component({  
  selector: 'app-dashboard',  
  templateUrl: './dashboard.component.html',  
  styleUrls: ['./dashboard.component.css']  
})  


export class DashboardComponent implements OnInit {  
  socialusers = new SocialUsers();  

  cities1: City[];
  selectedCity1:string;



  constructor(public OAuth: AuthService,    private router: Router) { 

    this.cities1 = [
      {name: 'New York', code: 'NY'},
      {name: 'Rome', code: 'RM'},
      {name: 'London', code: 'LDN'},
      {name: 'Istanbul', code: 'IST'},
      {name: 'Paris', code: 'PRS'}
  ];
  }  
  ngOnInit() {  
    this.socialusers = JSON.parse(localStorage.getItem('socialusers'));  
    console.log(this.socialusers);  
  }  
  logout() {  
    this.OAuth.signOut().then(data => {  
      debugger;  
      this.router.navigate(['login']);  
    });  
  }  
}

dashboard.component.html

<p-listbox id="listId" [options] ="cities1" [(ngModel)] ="selectedCity1" optionLabel = "name">

</p-listbox>

selected city={{selectedCity1?.name}}

dashboard.component.css

.ui-listbox-list-wrapper {
    width: 20REM;
}

enter image description here

Solution

you can’t change a component’s style from another component directly. you need to use ::ng-deep

dashboard.component.css

:host ::ng-deep p-listbox .ui-listbox-list{
    width: 20rem;
}

another option: primeng provides a way to change any component style by providing a custom class 🎉

<p-listbox styleClass="listbox-20rem" [options]="cities1" [(ngModel)] ="selectedCity1" optionLabel="name">
</p-listbox>

in global style file

style.css

.listbox-20rem .ui-listbox-list {
  width: 20rem;
}

check this 👉 demo 🚀🚀

Answered By – Muhammed Albarmavi

Answer Checked By – Willingham (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published