Skip to main content

Related Articles

PrimeNG UI Components For Angular Application

PrimeNG UI Components For Angular 9 Application

Getting Started With PrimeNG Styling in Angular App


 This chapter we are going to check how you can add PrimeNG card component to show available flowers in the stock. Before styling the app, I have added some images of flowers to our assets folder as below.


images in assets folder in angular app

In app.module.ts file you can import PrimeNG Card module as below.


import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';


import { AppRoutingModule } from './app-routing.module';

import { AppComponent } from './app.component';

import { LandingComponent } from './modules/landing/landing.component';

import { HomeComponent } from './modules/home/home.component';

import { CardModule } from 'primeng/card';

;


@NgModule({

  declarations: [

    AppComponent,

    LandingComponent,

    HomeComponent

  ],

  imports: [

    BrowserModule,

    AppRoutingModule,

    CardModule,

    

  ],

  providers: [],

  bootstrap: [AppComponent]

})

export class AppModule { }


import card module from primeng/card


Then you have to import card module in landing.component.ts file also.


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

import { flower } from '../../domain/flower';

import {CardModule} from 'primeng/card';


@Component({

  selector: 'app-landing',

  templateUrl: './landing.component.html',

  styleUrls: ['./landing.component.scss']

})

export class LandingComponent implements OnInit {


  myFlowerList:flower[]=[];

  constructor() { }

  ngOnInit() {

    this.mySellingFlowers();

  }


  mySellingFlowers(){

    let rose = new flower();

    rose.name = "Rose";

    rose.price = 100;

    rose.availableQuantity = 1000;

    this. myFlowerList.push(rose);


    let lily = new flower();

    lily.name = "Lilly";

    lily.price = 80;

    lily.availableQuantity = 2000;

    this. myFlowerList.push(lily);


    let tulip = new flower();

    tulip.name = "Tulip";

    tulip.price = 100;

    tulip.availableQuantity = 2300;

    this. myFlowerList.push(tulip);


    let carnation = new flower();

    carnation.name = "Carnation";

    carnation.price = 50;

    carnation.availableQuantity = 1500;

    this. myFlowerList.push(carnation);


    let gerbera = new flower();

    gerbera.name = "Gerbera";

    gerbera.price = 50;

    gerbera.availableQuantity = 1500;

    this. myFlowerList.push(gerbera);


    let orchid = new flower();

    orchid.name = "Orchid";

    orchid.price = 50;

    orchid.availableQuantity = 1500;

    this. myFlowerList.push(orchid);

  }

}


   trackFlowers(index,flower){

    return flower?flower.name:undefined

  }

  }



Then you can use PrimeNG card module inside your component.html file as below.


<h1>Welcome to my flower store</h1>

<div *ngFor="let flower of myFlowerList;trackBy:trackFlowers">

        <p-card header="{{flower.name}}"  [style]="{width: '360px','float':'left','margin-bottom': '2em','margin-right': '2em','background-color':'light-pink'}" styleClass="p-card-shadow">

                <img alt="Card" width="100" height="100" src="assets/stock/{{flower.name}}.jpg">

            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore sed consequuntur error repudiandae numquam deserunt

                quisquam repellat libero asperiores earum nam nobis, culpa ratione quam perferendis esse, cupiditate neque quas!</p>

                   </p-card>

    </div>


I have changed background styles in the banner in style.scss file.  


@import url('https://fonts.googleapis.com/css family=Nunito:400,700&display=swap');


$primary: indianred;


body {

    margin: 0;

    font-family: 'Nunito', 'sans-serif';

    font-size: 18px;

}


.container {

    width: 80%;

    margin: 0 auto;

}


header {

    background: $primary;

    padding: 1em 0;


    a {

        color: white;

        text-decoration: none;

        font-size: larger;

    }

    a.logo {

        font-weight: bold;

        color: white;

        font-size: larger;

    }


    nav {

        float: right;


        ul {

            list-style-type: none;

            margin: 0;

            display: flex;


            li a {

                padding: 1em;

                color:white;

                font-weight: bold;

                &:hover {

                    background: darken($primary, 10%);

                }

            }

        }

    }

}


h1 {

    margin-top: 2em;

}



Once you apply PrimeNG cards modules, app will look like below.


PrimeNG cards modules styles css example - flower store app


Let’s give a background color to the card title. For that you must override ui-card-title CSS inside your landing.component.scss file as below.
.ui-card-title{
    background-color: lightpink !important;
}



.ui-card-title css is defined inside primeng.min.scss file. To override it you must put below line of code in your landing.component.ts file.



import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { flower } from '../../domain/flower';
import {CardModule} from 'primeng/card';

@Component({
  selector: 'app-landing',
  templateUrl: './landing.component.html',
  styleUrls: ['./landing.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class LandingComponent implements OnInit {

  myFlowerList:flower[]=[];
  constructor() { }

  ngOnInit() {
    this.mySellingFlowers();
  }

  mySellingFlowers(){
    let rose = new flower();
    rose.name = "Rose";
    rose.price = 100;
    rose.availableQuantity = 1000;
    this. myFlowerList.push(rose);

    let lily = new flower();
    lily.name = "Lilly";
    lily.price = 80;
    lily.availableQuantity = 2000;
    this. myFlowerList.push(lily);

    let tulip = new flower();
    tulip.name = "Tulip";
    tulip.price = 100;
    tulip.availableQuantity = 2300;
    this. myFlowerList.push(tulip);

    let carnation = new flower();
    carnation.name = "Carnation";
    carnation.price = 50;
    carnation.availableQuantity = 1500;
    this. myFlowerList.push(carnation);

    let gerbera = new flower();
    gerbera.name = "Gerbera";
    gerbera.price = 50;
    gerbera.availableQuantity = 1500;
    this. myFlowerList.push(gerbera);

    let orchid = new flower();
    orchid.name = "Orchid";
    orchid.price = 50;
    orchid.availableQuantity = 1500;
    this. myFlowerList.push(orchid);

  }

  trackFlowers(index,flower){
    return flower?flower.name:undefined
  }
}



primeng landing.component.ts file




After applying all our changes flower store app will look like below. 


primeng flower store app complete example




Comments

Popular posts from this blog

The Power of ChatGPT and Whisper Models

A Deep Dive into Natural Language Processing Natural Language Processing (NLP) has seen a significant boost in recent years due to advancements in artificial intelligence and machine learning. Two models that have shown remarkable success in NLP are ChatGPT and Whisper. In this article, we will delve into the power of these models and their applications in the field of NLP. ChatGPT is a transformer-based language model developed by OpenAI that uses unsupervised learning to predict the next word in a sentence based on the context of previous words. ChatGPT is a generative model that is trained on large datasets of text, such as books and articles, and can be fine-tuned for specific tasks, such as question-answering or dialogue generation. ChatGPT is known for its ability to produce human-like text, making it an ideal tool for applications such as chatbots, content creation, and language translation. Whisper, on the other hand, is a paraphrasing model developed by Google that is based on

Angular PrimeNG checkboxes styling and events handling

  Getting Started With PrimeNG Styling in Angular App This chapter I am planning to show how you can handle events of the checkboxes. For that I will use checkbox for every card to place the order.  To use PrimeNG checkboxes you have to import checkbox module in app.module.ts file. import {CheckboxModule} from 'primeng/checkbox'; import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { LandingComponent } from './modules/landing/landing.component'; import { HomeComponent } from './modules/home/home.component'; import { CardModule } from 'primeng/card'; import {CheckboxModule} from 'primeng/checkbox'; @NgModule({ declarations: [ AppComponent, LandingComponent, HomeComponent ], imports: [ BrowserModule, AppRoutingModule, CardModu

Angular NgFor directive and trackby

Today we will learn about NgFor one of the core directive of angular. NgFor helps to build list and tables in Angular   Let us see the example in our flower store app. In the landing page I am going to print list of flowers that are available in the store. Later we will add images and show them in a carousel to rotate automatically. First we will create the domain class called flower.ts and you can copy paste below code.  export class flower{ constructor() { } name:string=''; price:number = 0; availableQuantity:number = 0 } To use this domain class inside another component you must specify export keyword along with the class keyword. Flower domain has 3 attributes to store name of the flower then price and the available quantity. Now we can create the array from the flower type as below inside the landing.component.ts file. myFlowerList:flower[]=[]; I will call a method inside ngOnInit to add values to the array as below. ngOnInit is one of t