Суммируйте текущие значения результатов по вертикали, которые были проанализированы из JSON - Angular JS

0

У меня есть контроллер Angular JS, который суммирует результат значений по горизонтали, но теперь у меня есть потребность в суммировании текущего результата значений по вертикали. Этот результат анализировался от JSON и я пробовал с таким количеством фильтров, но не добился успеха. Не знаю, как вычислить набор результатов из представления в контроллер. Я требую желаемого результата. Помощь определенно оценена.

var arr = [
  {
    "unique_id": "CS",
    "college": "BSCS",
    "arr_length": 1,
    "program_section": [
      {
        "question": "Q1",
        "total": 135,
        
      },
      {
        "question": "Q2",
        "total": 142,
        
      },
      {
        "question": "Q3",
        "total": 135,
        
      },
      {
        "question": "Q4",
        "total": 137,
        
      },
      
    ]
  },
  {
    "unique_id": "MBA",
    "college": "BBA",
    "arr_length": 2,
    "program_section": [
      {
        "question": "Q1",
        "total": 175,
        
      },
      {
        "question": "Q2",
        "total": 142,
        
      },
      {
        "question": "Q3",
        "total": 165,
        
      },
      {
        "question": "Q4",
        "total": 137,
        
      },
      
    ]
  },
  {
    "unique_id": "CA",
    "college": "Account",
    "arr_length": 1,
    "program_section": [
      {
        "question": "Q1",
        "total": 145,
        
      },
      {
        "question": "Q2",
        "total": 162,
        
      },
      {
        "question": "Q3",
        "total": 125,
        
      },
      {
        "question": "Q4",
        "total": 117,
        
      },
      
    ]
  }
];
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $scope.names = arr;
    $scope.totals = {}
    arr.map(function(obj){
        obj.program_section.map(function(section){
            $scope.totals[section.question] = ($scope.totals[section.question] || 0) + section.total
        })
    })
}).filter('range', function() {
  return function(input, total) {
    total = parseInt(total);

    for (var i=0; i<total; i++) {
      input.push(i);
    }

    return input;
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl"> 

<table  border="1">
<thead>
  <tr>
    <td>Question</td>
    <td ng-repeat="x in names">{{ x.college }}</td>
    <td>Totals</td>
  </tr>
</thead>
<tbody>
      <tr ng-repeat="n in [] | range:4">
        <td>
            {{ names[0].program_section[n].question }}
        </td>
        <td width="100px" ng-repeat="x in names" >
           {{ x.program_section[n].total }}
        </td>
        <td width="50%" ng-bind="totals[names[0].program_section[n].question]"></td>
      </tr>
      
</tbody>
  
</table>

</div>

Желаемый результат

<table border="1">
  <thead>
    <tr>
      <td>Question</td>
      <td >BSCS</td>
      <td >BBA</td>
      <td >Account</td>
      <td>Totals</td>
    </tr>
  </thead>
  <tbody>
    <tr >
      <td> Q1 </td>
      <td  width="100px"> 135 </td>
      <td  width="100px"> 175 </td>
      <td  width="100px"> 145 </td>
      <td width="50%">455</td>
    </tr>
    <tr >
      <td> Q2 </td>
      <td  width="100px"> 142 </td>
      <td  width="100px"> 142 </td>
      <td  width="100px"> 162 </td>
      <td width="50%">446</td>
    </tr>
    <tr >
      <td> Q3 </td>
      <td  width="100px"> 135 </td>
      <td  width="100px"> 165 </td>
      <td  width="100px"> 125 </td>
      <td width="50%">425</td>
    </tr>
    <tr >
      <td> Q4 </td>
      <td  width="100px"> 137 </td>
      <td width="100px" > 137 </td>
      <td width="100px" > 117 </td>
      <td width="50%">391</td>
    </tr>
    <tr >
      <td><strong>Total</strong></td>
      <td ><strong>549</strong></td>
      <td width="100px" ><strong>619</strong></td>
      <td width="100px" ><strong>549</strong></td>
      <td width="50%"><strong>1717</strong></td>
    </tr>
  </tbody>
</table>

1 ответ

2
Лучший ответ

Это должно выполнить то, что вы собираетесь делать:

var arr = [{
  "unique_id": "CS", "college": "BSCS", "arr_length": 1,
  "program_section": [
    { "question": "Q1", "total": 135 },
    { "question": "Q2", "total": 142 },
    { "question": "Q3", "total": 135 },
    { "question": "Q4", "total": 137 }
  ]
}, {
  "unique_id": "MBA", "college": "BBA", "arr_length": 2,
  "program_section": [
    { "question": "Q1", "total": 175 },
    { "question": "Q2", "total": 142 },
    { "question": "Q3", "total": 165 },
    { "question": "Q4", "total": 137 }
  ]
}, {
  "unique_id": "CA", "college": "Account", "arr_length": 1,
  "program_section": [
    { "question": "Q1", "total": 145 },
    { "question": "Q2", "total": 162 },
    { "question": "Q3", "total": 125 },
    { "question": "Q4", "total": 117 }
  ]
}];
angular.module('myApp', []).controller('customersCtrl', function($scope, $http) {
    $scope.names = arr;
    $scope.totals = {};
    arr.forEach(function(obj){
        obj.program_section.forEach(function(section){
            $scope.totals[section.question] = ($scope.totals[section.question] || 0) + section.total
        });
    });

    // Sum each item in 'arr'.
    $scope.colTotals = $scope.names.map(function (name) {
        return name.program_section.reduce(function (total, section) {
            return total + section.total;
        }, 0);
    });

    // Add the sum of sums
    $scope.colTotals.push($scope.colTotals.reduce(function (x, y) {
        return x + y;
    }, 0));
}).filter('range', function() {
  return function(input, total) {
    total = parseInt(total);

    for (var i=0; i<total; i++) {
      input.push(i);
    }

    return input;
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl"> 

<table  border="1">
<thead>
  <tr>
    <td>Question</td>
    <td ng-repeat="x in names">{{ x.college }}</td>
    <td>Totals</td>
  </tr>
</thead>
<tbody>
      <tr ng-repeat="n in [] | range:4">
        <td>
            {{ names[0].program_section[n].question }}
        </td>
        <td width="100px" ng-repeat="x in names" >
           {{ x.program_section[n].total }}
        </td>
        <td width="50%" ng-bind="totals[names[0].program_section[n].question]"></td>
      </tr>
      <tr style="font-weight: bold;">
        <td>Totals</td>
        <td ng-repeat="colTotal in colTotals track by $index">{{ colTotal }}</td>
      </tr>
</tbody>
  
</table>

</div>
  • 0
    Спасибо за помощь. ты спас мой день

Ещё вопросы

Сообщество Overcoder
Наверх
Меню