modelling data from rows to columns in Angular dynamically

后端 未结 1 861
礼貌的吻别
礼貌的吻别 2021-01-17 00:42

I have data that looks like this:

$scope.items =[{name: \"item1\", year : \"2013\", value : \"100\"},
    {name: \"item1\", year : \"2012\", value : \"97\"},         


        
相关标签:
1条回答
  • 2021-01-17 01:41

    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

    0 讨论(0)
提交回复
热议问题