4

I am using angular.js for my font-end, node.js for server side and PostgreSQL for database.

Now, I am having some list of values in DB.

Database:

enter image description here

Controller code:

I got below output in console.

console.log($scope.items);

$scope.items = [
  {
    salary_head_name : 'BASIC',
    salary_head_value : 15000,
    salary_head_type : 'E'

  }, {    
    salary_head_name : 'HRA',
    salary_head_value : 7500,
    salary_head_type : 'E'    
  },{    
    salary_head_name : 'Conveyance',
    salary_head_value : 1600,
    salary_head_type : 'E'    
  },{    
    salary_head_name : 'Med. Allow',
    salary_head_value : 1800,
    salary_head_type : 'E'    
  },{    
    salary_head_name : 'PF',
    salary_head_value : 1800,
    salary_head_type : 'D'    
  }
];

Note: For readability I printed only few records in the above array (edited manually), actually it prints all the records.

Expected output in UI:

enter image description here

And also I want to print the sum of Earnings and Deductions in the Total fields in the above table.

2
  • 2
    Take a look at ng-repeat directive to make your table with angularjs. You should post some code of what you tried to get this to work, even if it doesn't work. Commented Mar 10, 2016 at 14:57
  • node.js tag is not required here, your question is related to angular.js only Commented Mar 10, 2016 at 14:59

1 Answer 1

1

Use ng-repeat, For basic Idea look at following code:

(function(angular) {
  'use strict';
angular.module('ngRepeat', ['ngAnimate']).controller('repeatController', function($scope) {
  $scope.items = [{

                salary_head_name : 'BASIC',
                        salary_head_value : 15000,
                            salary_head_type : 'E'

            }, {

                salary_head_name : 'HRA',
                         salary_head_value : 7500,
                            salary_head_type : 'E'

            },{

                salary_head_name : 'Conveyance',
                         salary_head_value : 1600,
                            salary_head_type : 'E'

            },{

                salary_head_name : 'Med. Allow',
                        salary_head_value : 1800,
                            salary_head_type : 'E'

            },{

                salary_head_name : 'PF',
                         salary_head_value : 1800,
                            salary_head_type : 'D'

            }];
});
})(window.angular);
table {
  border: 1px solid #666;   
    width: 100%;
}
th {
  background: #f8f8f8; 
  font-weight: bold;    
    padding: 2px;
}
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-ngRepeat-production</title>
  <link href="animations.css" rel="stylesheet" type="text/css">
  

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
  <script src="script.js"></script>
  

  
</head>
<body ng-app="ngRepeat">
  <div ng-controller="repeatController">
  <table>
    <tr>
        <th>Earnings</th>
        <th>Amount</th>
        <th>Type</th>
    </tr>
    <tr ng-repeat="item in items">
        <td>{{item.salary_head_name}}</td>
        <td>{{item.salary_head_value}}</td>
        <td>{{item.salary_head_type}}</td>
    </tr>
</table>
        
</div>
</body>
</html>

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.