In this article, we will learn how we create a nested grid in Angular 8. We are using the grid list to separate the page into small sections. In each section, we want to place a different component, where one of these components is another smaller grid.

Prerequisites

  • Basic knowledge of Angular.
  • Visual Studio Code must be installed.
  • Angular CLI must be installed.
  • Node.js must be installed.

Building the Frontend

Step 1

Let’s create an Angular project using the following npm command.

Step 2

Open the newly created project in Visual Studio code and install Bootstrap in the project. 

Now, open styles.css file and add the Bootstrap file reference. To add a reference in styles.css file, add this line:

You may also like:
Angular 7 + Spring Boot Application: Hello World Example.

Step 3

Now, let’s create a new component by using the following command.

Step 4

Now, create a new service using the following command.

Step 5

Now, open the product.component.html and paste the following code to see the HTML template. 

Step 6

After then open product.component.ts file and add the following code in this file where our logic has been written.

Step 7

Next open product.component.css file and paste the code for styling.

Step 8

At last, open product.service.ts file and add services to call our API.

 

Step 9

Its time to see the output in your terminal type ng serve -o to compile and open it in the browser. After loading our page, you can see the output like in the below images.

 

 

Here, I am taking a product, for example. The grid data is related to a product and its corresponding nested grid has information related to their parent grid.

You can check a nested grid by clicking the plus icon of the parent grid so that you can see the resulting nested grid based on the index, (i.e if you click on the very first plus icon,  you can only see its first product-related information. If you click on the second plus icon, then their product-related information would show.)

Now, the frontend is done and it’s time to add server-side code.

WEB API — Create an ASP.NET Core Application

 Follow these steps to create an ASP.NET Core application.

Step 1

In Visual Studio 2019, click on File -> New -> Project.

 

Step 2

Choose the Create option and select ASP.NET web application.

 

Step 3

Select Web API and click Ok.

 

Step 4

Now right click on controller and then add a new item.

 

Step 5

Choose Ado.net Entity Data Model and then click on Add.

 

Step 6

The next step is the EF Designer, just click on next.

 

Step 7

A new pop-up will appear. Click on next. If yours isn’t established, then click on a new connection

 

Step 8

Copy your database connection server name and paste it in the server name textbox. You will see all the databases available — select your database and click ok.  

 

Step 9

The next popup will show. Paste your database server name, choose the correct database, test the connection, and click next. Here, in the new screen, select your tables and store the procedure. Then, click finish.

Our next step is to right-click on the controllers folder and add a new controller. Name it “Product controller” and add the following namespace in the student controller.

Here is the complete code for getting all the product data and their nested product information data. 

Complete Product controller code 

  

Now, its time to enable CORS. Go to Tools, open NuGet Package Manager, search for CORS, and install the Microsoft.Asp.Net.WebApi.Cors package.

If you are running your frontend application on a different port than your server, then, to avoid a Cross-Origin-Resource-Sharing issue, you have to add the following code in the webapiconfig.cs file:

 

Building the Backend

Here, we will continue to build out our backend with SQL server. The very first step is to create a database.

Create a Database

Let’s create a database on your local SQL Server. I hope you have installed SQL Server 2017 in your machine (you can use SQL Server 2008, 2012, or 2016, as well).

Step 1

Create a database, product.

Step 2

Create a product table using the following code:

Make product ID the primary key.

Step 3

Create a product information table using the following code:

Now, it’s time to add some store procedures.

Step 4

All you have to do is paste the following code in a new query:

With this step, we have successfully completed our front end, web API, and back end coding. 

Conclusion

In this article, I have discussed how to create a nested grid in Angular 8 Application. In my next article, which is a continuation of this part, we are going to learn about the JavaScript function tolocalestring() by changing the price by country, which will give their appropriate currency format.

Please give your valuable feedback/comments/questions about this article. Please let me know if you liked and understood this article and how I could improve it.

Further Reading



Source link

Write A Comment