Implementation
Here's the final file structure for our project:
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:
npm install -g @angular/cli
Then, create a new Angular project named "my-data".
ng new my-data
Go inside the created Angular project and start the project:
ng serve
Go to the home page and check that everything's initially working.
Use PrimeNG Components
First, install PrimeNG.
npm install primeng --save
You should see something like this as an output in your terminal:
Next, install Prime Icons with the following command:
npm install primeicons --save
Then, install Font Awesome
npm install font-awesome --save
Now, you can install the Angular CDK:
npm install @angular/cdk --save
If we now go to package.json, we will see the following PrimeNG dependencies:
Open the angular.json and add the following in the styles section:
Create a new component named displayBooks
, as follows:
ng generate component book-data
In the app-routing.module.ts, add the route as books for our new Books
component.
Also, in the app.component.html file, keep only the below code and remove anything remaining:
<router-outlet></router-outlet>
Start the application using:
ng serve
If we now go to localhost:4200/books, we see the following:
Adding the PrimeNG DataTable Component in Angular Application
For adding any PrimeNG Component in Angular, we will be following these steps:
We will be creating the component and service modules as follows-
Add the PrimeNG Table module to the app.module.ts file.
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:
Create the BookService
as follows:
ng generate service book
Add the following to BookService
:
For making use of the httpClient
, we will need to add the HttpClientModule
to the app-module.ts file.
Modify the book-data
component to get the backing data for the PrimeNG DataTable by calling the above service:
Use the p-table tag in the book-data.component.html file:
If we now go to localhost:4200/hello, we see the following: