2016-07-23 88 views
0

我的JSON数据看起来是这样的:NG-重复:绘制表基础上的对象分组值

{'A': 'L', 'B': 'L', 'C': 'G', 'D': 'L', 'E': 'G', 'F': 'L' } 

我想提请表像这样用NG-重复,

L G 
A C 
B E 
D 
F 

这里,我根据valueskeys进行分组。

这是我试过的,但我没有得到我想要的。

<table class="table table-hover"> 
    <thead> 
    <tr> 
     <th>L</th> 
     <th>G</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr ng-repeat="(key, val) in intlist track by $index"> 
     <td ng-if="val=='L'">{{ key }}</td> 
     <td ng-if="val=='K'">{{ key }}</td> 
    </tr> 
    </tbody> 
</table> 
+0

我不明白你的逻辑..你能解释一下? – developer033

+0

根据价值,我想分组密钥 – ramesh

回答

4

地图数据阵列的新数组,可重复的,那么内部的重复每个细胞

var app = angular.module('angularTest', []); 
 

 
app.controller('MainCtrl', ['$scope', 
 
    function($scope) { 
 
    var data = {'A': 'L', 'B': 'L', 'C': 'G', 'D': 'L', 'E': 'G', 'F': 'L' }; 
 
    
 
    var tmp = {}; 
 
    // map values to arrays of keys in tmp 
 
    for (var key in data) { 
 
     if (!tmp[data[key]]) { 
 
     tmp[data[key]] = []; 
 
     } 
 
     tmp[data[key]].push(key); 
 
    } 
 
    // create array of headings from `tmp` keys 
 
    var headings = Object.keys(tmp); 
 
    // get longest array 
 
    var res = headings 
 
     .reduce(function(a, c) { 
 
      return tmp[c].length > a.length ? tmp[c] : a;   
 
     }, []) 
 
     // use longest to map results array 
 
     .map(function(_, i) { 
 
      // iterate headings 
 
      return headings.map(function(heading) { 
 
       // return array element if it exists or empty string   
 
       return tmp[heading][i] || ''; 
 
      }); 
 
     }); 
 

 
    $scope.headings = headings; 
 
    $scope.list = res; 
 

 
    } 
 
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.js"></script> 
 
<div ng-app="angularTest" ng-controller="MainCtrl"> 
 
    <h4>List Data</h4> 
 
    <pre>{{list}}</pre> 
 
    <h4>View Table</h4> 
 
<table class="table table-hover" border="1"> 
 
    <thead> 
 
    <tr> 
 
     <th ng-repeat="heading in headings">{{heading }}</th>   
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr ng-repeat="arr in list"> 
 
     <td ng-repeat="item in arr">{{item}}</td> 
 
    </tr> 
 

 
    </tbody> 
 
</table> 
 
</div>

+0

这是一个很好的解决方案,我很好奇地看到这个问题的答案。 +1 :) – developer033

+0

@ developer033 thx ...比第一次见到眼更复杂的问题 – charlietfl

+0

是的,我也试图解决,但它比我想象的复杂得多。 – developer033