How to Create a Nested Grid Using Angular 8
How to Create a Nested Grid Using Angular 8
In this article, we discuss how to create a nested grid with Angular 8 in order to better represent data between parent and child elements.
Join the DZone community and get the full member experience.Join For Free
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.
- Basic knowledge of Angular.
- Visual Studio Code must be installed.
- Angular CLI must be installed.
- Node.js must be installed.
Building the Frontend
Let's create an Angular project using the following npm command.
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.
Now, let's create a new component by using the following command.
Now, create a new service using the following command.
Now, open the product.component.html and paste the following code to see the HTML template.
After then open product.component.ts file and add the following code in this file where our logic has been written.
Next open product.component.css file and paste the code for styling.
At last, open product.service.ts file and add services to call our API.
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.
In Visual Studio 2019, click on File -> New -> Project.
Choose the Create option and select ASP.NET web application.
Select Web API and click Ok.
Now right click on controller and then add a new item.
Choose Ado.net Entity Data Model and then click on Add.
The next step is the EF Designer, just click on next.
A new pop-up will appear. Click on next. If yours isn't established, then click on a new connection
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.
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).
Create a database, product.
Create a product table using the following code:
Make product ID the primary key.
Create a product information table using the following code:
Now, it's time to add some store procedures.
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.
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.
Opinions expressed by DZone contributors are their own.