0

I have two tables, Category and Products with a 1-N cardinality.

enter image description here

What I need to do is get every product that belongs to an specific category.

enter image description here

This is the html table

<tr ng-repeat="category in categories">
    <td>{{category.id}}</td>
    <td>{{category.name}}</td>
    <td>{{products[category.id]}}</td>
</tr>

And this is the controller but I don't know how to merge the categories array with the another function in order to get each object from the categories array and then make the async call with the $http service

app.controller("appController", function($scope. $http, getCategoriesService){

    // Here I get all categories
    $scope.categories = getCategoriesService.query();

    //1. This function receive an object as a parameter and then 
    // make the respective request.
    //2. So what I need to do is send every category from 'categories'
    // array and get all products by category.
    //3. I don't know how to merge this function with the array above
    function(category){
        $http("getProductsByCategory/"+category.id)
        .success(function(data){
            $scope.product[category.id];
        })
        .error(function(status){
            console.log(status)
        })
    }
});

app.factory("getCategoriesService", function($resource){
    return $resource("categories", {}, {
        listCategories: {
            method: "GET",
            isArray: true
        }
    })
})

Any idea?

2 Answers 2

1

You can handle it all in your HTML with a second ng-repeat that filters the products by current category, like this:

<tr ng-repeat="category in categories">
  <td>{{category.id}}</td>
  <td>{{category.name}}</td>
  <td>
    <table>
      <tr ng-repeat="product in products | filter: {categoryId: category.id}">
        <td>{{ product.name}}</td>
        </tr>
    </table>
  </td>
</tr>

Here's a working plunk.

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

4 Comments

Nice, please see this example plnkr.co/edit/XUMO0Vp9neORCd93v9gE?p=preview I don't know what's wrong.
@wilson - What happened to product.categoryId (Product / Category Id) in your original question?
As I'm working with JSON format, the product object has nested the category object not only its id.
@wilson - In any event, since your dealing with a nested object (category) your filter needs to be this filter: {category: {id: c.id}}
0

Once you have your categories, why wouldn't you do something like:

categories.forEach(
  category => getProducts(category);
);

With getProducts() being something like:

 function getProducts(category){
        $http("getProductsByCategory/"+category.id)
        .success(function(data){
            $scope.product[category.id];
        })
        .error(function(status){
            console.log(status)
        })
    }

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.