How to display objects in a table using angularjs

I have data of such objects:

 var details = {  
            3:{
               2015-3-17 : 1,
               2015-3-18 : 0,
               routelines: "PASAY - CAGAYAN",
               tripcode: 3
              },
            4:{ 
               2015-3-17 : 0
               2015-3-18 : 4
               routelines: "PASAY - CAVITE",
               tripcode:4        
              },
 }

Now I plan to display them in a table, but I'm not sure how to start, since they are all objects. I want to get a result that looks like this:

tripcode | routlines | 2015-3-17 | 2015-3-18| 3 |PASAY - CAGAYAN | 1 | 0 | 4 |PASAY - CAVITE | 0 | 4 |

Does anyone know how to do this? I tried this, but unfortunately it does not work.

<div ng-repeat="detail in details">
  <div ng-repeat="(key, value) in detail">
      {{key}} : {{value}}
  </div>
</div>
+4
source share
2 answers

Make sure you format your data object correctly; some keys do not include quotation marks. Details should also be tied to $ scope.

Try the following:

function MyCtrl($scope) {
    
    $scope.details = {
        '3': {
            tripcode: 3,
            routelines: "PASAY - CAGAYAN",
            '2015 - 3 - 17': 1,
            '2015 - 3 - 18': 0
        },
        '4': {
           tripcode: 4,
           routelines: "PASAY - CAVITE",
           '2015 - 3 - 17': 0,
           '2015 - 3 - 18': 4
        },
    };
    
}
.header,
.items {
 border-bottom: 1px solid #000;
 width: 600px;
}

.header span,
.items span {
    display: inline-block;
    width: 120px;
    border-right: 1px solid #000;
    padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
    <div ng-controller="MyCtrl">
        <div ng-repeat="detail in details">
            <div class="header" ng-show="$index == 0">
                <span ng-repeat="(key, value) in detail">{{key}}</span>
            </div>
            <div class="items">
                <span ng-repeat="(key, value) in detail">{{value}}</span>
            </div>
        </div>
    </div>
</div>
Run codeHide result
+2
source

you skipped it $scope

change var detailsto$scope.details

+1
source

All Articles