13

Hi I just started working with angular 2. I found this library PrimeNG, I followed this tutorial: http://blog.davincisoftware.sk/primeng-web-component-framework-based-on-angularjs-2 It it all works, except the styles. They're not applying somehow and I really don't know what to do. here's what my table looks like: primeNG datatable

<div class="content-wrapper fullscreen-override">
<section class="content-header">
<H1>Users</H1>
</section>

<section class="content">
<div class="row col-lg-10 center">
<div class="box box-primary"> 
<p-dataTable [(value)]="users" selectionMode="single" [(selection)]="selectedUser" (onRowSelect)="onRowSelect($event)" rows="5" [paginator]="true" [pageLinks]="3" [rowsPerPageOptions]="[5,10,20]" [globalFilter]="gb" [responsive]="true">

    <p-column field="email" header="email" [filter]="true" filterMatchMode="contains" [sortable]="true"></p-column>
    <p-column field="first_name" header="first_name" [filter]="true" filterMatchMode="contains" [sortable]="true"></p-column>
    <p-column field="last_name" header="last_name" [filter]="true" filterMatchMode="contains" [sortable]="true"></p-column>
    <p-column field="is_superuser" header="is_superuser" [filter]="true" filterMatchMode="contains" [sortable]="true"></p-column>

    <footer>
        <div class="ui-helper-clearfix" style="width:100%">
            <button type="button" pButton icon="fa-plus" style="float:left" (click)="showDialogToAdd()" label="Add"></button>
            <button type="button" pButton icon="fa-pencil-square-o" style="float:left" (click)="showDialogToEdit()" [disabled]="selectedUser == null" label="Edit"></button>
            <button type="button" pButton icon="fa-close" style="float:left" (click)="delete()" [disabled]="selectedUser == null" label="Delete"></button>
        </div>
    </footer>
</p-dataTable>

<p-dialog header="User details" [(visible)]="displayDialog" [responsive]="true" showEffect="fade" [modal]="true">
    <div class="ui-grid ui-grid-responsive ui-fluid" *ngIf="displayableUser">
        <div class="ui-grid-row">
            <div class="ui-grid-col-4"><label for="email">email</label></div>
            <div class="ui-grid-col-8"><input pInputText id="email" [(ngModel)]="displayableUser.email" /></div>
        </div>
        <div class="ui-grid-row">
            <div class="ui-grid-col-4"><label for="name">first_name</label></div>
            <div class="ui-grid-col-8"><input pInputText id="first_name" [(ngModel)]="displayableUser.first_name" /></div>
        </div>
        <div class="ui-grid-row">
            <div class="ui-grid-col-4"><label for="surname">last_name</label></div>
            <div class="ui-grid-col-8"><input pInputText id="last_name" [(ngModel)]="displayableUser.last_name" /></div>
        </div>
        <div class="ui-grid-row">
            <div class="ui-grid-col-4"><label for="country">is_superuser</label></div>
            <div class="ui-grid-col-8"><input pInputText id="is_superuser" [(ngModel)]="displayableUser.is_superuser" /></div>
        </div>
    </div>
    <footer>
        <div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
            <button type="button" pButton icon="fa-check" (click)="save()" label="Save"></button>
        </div>
    </footer>
</p-dialog>
</div>
</div>
</section>
</div>

Above is my template.

Also, I don't quite understand how to apply my own classes on their elements.

This is my component class( I ve also tried removing the styles attribute in the decorator Component

import { Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
import { UsersFormCreation } from './modal_forms/creation/users.forms.creation';
import {DataTable,
        Column,
        TabPanel,
        TabView,
        Header,
        Footer,
        Dialog,
        Button,
        InputText} from 'primeng/primeng';
import { RequestService } from '../../common/request.service';
import {User} from './user';

const template = require('./users.template.html');
const style = require('./style.css');

@Component({
  selector: 'dashboardUsers',
  template: template,
  providers: [RequestService],
  directives: [
        ROUTER_DIRECTIVES,
        DataTable,
        Column,
        TabPanel,
        TabView,
        Header,
        Footer,
        Dialog,
        Button,
        InputText]
  styles: [style]
})
export class DashboardUsersComponent implements OnInit {
  response: string;
  api_path: string;
  users: User[];
  cols: any;
  displayableUser: User = new DisplayableUser();
  selectedUser: User;
  displayDialog: boolean;
  newUser: boolean;
  count: number;
  next: string;
  previous: string;

  constructor(public router: Router, public requestService: RequestService) {
    this.api_path = 'http://127.0.0.1:8000/users/';

  }
Miguel Rosales
  • 769
  • 4
  • 13
  • 29
  • I have moved my css files away from the directory node_modules/primeui to a public container and a few styles applied. I did a chmod 777 on the folder but it didn\t do much For some reason my styles (styles of primeui) arent being loaded. – Miguel Rosales Jul 12 '16 at 21:19

7 Answers7

28

You need to turn off encapsulation for the component.

The basic concept is that each component hides it's styles from other component so they don't overwrite each other. You can read more about encapsulation here.

...
import { ..., ViewEncapsulation } from '@angular/core';

@Component {
    ...
    encapsulation: ViewEncapsulation.None,
} ...
Keifer Caebe
  • 617
  • 6
  • 9
  • It didn't work, This is the current flow of my application app.ts->dashboard.ts->dashboard.users-> I also tried adding ViewEncapsulation to the dashboard.ts component, didn't help – Miguel Rosales Jul 12 '16 at 22:22
  • It worked for me, was trying to set .ui-datagrid-content for http://www.primefaces.org/primeng/#/datagrid and it did not work – Nylon Smile Jan 06 '17 at 04:39
  • 9
    Setting ViewEncapsulation to none is not the correct thing to do. By doing this you are switching off usage of Shadow DOM of Angular, which is one of the most important and powerful features of Angular. And by doing this you move away from component based programming. IMO there is almost never a reason to do that. You should use ` :host /deep/ ` to apply the styles to the child tree or light tree of your component. – Himanshu Arora Aug 04 '17 at 17:21
  • 3
    I do agree that entirely removing encapsulation is not an ideal solution. Do note however that /deep/, >>>, and ::ng-deep are [already deprecated in the latest version of Angular](https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep). Additionally, /deep/ and >>> are [deprecated in Chrome](https://www.chromestatus.com/features/6750456638341120). If you want to use a shadow piercing solution ::ng-deep is the best way to do so as it is maintained by the angular team, at least for now, and not dependent on browser support. – Keifer Caebe Aug 04 '17 at 18:29
24

Open your style.css file and import the styles.

@import '../node_modules/primeng/resources/themes/omega/theme.css';
@import '../node_modules/primeng/resources/primeng.min.css';
gtzinos
  • 1,205
  • 15
  • 27
  • 1
    Strangely this solution worked for me, although I followed exactly the setup page why, am I doing something wrong to have only this solution working for me especially that this line is mentionned nowhere in the documentation...? – Akyo May 16 '18 at 08:06
  • Is the way you designed your application. This solution works for most cases. – gtzinos May 16 '18 at 10:04
  • Hmm... I had the issue with the tour-of-heroes official angular source exercise, and I could fix the integration of theming of prime ng with this solution only. – Akyo May 16 '18 at 14:25
  • i was working on angular scss application and this answer worked for me. Thanks a lot :) – Sourav Sep 06 '20 at 09:52
7

Have you included primeNG css and one of the theme in your index.html?

<link rel="stylesheet" type="text/css" href="node_modules/primeui/themes/omega/theme.css" />
<link rel="stylesheet" type="text/css" href="node_modules/primeui/primeui-ng-all.min.css" />

See if this helps.

Sanket
  • 19,295
  • 10
  • 71
  • 82
  • This is mentioned on their setup page - http://www.primefaces.org/primeng/#/setup (Look for Dependencies section) – Sanket Jul 13 '16 at 11:25
  • 1
    The reference may have changed since this solution was offered, but for PrimeNg 4.1.0 the directory structure and naming for me required I reference them as: "primeng/resources/themes/omega/theme.css" "primeng/resources/primeng.min.css" – tengen Jul 13 '17 at 17:14
7

I wouldn't turn off ViewEncapsulation as your styles could bleed out and potentially cause issues in the rest of your application.

I'd recommend using the /deep/ selector to force a style down through the child component tree. This can be applied on a Class by Class basis or to multiple Classes, as below.

A single Class

:host #{"/deep/"} .yourStyle1 {
    color: #ffffff; 
}

Multiple Classes

:host #{"/deep/"} {

     .yourStyle1 { color: #ffffff; }

     .yourStyle2 { color: #000000; }
}

More Info: https://angular.io/docs/ts/latest/guide/component-styles.html

danmc
  • 1,172
  • 13
  • 11
4

Add the necessary CSS files to the styles section of your .angular-cli.json:

"styles": [
    "../node_modules/font-awesome/css/font-awesome.css",
    "../node_modules/primeng/resources/primeng.min.css",
    "../node_modules/primeng/resources/themes/omega/theme.css"
    //...
],

See also PrimeNg Setup, section "Styles Configuration".

Markus Pscheidt
  • 6,853
  • 5
  • 55
  • 76
1

The best solution for me was to add the styles to the angular.json file. Dont forget to stop your project and ng serve again after the changes:

"styles": [
                        "src/styles.scss",
                        "node_modules/primeng/resources/themes/saga-blue/theme.css",
                        "node_modules/primeng/resources/primeng.min.css"

                    ],
Pablo Varela
  • 650
  • 1
  • 5
  • 14
-4

your syntax is wrong.

In place of

<button type="button" pButton icon="fa-plus" style="float:left" (click)="showDialogToAdd()" label="Add"></button>

see the change in style syntax

you should use

<button type="button" pButton icon="fa-plus" [style]="{'float':'left'}" (click)="showDialogToAdd()" label="Add"></button>
Community
  • 1
  • 1