PrimeNG 6 dropdown not correctly showed

倾然丶 夕夏残阳落幕 提交于 2021-02-10 17:44:07

问题


I migrated to PrimeNG 6.1.7 and I've a problem with p-dropdown. This is my code import in app.module (taken from a simple example):

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule} from '@angular/platform-browser/animations'; 
import {DropdownModule} from 'primeng/dropdown'; // include this for dropdown support
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    DropdownModule // dropdown support
  ],
  providers: [],
  bootstrap: [AppComponent],
  schemas:
        []

})
export class AppModule { }

In app.component.ts:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  employes: SelectItem[];
  selectedEmploye: any;  

  constructor(){
    this.employes = [
      {label:'Select Employee', value:null},
      {label:'Franc', value:1},
      {label:'Kiran', value:2},
      {label:'John', value:3},
    ];
  }
ngOnInit(){
}
}

And in html I've:

<p-dropdown employes="" ngmodel="" options="" selectedemploye=""></p-dropdown>
Selected Employee: {{selectedEmploye }}

I also installed primeicons e imported it in angular.json:

...
"styles": [
   "src/styles.css",
   "node_modules/primeicons/primeicons.css"
],
...

Dropdown is not correctly showed:

Any suggestion? Very thanks...


回答1:


the correct html to use the primeng dropdown in that case should be:

<p-dropdown [options]="employes" [(ngModel)]="selectedEmploye"></p-dropdown>

If after that correction the dropdown doesn't look fine, then you have to take a look on how you installed the primeng library and if the styles are configured propertly and imported in your index.html (https://www.primefaces.org/primeng/#/setup)

<link rel="stylesheet" type="text/css" href="/node_modules/primeicons/primeicons.css" />
<link rel="stylesheet" type="text/css" href="/node_modules/primeng/resources/themes/nova-light/theme.css" />
<link rel="stylesheet" type="text/css" href="/node_modules/primeng/resources/primeng.min.css" />



回答2:


I use sass in my angular project and this how I import primeng css files every thing working fine

style.scss

@import "primeicons/primeicons.css";
@import "primeng/resources/themes/nova-dark/theme.css";
@import "primeng/resources/primeng.min.css";

primeicons is different package so make sure to install it npm install primeicons --save




回答3:


I ran into the same issue. Be sure to add the necessary dependencies via npm install, as well as the browser animation module from angular core. That said, ultimately, what resolved the issue for me was that I had to set a few extra primeNG properties listed in the documentation for autocomplete in my template code, and then I had to override the default primeNG stylings in my scss file.

Listed below is the template code and scss. Obviously, you'll want to customize according to the styling you want, but this worked for me.

PrimeNG autocomplete template code

 <p-autoComplete name="p-autoComplete" [style]="{'width':'100%'}" [inputStyle]="{'width':'100%'}" class="p-autocomplete"
      [suggestions]="listAuthors" (completeMethod)="filterAuthors($event)" [size]="40"
      field= "author" placeholder="Search Quotes" [dropdown]="true" [minLength]="1" [autoHighlight] = "true"
      [dropdown]="true" [autofocus]= "true" [style]="{'text-transform': 'uppercase'}"
      scrollable="true">
  </p-autoComplete>

scss

.ui-autocomplete-dd .ui-autocomplete-dropdown.ui-corner-all{
  position:absolute;
  transform: translateX(-100%);
}

.ui-autocomplete{
  width: 100% !important;
}
  .ui-autocomplete-input{
  width: 100% !important;
}


来源:https://stackoverflow.com/questions/53517301/primeng-6-dropdown-not-correctly-showed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!