无法在 table -AngularJS 中显示正确的数据
Not able to display correct data in table -AngularJS
下面是我的 plunker,我试图在其中显示每个 category.But 数据未按预期打印的数据。示例:-rom 值 12345 应该直接位于面包下。
我不确定我的代码哪里做错了:
$scope.allProducts = [];
angular.forEach($scope.data,function(item, index){
angular.forEach(item.products, function(item2, index){
if($scope.allProducts.indexOf(item2.consummable) == -1){
$scope.allProducts.push(item2.consummable);
}
})
})
请检查结构:
<table style="border : 1px solid">
<tr>
<td></td>
<td ng-repeat="product in allProducts">
{{product.consummable}}
</td>
</tr>
<tr ng-repeat="test in data">
<td>{{test.resource}}</td>
<td ng-repeat="pro in allProducts">{{pro.rom}}</td>
</tr>
并且只需在 allProducts 中推送 item2 而不是仅在 consummable 字段中推送 :
angular.forEach($scope.data,function(item, index){
angular.forEach(item.products, function(item2, index){
if($scope.allProducts.indexOf(item2.consummable) == -1){
$scope.allProducts.push(item2); // Here I have pushed item2 instead of item2.consummable
}
})
})
另一个变体 ng-repeat
表达式。由于您在第一行重复了 allProducts,因此您需要重复它并在其他行中重复,并为所选值过滤数据。请参阅下面的代码片段
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.data =[
{
"resource": "100",
products: [
{
"consummable": "milk",
"rom": 1
},
]
},
{
"resource": "200",
products: [
{
"consummable": "bread",
"rom": 12345
},
]
},
{
"resource": "300",
products: [
{
"consummable": "butter",
"rom": 123456789
}
]
}
];
$scope.allProducts = [];
angular.forEach($scope.data,function(item, index){
angular.forEach(item.products, function(item2, index){
if($scope.allProducts.indexOf(item2.consummable) == -1){
$scope.allProducts.push(item2.consummable);
}
})
})
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.17/angular.min.js"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<table style="border:1px solid red;">
<tr>
<td>
</td>
<td ng-repeat="itemProd in allProducts" style="border:1px solid red;">
{{itemProd}}
</td>
</tr>
<tr ng-repeat="item in data">
<td>
{{item.resource}}
</td>
<td ng-repeat="item2 in allProducts" style="border:1px solid red;" ng-init="product=(item.products | filter: {consummable:item2})[0]">
<a>{{product.rom}}</a>
</td>
</tr>
</table>
{{allProducts}}
{{data}}
</div>
您需要的解决方案如下:
1) 保持 allProducts 生成如下:
$scope.allProducts = [];
angular.forEach($scope.data,function(item, index){
angular.forEach(item.products, function(item2, index){
if($scope.allProducts.indexOf(item2.consummable) == -1){
$scope.allProducts.push(item2.consummable);
}
})
})
2) 在您的控制器中添加:
$scope.getColumnValue = function(item,item2, index) {
var returnVal;
angular.forEach(item.products, function(val, index){
if(item2 == val.consummable){
returnVal = val.rom;
}
})
return returnVal;
}
3) table 代将是:
<table style="border:1px solid red;">
<tr>
<td>
</td>
<td ng-repeat="itemProd in allProducts" style="border:1px solid red;">
{{itemProd}}
</td>
</tr>
<tr ng-repeat="item in data">
<td>
{{item.resource}}
</td>
<td ng-repeat="item2 in allProducts" style="border:1px solid red;">
<a>{{getColumnValue(item, item2)}}</a>
</td>
</tr>
</table>
下面是我的 plunker,我试图在其中显示每个 category.But 数据未按预期打印的数据。示例:-rom 值 12345 应该直接位于面包下。 我不确定我的代码哪里做错了:
$scope.allProducts = [];
angular.forEach($scope.data,function(item, index){
angular.forEach(item.products, function(item2, index){
if($scope.allProducts.indexOf(item2.consummable) == -1){
$scope.allProducts.push(item2.consummable);
}
})
})
请检查结构:
<table style="border : 1px solid">
<tr>
<td></td>
<td ng-repeat="product in allProducts">
{{product.consummable}}
</td>
</tr>
<tr ng-repeat="test in data">
<td>{{test.resource}}</td>
<td ng-repeat="pro in allProducts">{{pro.rom}}</td>
</tr>
并且只需在 allProducts 中推送 item2 而不是仅在 consummable 字段中推送 :
angular.forEach($scope.data,function(item, index){
angular.forEach(item.products, function(item2, index){
if($scope.allProducts.indexOf(item2.consummable) == -1){
$scope.allProducts.push(item2); // Here I have pushed item2 instead of item2.consummable
}
})
})
另一个变体 ng-repeat
表达式。由于您在第一行重复了 allProducts,因此您需要重复它并在其他行中重复,并为所选值过滤数据。请参阅下面的代码片段
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.data =[
{
"resource": "100",
products: [
{
"consummable": "milk",
"rom": 1
},
]
},
{
"resource": "200",
products: [
{
"consummable": "bread",
"rom": 12345
},
]
},
{
"resource": "300",
products: [
{
"consummable": "butter",
"rom": 123456789
}
]
}
];
$scope.allProducts = [];
angular.forEach($scope.data,function(item, index){
angular.forEach(item.products, function(item2, index){
if($scope.allProducts.indexOf(item2.consummable) == -1){
$scope.allProducts.push(item2.consummable);
}
})
})
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.17/angular.min.js"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<table style="border:1px solid red;">
<tr>
<td>
</td>
<td ng-repeat="itemProd in allProducts" style="border:1px solid red;">
{{itemProd}}
</td>
</tr>
<tr ng-repeat="item in data">
<td>
{{item.resource}}
</td>
<td ng-repeat="item2 in allProducts" style="border:1px solid red;" ng-init="product=(item.products | filter: {consummable:item2})[0]">
<a>{{product.rom}}</a>
</td>
</tr>
</table>
{{allProducts}}
{{data}}
</div>
您需要的解决方案如下:
1) 保持 allProducts 生成如下:
$scope.allProducts = [];
angular.forEach($scope.data,function(item, index){
angular.forEach(item.products, function(item2, index){
if($scope.allProducts.indexOf(item2.consummable) == -1){
$scope.allProducts.push(item2.consummable);
}
})
})
2) 在您的控制器中添加:
$scope.getColumnValue = function(item,item2, index) {
var returnVal;
angular.forEach(item.products, function(val, index){
if(item2 == val.consummable){
returnVal = val.rom;
}
})
return returnVal;
}
3) table 代将是:
<table style="border:1px solid red;">
<tr>
<td>
</td>
<td ng-repeat="itemProd in allProducts" style="border:1px solid red;">
{{itemProd}}
</td>
</tr>
<tr ng-repeat="item in data">
<td>
{{item.resource}}
</td>
<td ng-repeat="item2 in allProducts" style="border:1px solid red;">
<a>{{getColumnValue(item, item2)}}</a>
</td>
</tr>
</table>