2015-09-11 6 views
5

नीचे मेरी plunker मैं कहाँ के तहत प्रत्येक category.But रूप .example करने वाला डेटा मुद्रण नहीं कर रहे हैं डेटा को प्रदर्शित करने की कोशिश की है है: -ROM मान 12345 सीधे रोटी के तहत आना चाहिए ।में सही डेटा प्रदर्शित करने में सक्षम नहीं तालिका -AngularJS

<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 धक्का:

$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); 
     } 
    }) 
    }) 

Creating a table matrix

+0

हमें पूर्ण जानकारी, json प्रतिक्रिया उदाहरण के लिए, अनुरोध के कोड बैकेंड और आदि देना – num8er

+0

JSON में डेटा की plunker.The व्यवस्था में होती है टेबल की आवश्यकता है – forgottofly

+0

'ng-repeat' सरल लूप को छोड़कर कुछ नहीं करता है, इसलिए यदि आपके 'आइटम' में एक _product_ है तो यह केवल एक _cell_ प्रस्तुत करता है। तो आपको अपने डेटा को प्री-प्रोसेस करने की आवश्यकता है, या 'ng-repeat' के लिए अभिव्यक्ति बदलें :-) – Grundy

उत्तर

2

फिर भी बदल ng-repeat अभिव्यक्ति के साथ एक और संस्करण। चूंकि आप पहली पंक्ति में सभी उत्पाद दोहराते हैं, इसलिए आपको इसे दोबारा और अन्य में दोहराने की आवश्यकता है, और चयनित मान के लिए डेटा फ़िल्टर करें।

1) इस तरह 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>

+0

ग्रांडी के तहत होना चाहिए, क्या आप फ़िल्टर को समझा सकते हैं? फिल्टर: {consummable: ITEM2}) [0] – forgottofly

+1

@forgottofly, सुनिश्चित करें कि: [फिल्टर] (https://docs.angularjs.org/api/ng/filter/filter) standart फिल्टर, हम वस्तु पैरामीटर है कि साधन के रूप में पारित किया गया है वस्तुओं को खोजने जहां 'consummable' संपत्ति' item2' के रूप में ही है, और इसलिए वापसी सरणी फ़िल्टर किए गए तत्वों को फ़िल्टर, हम सरल पहले – Grundy

+0

धन्यवाद Grundy पाने की कोशिश, अच्छी तरह से समझाया – forgottofly

0

संरचना की जांच करें: मुझे यकीन है कि जहां मैं अपने कोड में गलत कर रहा हूँ नहीं कर रहा हूँ बजाय केवल 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 
    } 
}) 

})

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

+0

अपने मुद्रण सभी मेज़ में मूल्यों के तहत केवल विशिष्ट मूल्य प्रिंट करना चाहते हैं। प्रत्येक शीर्षक। उदाहरण: रोम मान 1 100-दूध यानी, (1 * 1) के तहत होना चाहिए। मूल्य 12345 200-रोटी – forgottofly

0

समाधान कोड स्निपेट देखें

$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 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> 
संबंधित मुद्दे