Skip to main content

AngularJS - Tables

Table data is normally repeatable by nature. ng-repeat directive can be used to draw table easily.  Following example states the use of ng-repeat directive to draw a table. <table> <tr> <th> Name </th> <th> Marks </th> </tr> <tr ng-repeat = "subject in student.subjects" > <td> {{ subject.name }} </td> <td> {{ subject.marks }} </td> </tr> </table> Table can be styled using CSS Styling. <style> table , th , td { border : 1px solid grey ; border - collapse : collapse ; padding : 5px ; } table tr : nth - child ( odd ) { background - color : #f2f2f2; } table tr : nth - child ( even ) { background - color : #ffffff; } </style> Example Following example will showcase all the above mentioned directive. <html> <head> <...

AngularJS - Tables

Table data is normally repeatable by nature. ng-repeat directive can be used to draw table easily. 

Following example states the use of ng-repeat directive to draw a table.





<table>
   <tr>
      <th>Name</th>
      <th>Marks</th>
   </tr>
   
   <tr ng-repeat = "subject in student.subjects">
      <td>{{ subject.name }}</td>
      <td>{{ subject.marks }}</td>
   </tr>
</table>
Table can be styled using CSS Styling.
<style>
   table, th , td {
      border: 1px solid grey;
      border-collapse: collapse;
      padding: 5px;
   }
   
   table tr:nth-child(odd) {
      background-color: #f2f2f2;
   }

   table tr:nth-child(even) {
      background-color: #ffffff;
   }
</style>

Example

Following example will showcase all the above mentioned directive.
<html>
   
   <head>
      <title>Angular JS Table</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
      
      <style>
         table, th , td {
            border: 1px solid grey;
            border-collapse: collapse;
            padding: 5px;
         }
         
         table tr:nth-child(odd) {
            background-color: #f2f2f2;
         }
         
         table tr:nth-child(even) {
            background-color: #ffffff;
         }
      </style>
      
   </head>
   <body>
      <h2>AngularJS Sample Application</h2>
      <div ng-app = "mainApp" ng-controller = "studentController">
         
         <table border = "0">
            <tr>
               <td>Enter first name:</td>
               <td><input type = "text" ng-model = "student.firstName"></td>
            </tr>
            
            <tr>
               <td>Enter last name: </td>
               <td>
                  <input type = "text" ng-model = "student.lastName">
               </td>
            </tr>
            
            <tr>
               <td>Name: </td>
               <td>{{student.fullName()}}</td>
            </tr>
            
            <tr>
               <td>Subject:</td>
     
               <td>
                  <table>
                     <tr>
                        <th>Name</th>.
                        <th>Marks</th>
                     </tr>
                     
                     <tr ng-repeat = "subject in student.subjects">
                        <td>{{ subject.name }}</td>
                        <td>{{ subject.marks }}</td>
                     </tr>
       
                  </table>
               </td>
     
            </tr>
         </table>
         
      </div>
      
      <script>
         var mainApp = angular.module("mainApp", []);
         
         mainApp.controller('studentController', function($scope) {
            $scope.student = {
               firstName: "Manish",
               lastName: "Vagh",
               fees:500,
               
               subjects:[
                  {name:'Physics',marks:90},
                  {name:'Chemistry',marks:80},
                  {name:'Math',marks:90},
                  {name:'English',marks:90},
                  {name:'Hindi',marks:85}
               ],
               
               fullName: function() {
                  var studentObject;
                  studentObject = $scope.student;
                  return studentObject.firstName + " " + studentObject.lastName;
               }
            };
         });
      </script>
      
   </body>
</html>

Output

AngularJS Sample Application
Enter first name:
Enter last name:
Name:Manish Vagh
Subject:
NameMarks
Physics90
Chemistry80
Math90
English90
Hindi85

Comments

Popular posts from this blog

10 Reasons That Makes Magento The Dominating eCommerce Platform in 2018

In eCommerce market, for marketers, manufacturers and consumer 2017 was a great year, and it seems to be growing in 2018 as well. Every passing year shows tremendous growth in e-commerce sale that helps to take the business to new levels of success. The world's greatest brands love  Magento  for its adaptability since the present shoppers and their purchasing designs are changing incrementally. Just Magento — open source and nimble — can enable you to adjust and flourish. With a worldwide biological community of 150,000 engineers and a system of 300+ profoundly prepared arrangement accomplices, Magento helps your online deals while expanding gross edges. Magento organizations offer more at a lower TCO than dealers on aggressive trade stages. Maintaining an e-commerce business isn't a simple undertaking and expects you to put steady endeavors and to strategize to build up a solid web nearness that encourages you to give an upper hand. Wh...

Angular 2 Hello World

<!DOCTYPE html> <html>   <head>     <title>Angular 2 Hello World</title>     <meta name="viewport" content="width=device-width, initial-scale=1">     <link rel="stylesheet" href="styles.css">     <!-- IE required polyfills, in this exact order -->     <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-shim.min.js"></script>     <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.26/system-polyfills.js"></script>     <script src="https://npmcdn.com/angular2@2.0.0-beta.15/es6/dev/src/testing/shims_for_IE.js"></script>         <script src="https://code.angularjs.org/2.0.0-beta.15/angular2-polyfills.js"></script>     <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.26/system.js"></script>     <script sr...