modelling data from rows to columns in Angular dyn

2019-02-21 04:56发布

问题:

I have data that looks like this:

$scope.items =[{name: "item1", year : "2013", value : "100"},
    {name: "item1", year : "2012", value : "97"},
    {name: "item1", year : "2011", value : "98" },
    {name: "item2", year : "2013", value : "-7" },
    {name: "item2", year : "2012", value : "-6" },
    {name: "item2", year : "2011", value : "-5" },
    {name: "item3", year : "2013", value : "93" },
    {name: "item3", year : "2013", value : "91" },
    {name: "item3", year : "2012", value : "93" },
    {name: "item4", year : "2013", value : "-35" },
    {name: "item4", year : "2012", value : "-36" },
    {name: "item4", year : "2011", value : "-37" },
    {name: "item5", year : "2013", value : "58" },
    {name: "item5", year : "2012", value : "55" },
    {name: "item5", year : "2011", value : "56" }]

I want to render the data as such:

Name  2011 2012 2013
item1   98   97  100
item2   -5   -6   -7
tiem3   93   91   93
item4  -35  -36  -37
item5   58   55   56

How can I achieve this using Angular? I understand the basics of ngRepeat but if I do ng-repeat = "(key, value) in items" and then value.name or value.year I have 15 instances of them instead of just item1 to item 5 and 2011, 2012 and 2013. Ideally I would like to have one loop that gives the years for the columns headers and one other loop that gives me the list of items for the rows, then i would reference the values by rows, columns. Any ideas?

回答1:

You can use _lodash to get helped.

script:

var app = angular.module('app',[]);
app.controller('TestCtrl',['$scope',function($scope){
  var items =[{name: "item1", year : "2013", value : "100"},
      {name: "item1", year : "2012", value : "97"},
      {name: "item1", year : "2011", value : "98" },
      {name: "item2", year : "2013", value : "-7" },
      {name: "item2", year : "2012", value : "-6" },
      {name: "item2", year : "2011", value : "-5" },
      {name: "item3", year : "2013", value : "93" },
      {name: "item3", year : "2013", value : "91" },
      {name: "item3", year : "2012", value : "93" },
      {name: "item4", year : "2013", value : "-35" },
      {name: "item4", year : "2012", value : "-36" },
      {name: "item4", year : "2011", value : "-37" },
      {name: "item5", year : "2013", value : "58" },
      {name: "item5", year : "2012", value : "55" },
      {name: "item5", year : "2011", value : "56" }]


      $scope.headCells = _.keys(_.groupBy(items, function(item){ return item.year}));
      $scope.rows = _.groupBy(items, function(item){ return item.name});

      $scope.sortByYearProp = function(values){
        return _.sortBy(values, function(value){
          return value.year;
        });
      }

}])

html:

<table ng-controller="TestCtrl as test">
  <tr><th>Name</th><th ng-repeat="year in headCells">{{year}}</th></tt>
  <tr ng-repeat="(itemName, value) in rows"><td>{{itemName}}</td><td ng-repeat="obj in sortByYearProp(value)">{{obj.value}}</td></tr>
</table>

http://plnkr.co/edit/zvX6bsqicS5flD7BXzzN