0

I have a controller of Angular JS which sum result of values in Horizontally but now i have requirement need to sum current result of values in vertically. This result parsed from the JSON also i have tried with so many filters but no success. No idea how to calculate result set from view to controller. I have require desired result. Helps are definitely appreciated.

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>

Desired Result

<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>

0

1 Answer 1

2

This should accomplish what you're going for:

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.