Thursday, 7 October 2021

How to use ng prime p table in angular 8 +

Implementation

Here's the final file structure for our project:

PrimeNG DatatableProject file structure

PrimeNG DatatableProject file structure

Develop Angular Application

First, install NodeJS. Go to the NodeJS downloads page and download the installer. Start the installer and install NodeJS.

Install the Angular CLI using the following command:

Shell
1

Then, create a new Angular project named "my-data".

Shell
1

Go inside the created Angular project and start the project: 

Shell
1

Go to the home page and check that everything's initially working.

Use PrimeNG Components

First, install PrimeNG.

Shell
1

You should see something like this as an output in your terminal: 

PrimeNG Datatable Install

PrimeNG Datatable Install

Next, install Prime Icons with the following command:

Shell


npm install primeicons --save


Prime Datatable Icons Install

Prime Datatable Icons Install

Then, install Font Awesome

Shell
1

Font Datatable Awesome Install

Font Awesome Install

Now, you can install the Angular CDK:

Shell


npm install @angular/cdk --save

PrimeNG Datatable Angular CDK

PrimeNG Angular CDK

If we now go to package.json, we will see the following PrimeNG dependencies:
PrimeNG package dependencies

PrimeNG package dependencies

Open the angular.json and add the following in the styles section:

JSON
1
"./node_modules/primeicons/primeicons.css",
2
  "./node_modules/primeng/resources/themes/nova-light/theme.css",
3
  "./node_modules/primeng/resources/primeng.min.css",



PrimeNG style sheet

PrimeNG style sheet

Create a new component named displayBooks, as follows:

Shell


ng generate component book-data

PrimeNG Component created

PrimeNG Component created

In the app-routing.module.ts, add the route as books for our new Books component.

JavaScript


Also, in the app.component.html file, keep only the below code and remove anything remaining:

HTML

Start the application using:

1

If we now go to localhost:4200/books, we see the following:

PrimeNG Books Component

PrimeNG Books Component

Adding the PrimeNG DataTable Component in Angular Application

For adding any PrimeNG Component in Angular, we will be following these steps:

Add PrimeNG Component workflow

Add PrimeNG Component

We will be creating the component and service modules as follows-

Add PrimeNG Datatable Component And Service

Datatable Component And Service

Add the PrimeNG Table module to the app.module.ts file.

JavaScript


We will be creating a service named, BookService, which will be getting the Book data by making an HTTP call. Currently, we will not make the HTTP call to any exposed REST service. Instead, we'll get it from a JSON file named books.json, which we will create in the assets folder.

The book.json will contain the following:

JSON


Create the BookService as follows:

Shell


ng generate service book

Add the following to BookService:

JavaScript


For making use of the httpClient, we will need to add the HttpClientModule to the app-module.ts file.

JavaScript


Modify the book-data component to get the backing data for the PrimeNG DataTable by calling the above service:

JavaScript


Use the p-table tag in the book-data.component.html file:

HTML


If we now go to localhost:4200/hello, we see the following:
PrimeNG DataTable final output

PrimeNG DataTable final output