0

I have a list of items that I take from a json file via one service. At the end of the list a button can let you add every item to a list containing the elements that you have clicked/add. You can add same item only when you don't exceed the number of availability of items.

I have made a code that basically creates an object everytime you add an item, the problem is that the object build is not very easy for iterate (I have made another question regarding this problem..). When I rendered on the view it appears like a kind of object stringify

Here is part of the code:

the view:

<div ng-repeat="session in sessiones">
   Date:  {{session.date | dateformated }} &nbsp
  Availability: {{session.availability}} &nbsp
  <a ng-click="addItem($index, event, session);" ng-show="addMore"> [Add to cart] </a>

<div ng-repeat="(key, value) in cartItems">
    <h1> {{value.sessions}})</h1>
   </div> 

the function in the controller:

$scope.addItem = function(index, event, session) {
  if (session.availability>0){
       $scope.cartItems = $scope.cartItems || {};
       $scope.cartItems[event.id] = $scope.cartItems[event.id] || {title:event.title, sessions:{}}
       $scope.cartItems[event.id].sessions[session.date] =          $scope.cartItems[event.id].sessions[session.date] || {num:0}
       $scope.cartItems[event.id].sessions[session.date].num++;
        session.availability-=1;
        console.log($scope.cartItems);
    }
}

Here's my try on fiddle

Important update---> Here is an example that ilustrates very well what I want to do: just the same but instead of repeating elements when an element is already on the list I have to count the number of times that it has been added

Simple cart list

Community
  • 1
  • 1
Frnnd Sgz
  • 236
  • 1
  • 4
  • 19
  • The fiddle seems to be missing the `sessions` scope variable – Mouse Reeve Jun 19 '15 at 22:39
  • I don't know how to attached json's on fiddle...sorry! – Frnnd Sgz Jun 19 '15 at 22:43
  • @FrnndSgz just add the JSON string as a variable to the .js file. – doldt Jun 19 '15 at 22:58
  • @doldt I've tried...still getting nothing [link](http://jsfiddle.net/dph4fg66/31/) – Frnnd Sgz Jun 20 '15 at 08:51
  • @FrnndSgz change the script include method from onload to noWrap, then the basic binding will work. Also, you're trying to reference $scope outside of a controller, which throws an additional error. – doldt Jun 20 '15 at 14:25
  • @doldt I'll try: http://jsfiddle.net/dph4fg66/38/ Btw this example does exactly what I'm looking for except that I don't want to repeat elements, I want to count them. [link](http://plnkr.co/edit/Gaj2jOYIy1pxIPZwzWKS?p=preview) – Frnnd Sgz Jun 20 '15 at 14:50

1 Answers1

1

I fixed it. Fiddle

  • I didn't see the ADD+ link, so I played with that until it appeared.
  • I suspected trouble with index, but it was fine.
  • I noticed the $scope was a different color in the javascript, then noticed that you were assigning addItem to something that wasn't your controller.

So in the end I just moved this down to the bottom, enclosing addItem:

});
Amy B
  • 108,202
  • 21
  • 135
  • 185
  • I have updated the fiddle, now with your help I can finally reproduce entirely my problem...I'm gonna edit the original question with the link. – Frnnd Sgz Jun 24 '15 at 09:41