如何使用两个相关实体嵌套ng-repeat

时间:2016-12-16 14:05:57

标签: angularjs

我有两个实体:

  • OrderOpened
  • ProductOrdered with relation:

    关系ManyToOne {     ProductOrdered {Order} to OrderOpened }

我需要在一个视图中列出包含相关产品的订单。

订单没问题:

 <tr ng-repeat="orderOpened in vm.orderOpeneds track by orderOpened.id">
    <td><a ui-sref="order-opened-detail({id:orderOpened.id})">{{orderOpened.id}}</a></td>
    <td>
        <a ui-sref="desk-detail({id:orderOpened.desk.id})">{{orderOpened.desk.description}}</a>
    </td>
    <td>{{orderOpened.openingTime | date:'shortTime'}}</td>
    <td>{{orderOpened.user.login}}</td>
   [...]
 </tr>

但是我想嵌套与此订单相关的产品,如:

<tr ng-repeat="productOrdered in vm.productOrdereds track by productOrdered.id">
   <td><a ui-sref="product-ordered-detail({id:productOrdered.id})">{{productOrdered.id}}</a></td>
   <td>{{productOrdered.orderedTime | date:'medium'}}</td>
  [...]
</tr>

控制器:

[...]
function loadAll() {
    OrderOpened.query(function(result) {
        vm.orderOpeneds = result; //works
        angular.forEach(vm.orderOpeneds, function(productOrdered) {
            TestOrderOpened.query({id:productOrdered.id}, function(result2) { 
                //in java: List<ProductOrdered> productOrdereds = productOrderedRepository.findAllByOrderId(id);
                vm.productOrdereds = result2;
            })
        })
    });
 }

结果我得到的产品仅与angular.forEach的最后一次迭代相关,与当前订单无关。

如何将productOrdered.id传递给ng-repeat指令或使用其他方式在一个视图中获取相关实体?

2 个答案:

答案 0 :(得分:0)

你得到的结果是正常的: 在您的控制器中,您定义了2个列表:

  • vm.orderOpeneds
  • vm.productOrdereds

有了这个,你只能显示一个productOrdereds的列表,它恰好是最后一个加载的订单之一。

如果要显示每个订单的产品列表,则必须将它们存储在某个位置。几种解决方案:

1)在每个加载的订单中,存储相关产品,即

function loadAll() {
    OrderOpened.query(function(result) {
        vm.orderOpeneds = result; //works
        angular.forEach(vm.orderOpeneds, function(productOrdered) {
            TestOrderOpened.query({id:productOrdered.id}, function(result2) { 
                //in java: List<ProductOrdered> productOrdereds = productOrderedRepository.findAllByOrderId(id);
                // CHANGE: here store the products in the order
                productOrdered.productOrdereds = result2;
            })
        })
    });
}

2)或者建立一个单独的集合,如地图,其键是订单ID,值是相关产品列表

var productsByOrderId = {}

function loadAll() {
    OrderOpened.query(function(result) {
        vm.orderOpeneds = result; //works
        angular.forEach(vm.orderOpeneds, function(productOrdered) {
            TestOrderOpened.query({id:productOrdered.id}, function(result2) { 
                //in java: List<ProductOrdered> productOrdereds = productOrderedRepository.findAllByOrderId(id);
                // CHANGE: here store the products in the map
                productsByOrderId[productOrdered.id] = result2;
            })
        })
    });
}

答案 1 :(得分:0)

您可以将产品与订单绑定。

 var results = [];
 function loadAll() {
   OrderOpened.query(function(result) {
   vm.orderOpeneds = result; //works
   angular.forEach(vm.orderOpeneds, function(productOrdered) {
        TestOrderOpened.query({id:productOrdered.id}, function(result2) { 
          // bind your product with orders
          productOrdered.productOrdereds = result2;
         // then
          results.push(productOrdered);
        });
    });
   vm.orderOpeneds = results;
  });
}