In one of my article I explained about how to create a Master/Detail HTML GRID using MVC and AngularJS. Few members requested me to write an article for Master/Detail html grid with CRUD (Insert, Update, Select and Delete) for both Master and Detail grid. As a result here I have created a simple demo program with the following features.

This article will explain:

  • How to Create Order Master and Order Detail table with sample records inserted.
  • Create Stored Procedure to perform Insert/Update/Select and Delete both Order Master and Order Detail table.
  • Create Entity Framework and add all the Stored Procedures.
  • Create a separate WEB API for both Order Master and Order Detail to execute all our Stored Procedures from AngularJS Controller.
  • Create AngularJS Controller to perform all business logic part to display our Master/Detail HTML grid.
  • Add Sorting /Filtering features for both Master and Detail HTML grid.
  • Display Total Row for each Child Detail Grid.
  • Add/Edit/ and Delete each Order Master and Order Detail from grid.
  • Search Order Master Details.

Prerequisites

Visual Studio 2015 - You can download it from here.

You can also view my previous articles related to AngularJS using MVC and the WCF Rest Service.

Previous articles related to Angular JS, MVC and WEB API:

Code part

1. Create Database and Table

We will create an Order Master and Order Detail table to be used for the Master and Detail Grid data binding. The following is the script to create a database, table and sample insert query. Run this script in your SQL Server. I have used SQL Server 2014.

  1. use master
  2. --create DataBase
  3. -- 1) Check for the Database Exists .If the database is exist then drop and create new DB
  4. IF EXISTS (SELECT [name] FROM sys.databases WHERE [name] = 'OrderManagement' )
  5. DROP DATABASE OrderManagement
  6. GO
  7. CREATE DATABASE OrderManagement
  8. GO
  9. USE OrderManagement
  10. GO
  11. -- Create OrderMasters Table
  12. CREATE TABLE [dbo].[OrderMasters](
  13. [Order_No] INT IDENTITY PRIMARY KEY,
  14. [Table_ID] [varchar](20) NOT NULL,
  15. [Description] [varchar](200) NOT NULL,
  16. [Order_DATE] [datetime] NOT NULL,
  17. [Waiter_Name] [varchar](20) NOT NULL
  18. )
  19. -- Insert OrderMasters sample data
  20. INSERT INTO [OrderMasters]
  21. ([Table_ID] ,[Description],[Order_DATE],[Waiter_Name])
  22. VALUES
  23. ('T1','Order for Table T1',GETDATE(),'SHANU' )
  24. INSERT INTO [OrderMasters]
  25. ([Table_ID] ,[Description],[Order_DATE],[Waiter_Name])
  26. VALUES
  27. ('T2','Order for Table T2',GETDATE(),'Afraz' )
  28. INSERT INTO [OrderMasters]
  29. ([Table_ID] ,[Description],[Order_DATE],[Waiter_Name])
  30. VALUES
  31. ('T3','Order for Table T3',GETDATE(),'Afreen')
  32. CREATE TABLE [dbo].[OrderDetails](
  33. [Order_Detail_No] INT IDENTITY PRIMARY KEY,
  34. [Order_No] INT,
  35. [Item_Name] [varchar](20) NOT NULL,
  36. [Notes] [varchar](200) NOT NULL,
  37. [QTY] INT NOT NULL,
  38. [Price] INT NOT NULL
  39. )
  40. --Now let’s insert the 3 items for the above Order No 'Ord_001'.
  41. INSERT INTO [OrderDetails]
  42. ( [Order_No],[Item_Name],[Notes],[QTY] ,[Price])
  43. VALUES
  44. (1,'Ice Cream','Need very Cold',2 ,160)
  45. INSERT INTO [OrderDetails]
  46. ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
  47. VALUES
  48. (1,'Coffee','Hot and more Suger',1 ,80)
  49. INSERT INTO [OrderDetails]
  50. ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
  51. VALUES
  52. (1,'Burger','Spicy',3 ,140)
  53. INSERT INTO [OrderDetails]
  54. ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
  55. VALUES
  56. (2,'Pizza','More Chees and Large',1 ,350)
  57. INSERT INTO [OrderDetails]
  58. ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
  59. VALUES
  60. (2,'Cola','Need very Cold',3 ,50)
  61. INSERT INTO [OrderDetails]
  62. ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
  63. VALUES
  64. (3,'IDLY','Hot',3 ,40)
  65. INSERT INTO [OrderDetails]
  66. ([Order_No],[Item_Name],[Notes],[QTY] ,[Price])
  67. VALUES
  68. (3,'Thosa','Hot',3 ,50)
  69. -- To Select and test Order Master and Details
  70. Select * FROM OrderMasters
  71. Select * From OrderDetails
After creating our Table we will create a Stored Procedure for our CRUD Operations. Firstly, we will create a stored procedure for Order Master Table to perform CRUD.
  1. -- 1) Stored procedure to Select OrderMaster
  2. -- Author : Shanu
  3. -- Create date : 2015-10-26
  4. -- Description : Order Master
  5. -- Tables used : OrderMaster
  6. -- Modifier : Shanu
  7. -- Modify date : 2015-10-26
  8. -- =============================================
  9. -- exec USP_OrderMaster_Select '',''
  10. -- =============================================
  11. Create PROCEDURE [dbo].[USP_OrderMaster_Select]
  12. (
  13. @OrderNo VARCHAR(100) = '',
  14. @Table_ID VARCHAR(100) = ''
  15. )
  16. AS
  17. BEGIN
  18. Select [Order_No],
  19. [Table_ID],
  20. [Description],
  21. [Order_DATE],
  22. [Waiter_Name]
  23. FROM
  24. OrderMasters
  25. WHERE
  26. Order_No like @OrderNo +'%'
  27. AND Table_ID like @Table_ID +'%'
  28. ORDER BY
  29. Table_ID
  30. END
  31. -- 2) Stored procedure to insert OrderMaster
  32. -- Author : Shanu
  33. -- Create date : 2015-10-26
  34. -- Description : Order Master
  35. -- Tables used : OrderMaster
  36. -- Modifier : Shanu
  37. -- Modify date : 2015-10-26
  38. -- =============================================
  39. -- exec USP_OrderMaster_Insert 'T4','Table 4','SHANU'
  40. -- =============================================
  41. Create PROCEDURE [dbo].[USP_OrderMaster_Insert]
  42. (
  43. @Table_ID VARCHAR(100) = '',
  44. @Description VARCHAR(100) = '',
  45. @Waiter_Name VARCHAR(20) = ''
  46. )
  47. AS
  48. BEGIN
  49. IF NOT EXISTS (SELECT Table_ID FROM OrderMasters WHERE Table_ID=@Table_ID)
  50. BEGIN
  51. INSERT INTO [OrderMasters]
  52. ([Table_ID] ,[Description],[Order_DATE],[Waiter_Name])
  53. VALUES
  54. (@Table_ID,@Description,GETDATE(),@Waiter_Name )
  55. Select 'Inserted' as results
  56. END
  57. ELSE
  58. BEGIN
  59. Select 'Exists' as results
  60. END
  61. END
  62. -- 3) Stored procedure to Update OrderMaster
  63. -- Author : Shanu
  64. -- Create date : 2015-10-26
  65. -- Description : Order Master
  66. -- Tables used : OrderMaster
  67. -- Modifier : Shanu
  68. -- Modify date : 2015-10-26
  69. -- =============================================
  70. -- exec USP_OrderMaster_Update 4,'T4','Table 4 wer','SHANU'
  71. -- =============================================
  72. CREATE PROCEDURE [dbo].[USP_OrderMaster_Update]
  73. ( @OrderNo Int=0,
  74. @Table_ID VARCHAR(100) = '',
  75. @Description VARCHAR(100) = '',
  76. @Waiter_Name VARCHAR(20) = ''
  77. )
  78. AS
  79. BEGIN
  80. IF NOT EXISTS (SELECT Table_ID FROM OrderMasters WHERE Order_No!=@OrderNo AND Table_ID=@Table_ID)
  81. BEGIN
  82. UPDATE OrderMasters
  83. SET [Table_ID]=@Table_ID ,
  84. [Description]=@Description,
  85. [Order_DATE]=GETDATE(),
  86. [Waiter_Name]=@Waiter_Name
  87. WHERE
  88. Order_No=@OrderNo
  89. Select 'updated' as results
  90. END
  91. ELSE
  92. BEGIN
  93. Select 'Exists' as results
  94. END
  95. END
  96. -- 4) Stored procedure to Delete OrderMaster
  97. -- Author : Shanu
  98. -- Create date : 2015-10-26
  99. -- Description : Order Master
  100. -- Tables used : OrderMaster
  101. -- Modifier : Shanu
  102. -- Modify date : 2015-10-26
  103. -- =============================================
  104. -- exec USP_OrderMaster_Delete '3'
  105. -- =============================================
  106. CREATE PROCEDURE [dbo].[USP_OrderMaster_Delete]
  107. ( @OrderNo Int=0 )
  108. AS
  109. BEGIN
  110. DELETE FROM OrderMasters WHERE Order_No=@OrderNo
  111. DELETE from OrderDetails WHERE Order_No=@OrderNo
  112. Select 'Deleted' as results
  113. END
Next we create stored procedure for Order Detail Table to perform CRUD.
  1. USE OrderManagement
  2. GO
  3. -- 1) Stored procedure to Select OrderDetails
  4. -- Author : Shanu
  5. -- Create date : 2015-10-26
  6. -- Description : OrderDetails
  7. -- Tables used : OrderDetails
  8. -- Modifier : Shanu
  9. -- Modify date : 2015-10-26
  10. -- =============================================
  11. -- exec USP_OrderDetail_Select '1'
  12. -- =============================================
  13. Create PROCEDURE [dbo].[USP_OrderDetail_Select]
  14. (
  15. @OrderNo VARCHAR(100) = ''
  16. )
  17. AS
  18. BEGIN
  19. Select Order_Detail_No,
  20. [Order_No],
  21. [Item_Name],
  22. [Notes],
  23. [QTY],
  24. [Price]
  25. FROM
  26. OrderDetails
  27. WHERE
  28. Order_No like @OrderNo +'%'
  29. ORDER BY
  30. Item_Name
  31. END
  32. -- 2) Stored procedure to insert OrderDetail
  33. -- Author : Shanu
  34. -- Create date : 2015-10-26
  35. -- Description : Order Master
  36. -- Tables used : OrderDetail
  37. -- Modifier : Shanu
  38. -- Modify date : 2015-10-26
  39. -- =============================================
  40. -- exec USP_OrderDetail_Insert 4,'cadburys','cadburys Chocolate','50',50
  41. -- =============================================
  42. Create PROCEDURE [dbo].[USP_OrderDetail_Insert]
  43. (
  44. @Order_No VARCHAR(10),
  45. @Item_Name VARCHAR(100) = '',
  46. @Notes VARCHAR(100) = '',
  47. @QTY VARCHAR(20) = '',
  48. @Price VARCHAR(20) = ''
  49. )
  50. AS
  51. BEGIN
  52. IF NOT EXISTS (SELECT Item_Name FROM OrderDetails WHERE Order_No=@Order_No AND Item_Name=@Item_Name)
  53. BEGIN
  54. INSERT INTO [OrderDetails]
  55. ( [Order_No],[Item_Name],[Notes],[QTY] ,[Price])
  56. VALUES
  57. ( @Order_No,@Item_Name,@Notes,@QTY ,@Price )
  58. Select 'Inserted' as results
  59. END
  60. ELSE
  61. BEGIN
  62. Select 'Exists' as results
  63. END
  64. END
  65. -- 3) Stored procedure to Update OrderDetail
  66. -- Author : Shanu
  67. -- Create date : 2015-10-26
  68. -- Description : Order Master
  69. -- Tables used : OrderDetail
  70. -- Modifier : Shanu
  71. -- Modify date : 2015-10-26
  72. -- =============================================
  73. -- exec USP_OrderDetail_Update 8,4,'Cadburys','cadburys Chocolate','50',50
  74. -- =============================================
  75. ALTER PROCEDURE [dbo].[USP_OrderDetail_Update]
  76. ( @Order_Detail_No Int=0,
  77. @Order_No VARCHAR(10),
  78. @Item_Name VARCHAR(100) = '',
  79. @Notes VARCHAR(100) = '',
  80. @QTY VARCHAR(20) = '',
  81. @Price VARCHAR(20) = ''
  82. )
  83. AS
  84. BEGIN
  85. IF NOT EXISTS (SELECT Item_Name FROM OrderDetails WHERE Order_Detail_No!=@Order_Detail_No AND Item_Name=@Item_Name)
  86. BEGIN
  87. UPDATE OrderDetails
  88. SET [Item_Name]=@Item_Name,
  89. [Notes]=@Notes,
  90. [QTY] =@QTY,
  91. [Price]=@Price
  92. WHERE
  93. Order_Detail_No=@Order_Detail_No
  94. AND Order_No=@Order_No
  95. Select 'updated' as results
  96. END
  97. ELSE
  98. BEGIN
  99. Select 'Exists' as results
  100. END
  101. END
  102. -- 4) Stored procedure to Delete OrderDetail
  103. -- Author : Shanu
  104. -- Create date : 2015-10-26
  105. -- Description : Order Master
  106. -- Tables used : OrderDetail
  107. -- Modifier : Shanu
  108. -- Modify date : 2015-10-26
  109. -- =============================================
  110. -- exec USP_OrderDetail_Delete '8'
  111. -- =============================================
  112. CREATE PROCEDURE [dbo].[USP_OrderDetail_Delete]
  113. ( @Order_Detail_No Int=0 )
  114. AS
  115. BEGIN
  116. DELETE from OrderDetails WHERE Order_Detail_No=@Order_Detail_No
  117. Select 'Deleted' as results
  118. END
2. Create your MVC Web Application in Visual Studio 2015

After installing our Visual Studio 2015 click Start, then Programs and select Visual Studio 2015 - Click Visual Studio 2015.

Click New, then Project, select Web and click ASP.NET Web Application. Select your project location and enter your web application name.



Select MVC and in Add Folders and Core reference for select the Web API and click OK.

Add Database using ADO.NET Entity Data Model

Right click our project and click
Add, then New Item.



Select Data, then ADO.NET Entity Data Model and give the name for our EF and click Add.



Select EF Designer from the database and click Next >.



Here click New Connection and provide your SQL Server - Server Name and connect to your database.



Here you can see I have given my SQL server name, Id and PWD and after it connected I selected the database as OrderManagement since we have created the Database using my SQL Script.



Click Next and select the tables and all the Stored Procedures need to be used and click Finish.



Here we can see now we have created our OrderDetailModel.



Once the Entity has been created the next step is to add a Web API to our controller and write the function to Select/Insert/Update and Delete.

Procedure to add our Web API Controller

Right-click the Controllers folder, click Add and then click Controller.



Select Controller and add an Empty Web API 2 Controller. Provide your name to the Web API controller and click OK. Here for my Web API Controller I have given the name “OrderAPIController". In this demo project I have created 2 different controller for Order master and order Detail.

As we have created Web API controller, we can see our controller has been inherited with ApiController.

As we all know Web API is a simple and easy way to build HTTP Services for Browsers and Mobiles.

Web API has the following four methods as
Get/Post/Put and Delete where:
  • Get is to request for the data. (Select)
  • Post is to create a data. (Insert)
  • Put is to update the data.
  • Delete is to delete data.

Get Method

In our example I have used only a Get method since I am using only a Stored Procedure. We need to create an object for our Entity and write our Get Method to do Select/Insert/Update and Delete operations.

Select Operation

We use a get method to get all the details of the OrderMasters table using an entity object and we return the result as IEnumerable. We use this method in our AngularJS and display the result in an MVC page from the AngularJS controller. Using Ng-Repeat we can bind the details.

Here we can see in the get method I have passed the search parameter to the
USP_OrderMaster_Select Stored Procedure. In the Stored Procedure I used like "%" to return all the records if the search parameter is empty.

  1. OrderManagementEntities objapi = new OrderManagementEntities();
  2. // to Search Student Details and display the result
  3. [HttpGet]
  4. public IEnumerable<USP_OrderMaster_Select_Result> Get(string OrderNO, string TableID)
  5. {
  6. if (OrderNO == null)
  7. OrderNO = "";
  8. if (TableID == null)
  9. TableID = "";
  10. return objapi.USP_OrderMaster_Select(OrderNO, TableID).AsEnumerable();
  11. }
Here in my example I have used the get method for Select/Insert/Update and Delete operations, since in my Stored Procedure after insert/update and delete I have returned the message from the database.

Insert Operation

The same as select I passed all the parameters to the insert procedure. This insert method will return the result from the database as a record is inserted or maybe not. I will get the result and display it from the AngularJS Controller to MVC application.
  1. // To Insert new Student Details
  2. [HttpGet]
  3. public IEnumerable<string> insertOrderMaster(string Table_ID,string Description,string Waiter_Name)
  4. {
  5. return objapi.USP_OrderMaster_Insert( Table_ID, Description, Waiter_Name).AsEnumerable();
  6. }
Update Operation

The same as Insert I have passed all the parameters to the insert procedure. This Update method will return the result from the database as a record is updated or maybe not. I will pass the
>OrderNo to the update procedure to update the record for the >OrderNo. I will get the result and display it from the AngularJS Controller to the MVC application.
  1. //to Update Student Details
  2. [HttpGet]
  3. public IEnumerable<string> updateOrderMaster(int OrderNo, string Table_ID, string Description, string Waiter_Name)
  4. {
  5. return objapi.USP_OrderMaster_Update(OrderNo, Table_ID, Description, Waiter_Name).AsEnumerable();
  6. }
Delete Operation

The same as update I have passed the
>OrderNo to the procedure to delete the record.
  1. //to Delete Student Details
  2. [HttpGet]
  3. public IEnumerable<string> deleteOrderMaster(int OrderNo)
  4. {
  5. return objapi.USP_OrderMaster_Delete(OrderNo).AsEnumerable();
  6. }
Same like OrderMasterController I have created another controller as “DetailAPI” for Detail table CRUD Operations. Here is the complete code for DetailController.
  1. public class DetailAPIController: ApiController
  2. {
  3. OrderManagementEntities objapi = new OrderManagementEntities();
  4. // to Search Student Details and display the result
  5. [HttpGet]
  6. public IEnumerable < USP_OrderDetail_Select_Result > Get(string OrderNO)
  7. {
  8. if(OrderNO == null) OrderNO = "0";
  9. return objapi.USP_OrderDetail_Select(OrderNO)
  10. .AsEnumerable();
  11. }
  12. // To Insert new Student Details
  13. [HttpGet]
  14. public IEnumerable < string > insertOrderDetail(string Order_No, string Item_Name, string Notes, string QTY, string Price)
  15. {
  16. return objapi.USP_OrderDetail_Insert(Order_No, Item_Name, Notes, QTY, Price)
  17. .AsEnumerable();
  18. }
  19. //to Update Student Details
  20. [HttpGet]
  21. public IEnumerable < string > updateOrderDetail(int Order_Detail_No, string Order_No, string Item_Name, string Notes, string QTY, string Price)
  22. {
  23. return objapi.USP_OrderDetail_Update(Order_Detail_No, Order_No, Item_Name, Notes, QTY, Price)
  24. .AsEnumerable();
  25. }
  26. //to Delete Student Details
  27. [HttpGet]
  28. public IEnumerable < string > deleteOrderDetail(int Order_Detail_No)
  29. {
  30. return objapi.USP_OrderDetail_Delete(Order_Detail_No)
  31. .AsEnumerable();
  32. }
  33. }
Creating AngularJs Controller

Firstly, create a folder inside the Script Folder and I have given the folder name as “MyAngular”.




Now add your Angular Controller inside the folder.

Right click the MyAngular folder and click Add and New Item. Select Web and then AngularJS Controller and provide a name for the Controller. I have named my AngularJs Controller “Controller.js”.



Once the AngularJS Controller is created, we can see by default the controller will have the code with the default module definition and all.



I have changed the preceding code like adding a Module and controller as in the following.

If the AngularJS package is missing, then add the package to your project.

Right click your MVC project and click Manage NuGet Packages. Search for AngularJS and click Install.



Procedure to Create AngularJS Script Files

Modules.js:
Here we will add the reference to the AngularJS JavaScript and create an Angular Module named “RESTClientModule”.
  1. // <reference path="../angular.js" />
  2. /// <reference path="../angular.min.js" />
  3. /// <reference path="../angular-animate.js" />
  4. /// <reference path="../angular-animate.min.js" />
  5. var app;
  6. (function () {
  7. app = angular.module("RESTClientModule", ['ngAnimate']);
  8. })();
Controllers: In AngularJs Controller I have done all the business logic and returned the data from Web API to our MVC HTML page.

1. Variable declarations

Firstly, I declared all the local variables need to be used.

  1. app.controller("AngularJs_studentsController", function ($scope, $timeout, $rootScope, $window, $http) {
  2. $scope.date = new Date();
  3. $scope.MyName = "shanu";
  4. //For Order Master Search
  5. $scope.OrderNos = "";
  6. $scope.Table_IDs = "";
  7. //This variable will be used for Insert/Edit/Delete OrderMasters Table.
  8. $scope.OrderNo = 0;
  9. $scope.Table_ID = "";
  10. $scope.Description = "";
  11. $scope.Waiter_Name = "";
  12. //Show Hide OrderMaster Table
  13. $scope.showOrderMasterAdd = true;
  14. $scope.addEditOrderMaster = false;
  15. $scope.OrderMasterList = true;
  16. $scope.showItem = true;
  17. //This variable will be used for Insert/Edit/Delete OrderDetail Table.
  18. $scope.Order_Detail_No = 0;
  19. $scope.Item_Name ="";
  20. $scope.Notes = "";
  21. $scope.QTY = "1";
  22. $scope.Price = "0";
  23. $scope.addEditOrderDetail = false;
  24. $scope.expandImg = "expand.png";

2. Methods

Select Method

In the select method I have used $http.get to get the details from Web API. In the get method I will provide our API Controller name and method to get the details. Here we can see I have passed the search parameter of
OrderNO and TableID using:

{ params: { OrderNO: OrderNos, TableID: Table_IDs }

The final result will be displayed to the MVC HTML page using data-ng-repeat.

  1. $http.get('/api/OrderAPI/',
  2. {
  3. params:
  4. {
  5. OrderNO: OrderNos,
  6. TableID: Table_IDs
  7. }
  8. })
  9. .success(function (data)
  10. {
  11. $scope.OrderMasters = data;
  12. $scope.showOrderMasterAdd = true;
  13. $scope.addEditOrderMaster = false;
  14. $scope.OrderMasterList = true;
  15. $scope.showItem = true;
  16. $scope.addEditOrderDetail = false;
  17. if($scope.OrderMasters.length > 0)
  18. {}
  19. })
  20. .error(function ()
  21. {
  22. $scope.error = "An Error has occured while loading posts!";
  23. });

Search Button Click

In the search button click I will call the SearchMethod to bind the result. Here we can see in the search text box I have used ng-model="OrderNos". Using ng-model in the AngularJS Controller we can get the TextBox input value or we can set the value to the TextBox.

  1. <input type="text" name="txtOrderNos" ng-model="OrderNos" value="" />
  2. <input type="text" name="txtTable_IDs" ng-model="Table_IDs" /><input type="submit" value="Search" style="background-color:#336699;color:#FFFFFF" ng-click="searchOrderMasters()" />
  3. //Search
  4. $scope.searchOrderMasters = function ()
  5. {
  6. selectOrderMasters($scope.OrderNos, $scope.Table_IDs);
  7. }


Insert new Order Master

In the ADD New Student Detail button click I will make visible the StudentAdd table details where the user can enter the new student information. For a new student I will make the Student ID as 0. In the New Student save button click I will call the save method.
  1. // New Student Add Details
  2. $scope.showOrderMasters = function ()
  3. {
  4. cleardetails();
  5. $scope.addEditOrderDetail = false;
  6. $scope.showOrderMasterAdd = true;
  7. $scope.addEditOrderMaster = true;
  8. $scope.OrderMasterList = true;
  9. $scope.showItem = true;
  10. }

In the Save method I will check for the OrderNo. If the OrderNo is “0” then it will insert the new Order Master details. Here I will call the Insert Web API method and if the OrderNo is > 0 then it means to update the Order record I will call the Update Web API method.



To Insert Web API Method I will pass all the Input parameters. In my Stored Procedure I will check whether the Table Name for the Order already exists. If the Table name does not exist in the database then I will insert the records and return the success message as “inserted” and if the Table name already exists then I will return the message as “Exists”.

  1. //Save OrderMaster
  2. $scope.saveDetails = function ()
  3. {
  4. $scope.IsFormSubmitted1 = true;
  5. if($scope.IsFormValid1)
  6. {
  7. if($scope.OrderNo == 0)
  8. {
  9. $http.get('/api/OrderAPI/insertOrderMaster/',
  10. {
  11. params:
  12. {
  13. Table_ID: $scope.Table_ID,
  14. Description: $scope.Description,
  15. Waiter_Name: $scope.Waiter_Name
  16. }
  17. })
  18. .success(function (data)
  19. {
  20. $scope.orderMasterInserted = data;
  21. alert($scope.orderMasterInserted);
  22. cleardetails();
  23. selectOrderMasters('', '');
  24. })
  25. .error(function ()
  26. {
  27. $scope.error = "An Error has occured while loading posts!";
  28. });
  29. }
  30. else
  31. { // to update to the student details
  32. $http.get('/api/OrderAPI/updateOrderMaster/',
  33. {
  34. params:
  35. {
  36. OrderNo: $scope.OrderNo,
  37. Table_ID: $scope.Table_ID,
  38. Description: $scope.Description,
  39. Waiter_Name: $scope.Waiter_Name
  40. }
  41. })
  42. .success(function (data)
  43. {
  44. $scope.orderMasterUpdated = data;
  45. alert($scope.orderMasterUpdated);
  46. cleardetails();
  47. selectOrderMasters('', '');
  48. })
  49. .error(function ()
  50. {
  51. $scope.error = "An Error has occured while loading posts!";
  52. });
  53. }
  54. }
  55. else
  56. {
  57. $scope.Message1 = "All the fields are required.";
  58. }
  59. }

Update Order Master

The same as Insert I will display the update details for the user to edit the details and save it. In the Edit method I will get all the details for the row where the user clicks on the Edit Icon and sets all the results to the appropriate TextBox. In the Save button click I will call the save method to save all the changes to the database like Insert.

  1. //Edit Order Details
  2. $scope.OrderMasterEdit = function OrderMasterEdit(OrderNoss, Table_IDss, Descriptionss, Waiter_Namess)
  3. {
  4. cleardetails();
  5. $scope.OrderNo = OrderNoss;
  6. $scope.Table_ID = Table_IDss
  7. $scope.Description = Descriptionss;
  8. $scope.Waiter_Name = Waiter_Namess;
  9. $scope.addEditOrderDetail = false;
  10. $scope.showOrderMasterAdd = true;
  11. $scope.addEditOrderMaster = true;
  12. $scope.OrderMasterList = true;
  13. $scope.showItem = true;
  14. }

Delete Order Master Details



In the Delete button click, I will display the confirmation message to the user whether to delete the Order or not. If the user clicks the OK button I will pass the OrderNo to the delete method of the Web API to delete the record from the database.
  1. //Delete Order master Detail
  2. $scope.OrderMasterDelete = function OrderMasterDelete(OrderNoss)
  3. {
  4. cleardetails();
  5. $scope.OrderNo = OrderNoss;
  6. var delConfirm = confirm("Are you sure you want to delete the Order Master " + OrderNoss + " ?");
  7. if(delConfirm == true)
  8. {
  9. // alert($scope.OrderNo);
  10. $http.get('/api/OrderAPI/deleteOrderMaster/',
  11. {
  12. params:
  13. {
  14. OrderNo: $scope.OrderNo
  15. }
  16. })
  17. .success(function (data)
  18. {
  19. // alert(data);
  20. $scope.orderMasterDeleted = data;
  21. alert($scope.orderMasterDeleted);
  22. cleardetails();
  23. selectOrderMasters('', '');
  24. })
  25. .error(function ()
  26. {
  27. $scope.error = "An Error has occured while loading posts!";
  28. });
  29. }
  30. }

Filter and Sorting Order Master



Kindly refer my article that explains how to perform Filtering and Sorting for HTML generated grid.

The filters can be added with the ng-repeat using the pipe symbol.

Here we can see with ng-repeat we have added the filter and for the filter we have given the TextBox Model id. When the user presses the key on the TextBox the filter will be applied for the loop and display the appropriate value as in the following:

  1. </tr>
  2. <tr style="height: 30px; background-color:#336699 ; color:#FFFFFF ;border: solid 1px #659EC7;">
  3. <td width="100" align="center" colspan="3"> <img src="~/Images/filter.png" /> Filter By </td>
  4. <td width="180" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;">
  5. <input ng-model="search.Order_No" placeholder="Order..." width="90"> </td>
  6. <td width="180" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;">
  7. <input ng-model="search.Table_ID" placeholder="Table..."> </td>
  8. <td width="200" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;"> </td>
  9. <td width="200" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;"> </td>
  10. <td width="200" align="center" style="border: solid 1px #FFFFFF; padding: 5px;table-layout:fixed;">
  11. <input ng-model="search.Waiter_Name" placeholder="Name..."> </td>
  12. </tr>

Sorting Order Master

The same as for a filter we add the orderBy with field and reverse value in ng-repat using the pipe symbol.

The OrderBy can be added with the ng-repeat using the pipe symbol. For example, let's consider the preceding example.

And in ng-repeat we will be giving the search by filter that will filter all the textbox values which we enter and produce the filtered result.

  1. <tbody data-ng-repeat="stds in OrderMasters | filter:search | orderBy:predicate:reverse">

Displaying Order Detail



Here we can see how I have displayed the Order Detail grid inside the Order Master by clicking the Detail button click.

In each Order Master Row click I will check for the active row. And then in the detail button click I called the showNewOrderDetails() method to display the details.
  1. <tr ng-show="activeRow==stds.Order_No" >

In detail button click,

  1. <input type="button" value="Add Detail" style="background-color:#439633;color:#FFFFFF;font-size:large;width:100px;
  2. border-color:#a2aabe;border-style:dashed;border-width:2px;" ng-click="showNewOrderDetails()" />
  3. // New Detail Add
  4. $scope.showNewOrderDetails = function () {
  5. clearOrderdetails();
  6. $scope.showOrderMasterAdd = false;
  7. $scope.addEditOrderMaster = false;
  8. $scope.OrderMasterList = true;
  9. $scope.showItem = true;
  10. $scope.addEditOrderDetail = true;
  11. }

For order Detail CRUD, Sorting and Filtering the same logic as we have seen for Order master has been used. Here we will see the following output:

Order Detail Add



Order Detail Edit
Order Detail Delete
Order Detail Sorting and Filtering