modelling data from rows to columns in Angular dynamically

孤街醉人 提交于 2019-12-01 14:29:04

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!