2

I have a page that list an item and let users add comments to it.

  1. On that page I would like to display the last 3 comments added. Any tips on how to do get the last 3 comments from JSON objects?
  2. Also when adding a new comment how can I increment the comment number (hardcoded for now)?

See my code: http://plnkr.co/edit/iOBXuQVY40LD8d8QV5ss?p=preview

Thanks

John
  • 3,529
  • 14
  • 42
  • 48
  • My answer displays the last three comments, updates the comment count (including each added comment description). Is that what you are looking to achieve? – lucuma Apr 29 '13 at 01:03
  • @lucuma any chance you could take a look at my EDIT? – John May 04 '13 at 10:21
  • 1
    I forked it and made some changes: http://plnkr.co/edit/6LKdy43jw6o4J507YfKc?p=preview I think what you had could be simplified. Check it out. – lucuma May 04 '13 at 15:13
  • Instead of asking more question please award the answer to one of the posts below. I believe they all answer your original question – lucuma May 05 '13 at 04:12
  • You've already awarded an answer so presumably you are asking a new question so can you post your edits as a new specific question. – lucuma May 08 '13 at 17:27
  • @lucuma No worries, I created a new question for it http://stackoverflow.com/questions/16448973/angular-communication-between-controllers-and-directives – John May 08 '13 at 19:46

3 Answers3

3

Look at the limitTo Filter. You can specific from the end of a list by using negative.

You could do

message in item.comments|limitTo:-3

Also for your comments, you should haven't them as a object dictionary, rather just use an array like so:

"comments":
        [
          {
            "id": "comment1",
            "name":"user1",
            "description": "This is comment 1"
          },
          {
            "id": "comment2",
            "name":"Jane D.",
            "description": "This is comment 2"
          },
          {
            "id": "comment3",
            "name":"Jone D.",
            "description": "This is comment 3"
          },
          {
            "id": "comment4",
            "name":"Test",
            "description": "This is comment 4"
          },
          {
            "id": "comment5",
            "name":"user",
            "description": "This is comment 5"
          }
        ]

If they aren't already in order you may need to add a date field of sort and sort them after you pull them in.

Here is a update plunker to help you out. Note your ng-repeat was also in the wrong place

Ryan Q
  • 10,273
  • 2
  • 34
  • 39
1

There are a couple things to note:

I changed the comments to be an array in the json. For some reason you had it as an object.

You can use the limitTo filter to limit to the last three. <li ng-repeat="message in item.comments | limitTo:-3"></li>

My answer includes the total and functioning add comment.

Controller code :

myApp.controller('ItemController', function($scope, $route, $location, $http, Items){

  Items.get(function(response){
    $scope.items = response; 

  })

  $scope.$watch('items', function() {
     $scope.total = $scope.items[0].comments.length;
  });

  $scope.addMessage = function(mess) {
    $scope.items[0].comments.push(
      {
        "name":"user1",
        "description": "This is comment " + ($scope.total + 1) 
      }
    );
  }

})

Updated Plunkr: http://plnkr.co/edit/2oWLRU06Kdp0yKqjodmv?p=preview

lucuma
  • 18,247
  • 4
  • 66
  • 91
  • 2 more questions on the same topic if that's Okay? See my EDIT. Expanding from your plunker: http://plnkr.co/edit/W5FibkcRzjKeM5iO2gtn?p=preview – John May 08 '13 at 17:02
1

Expanding on other answers: You should look at adding an ID or date to each comment. For my example solution, I've used "ID".

In my forked plunk, I've added a total, and a 'show more' button for an expandable comment list. The size is controlled by the variable 'limit'.

Also, the variable 'itemID' inside $scope.addMessage() is adding the ID to each comment. This will solve part 2 of your question.

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

$scope.addMessage (adds item, description has the value of whatever you put in the input box and resets limit back to latest 3)

  $scope.addMessage = function(mess) {
    var itemID = $scope.items[0].comments.length + 1;
    $scope.items[0].comments.push(
          {
            "id": itemID,
            "name":"user" + itemID,
            "description": mess
          }
      );
    $scope.totalItems = itemID;
    $scope.limit = 3;
  }

$scope.showMore (increases limit by 3)

  $scope.showMore = function() {
    $scope.limit += 3;
  }

JSON: I've changed it a little. 'comments' is now an array, and an 'id' is added

    "comments":
    [
      {
        "id": 1,
        "name":"user1",
        "description": "This is comment 1"
      },
      {
        "id": 2,
        "name":"Jane D.",
        "description": "This is comment 2"
      },
      ...
    ]
mnsr
  • 12,337
  • 4
  • 53
  • 79
  • Thanks for your responses guys! Comments "id" should have been added to my original code.. my bad. @rudeovski-ze-bear Your version seems close to what I was looking for. I'll test it and come back to you asap. Thanks again. – John Apr 29 '13 at 23:52
  • @Patrice no problem. Remember, if this did solve your issue, please mark it as answered. – mnsr May 01 '13 at 01:28
  • A few more questions if that's okay? See my **EDIT** and plunker http://plnkr.co/edit/w4Tpz0Tp53fXRZj7Jbbe?p=preview Added some comments to the code. – John May 01 '13 at 21:31