Guillermo
Guillermo

Reputation: 1533

AngularJS, save data in a json and retrieve it allowing angular to rebuild a dynamic table

Good morning Everyone: My angular app is a restaurant manager. Somwhere in the backend it shows a restaurant delivery manager, where delivery costs can be managed. I'm giving the option to the users (Restaurant admins) in the backend ui to add Delivery methods, and locations, so each combination has a price and it's saved to the json and displayed in a table below the inputs using ng-repeat

My json is something like this:

{
  "deliveryLocation": [
                       {
                         "id": 1,
                         "location": "downtown"
                       }
                      ],
  "deliveryMethods":    [
                         {
                           "id": 1,
                           "name": "Delivery Boy"
                         },
                         {
                           "id": 2,
                           "name": "Cab"
                         }
                        ],
  "combinations":       []

}

So when you populate the inputs, you will have at least a combination, for ex: "Downtown - Cab". That combination will be stored in "combinations" (see json) with the following format: "deliveryLocation": "", "deliveryMethods": "", "price": 0

Where price will be an empty input in the combinations table. So the user indicates how much that delivery will cost and that will be saved in the combinations array. (For ex: "deliveryLocation": "downtown", "deliveryMethods": "cab", "price": 50).

I must send the data to the backend when the user saves the table. When the user opens the delivery manager again, the app will receive the data from the backend, will receive the same json, recovering deliveryLocations and deliveryMethods it's quite straightforward, but how should I indicate to AngularJS that everything that will be found in the combinations field is a combination of references? (deliveryLocation an deliveryMethods).

Thanks so much,

Guillermo

Upvotes: 0

Views: 1836

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

1st off take a look on this good example: Fiddle

This example demonstrates how to convert JSON object to table with angularjs.

So what you need to do:

1) with angular send your table object to your (suppose PHP) server side to DB by using some factory:

module.factory('ajax_post', ['$http',  function(_http) {   

var path = 'src/php/data.ajax.php'; // example

return{
    init: function(jsonData){
        var _promise= _http.post(path, 
            jsonData
            ,{
                headers: {
                    'SOAPActions': 'http://schemas.microsoft.com/sharepoint/soap/UpdateListItems'
                }
            }
            );            
        return _promise; 
    }
  }   
}]);

By the same way load table from DB.

Upvotes: 1

Related Questions