0

I'm trying to get an object like this:

$scope.order = {
        'client' : '24',
        'products' : [
            {'id': '23', 'format' : 'box', 'units': '3'},
            {'id': '33', 'format' : 'can', 'units': '24'},
            {'id': '11', 'format' : 'box', 'units': '4'}
        ]
    }

having the next controller

 (function(){  
        var app = angular.module('myApp',[]);

        app.controller('Controller', function($scope){
            //data from service
            var items = [
              {'id':1, 'name': 'redItem'},
              {'id':2, 'name': 'blueItem'},
              {'id':3, 'name': 'yellowItem'},
              {'id':4, 'name': 'greenItem'},
            ];

            $scope.products = items;

            $scope.order = {
                    'client' : '12',
            };
            $scope.order.products = {};

            $scope.show = function(productID){
                console.log($scope.order.products);
                console.log($scope.order);  
            };
        });
    })();

and view

<ul class="list">
<li ng-repeat="product in products | orderBy: 'name'">
    <p>
        <a ng-bind='product.name'></a>
        <input type="checkbox" ng-change="show()" ng-model="order.products.id[$index]" value="1" ng-true-value="{{product.id}}" ng-false-value="0">
        <input type="number" ng-model="order.products.units[$index]" min="1" ng-show="order.products.id[$index]"/>          
    </p>
</li>

I'm trying to update de object every time the checkbox is changed, adding the product id if checked and units of the input number but the structure I'm getting is wrong

$scope.order = {
    "client":"12",
    "products":{
      "id":{"0":1,"1":2,"3":4},
      "units":{"1":2,"3":1}
    }
}

any clues??

EDIT:

I forgot the plunker... plunker

1
  • here is the plunker @itcouldevenbeaboat Commented Feb 19, 2015 at 20:53

2 Answers 2

1

In your view:

<ul class="list">
<li ng-repeat="product in products | orderBy: 'name'">
    <p>
        <a ng-bind='product.name'></a>
        <input type="checkbox" ng-change="show()" ng-model="order.products.id[$index]" value="1" ng-true-value="{{product.id}}" ng-false-value="0">
        <input type="number" ng-model="order.products.units[$index]" min="1" ng-show="order.products.id[$index]"/>          
    </p>
</li>

You are creating an array called id by using the model order.products.id[$index] and an array called units by using the model order.products.units[$index]. You are actually trying to access a product in the order.products[] array. Rather than jump through the hoop of creating a new object, I would mark the item as selected and track its units. You can then filter to just the selected products and do any desired transformation when the order is completed.

Controller:

(function(){

  var app = angular.module('myApp',[]);

  app.controller('Controller', function($scope, $filter){
    //data from service
    var items = [
      {'id':1, 'name': 'redItem'},
      {'id':2, 'name': 'blueItem'},
      {'id':3, 'name': 'yellowItem'},
      {'id':4, 'name': 'greenItem'},
    ];

    $scope.products = items;

    $scope.order = {
            'client' : '12',
    };

    $scope.show = function(productID){
        console.log($scope.order.products);
        console.log($scope.order);

        $scope.order.products = $filter('filter')($scope.products, { isSelected: true }, true);
    };
  });

})();

View:

<!DOCTYPE html>
<html ng-app='myApp'>

  <head>
    <script data-require="angular.js@*" data-semver="1.4.0-beta.4" src="https://code.angularjs.org/1.4.0-beta.4/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-controller="Controller">
      <ul class="list">
          <li ng-repeat="product in products">
              <p>
                <a ng-bind='product.name'></a>
                  <input type="checkbox" ng-change="show(product.id)" ng-model="product.isSelected" />
                  <input type="number" ng-model="product.units" min="1" ng-show="product.isSelected" />
              </p>
          </li>
      </ul>
      <p>{{order}}</p>
    </div>
  </body>

</html>

Plunker: http://plnkr.co/edit/1DYsSYF6Fscxto6vykJU?p=preview

Sign up to request clarification or add additional context in comments.

2 Comments

though the {{order}} in the view shows the object correctly the first time a checkbox is changed {"client":"12","products":[{"id":1,"name":"redItem","isSelected":true}]}, the console.log($scope.order) in the controller logs Object {client: "12"} and console.log($scope.order.products) got undefined @Ben Jaspers
Yes, the products array isn't defined until after that first console.log. You can just move the filter statement above the console.log statements. I updated my Plunker with that.
0

Modified the ng-model and ng-show to reflect array instead of object and similarly modified the js code

Plunker: http://plnkr.co/edit/TNBZgKNDTDhiLt3yOJkB?p=preview

<input  type="checkbox" ng-change="show(product.id)" ng-model="order.products[$index].id" value="1" ng-true-value="{{product.id}}" ng-false-value="0">
<input type="number" ng-model="order.products[$index].units" min="1" ng-show="order.products[$index].id"/>

$scope.order.products = [];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.