0

I need to output a count of items from JSON by category (using .length I believe), and would like to manage this in a controller so I can place it to scope anywhere I want. How can I filter REST JSON in a controller?

My sample JSON is as follows:

[
    {
        "id": "66D5069C-DC67-46FC-8A51-1F15A94216D4",
        "articletitle": "artilce1",
        "articlecategoryid": 1,
        "articlesummary": "article 1 summary. "
    },
    {
        "id": "66D5069C-DC67-46FC-8A51-1F15A94216D5",
        "articletitle": "artilce2",
        "articlecategoryid": 2,
        "articlesummary": "article 2 summary. "
    }, 
    {
        "id": "66D5069C-DC67-46FC-8A51-1F15A94216D6",
        "articletitle": "artilce3",
        "articlecategoryid": 3,
        "articlesummary": "article 3 summary. "
    },   
    {
        "id": "66D5069C-DC67-46FC-8A51-1F15A94216D7",
        "articletitle": "artilce4",
        "articlecategoryid": 1,
        "articlesummary": "article 3 summary. "
    }, 
]

My Resource is as follows:

// Articles by ID
pfcServices.factory('pfcArticles', ['$resource', function ($resource) {
    return $resource('https://myrestcall.net/tables/articles', {},
    {
        'update': { method:'PATCH'}
    });
}]);

My Controller is as follows:

// Count number of total articles
pfcControllers.controller('pfcArticleCountCtrl', ['$scope', 'pfcArticles', function ($scope, pfcArticles) {
    $scope.articlecount = pfcArticles.query();

I believe I would need to add something to the controller (ex. $scope.cat1 = filter(my logic to get my category here), but am a little lost on how to query this so I have a count of items per category.

1
  • Thank you for the edits. I will try to follow that in the future. Commented Jan 9, 2015 at 18:09

1 Answer 1

1

Build a filter like this.

angular.module('FilterModule', [])
  .filter( 'cat1', function() {
    return function(yourJSON) {
      var count = 0;
      yourJSON.forEach(function(item) {
        switch(item.articlecategoryid) {
          case 1:
            count++;
            break;
          default:
            console.log('item is not of category 1');      
        }  
      });
      return count;
    };
  });

Inject the $filter service and fetch the cat1 filter. Now you can use it as $scope.cat1 = $filter('cat1')(yourJSON);

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

5 Comments

How would I use this for each category. Would I have it switch case for 2, etc. Would this exist with my controller that retrieves my REST call?
I suggest to create a single filter that returns count object containing counts of each category, which checks for all cases. I created a separate module for the filter. Include this module as a dependency for your app module and inject $filter into your controller. Use this filter as $scope.cat1= $filter('cat1')(yourJSON) (correction of earlier answer). For all categories count object $scope.categoryCountsObj = $filter('categoriesCount')(yourJSON);
Would something like this work as well? stackoverflow.com/questions/15273385/…
Yes. It will work. Nice approach utilizing the inbuilt text search feature. $scope.cat1Count = $filter('filter')(yourJSON, {"articlecategoryid": 1}); Don't forget to inject $filter into your controller.
Thank you. I will test and let you know how it turns out.

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.