简体   繁体   English

如何使用angular在表的两列中显示json数据

[英]How to display json data in two columns in a table using angular

I have data in my json it will be dynamic and I want to display the data in a table with two columns. 我在json中有数据,它将是动态的,我想在具有两列的表中显示数据。 I am trying but only the first two records are repeating. 我正在尝试,但是仅前两个记录在重复。 I don't see all the records in the table.. Any help? 我没有在表中看到所有记录。。有什么帮助吗?

http://plnkr.co/edit/Hnb7hkjA16XDbzRT8VAt?p=preview http://plnkr.co/edit/Hnb7hkjA16XDbzRT8VAt?p=preview

This is my json :  var data = '{  
   "output":{  
      "service-status":[  
         {  
            "service":"db",
            "service-name":"Mongo DB"
         },
         {  
            "service":"license",
            "service-name":"Smart License"
         },
         {  
            "service":"BRM",
            "service-name":"Billing"
         },
         {  
            "service":"subscription",
            "service-name":"subscription"
         }
      ]
   }
}';

my html code: 我的html代码:

<table  border="1px" width="100%" ng-repeat="data in serviceData" ng-if="$index % 2 == 0">
     <tr>
         <td>{{serviceData[index]["service-name"]}}</td>
      </tr>
</table>

i want to display something like this

Mongo Db       Smart License
Billing        subscription

Transform your data in the controller: 在控制器中转换数据:

var serviceDataView = function() {
    var res = [];
    for (var i = 0; i < $scope.serviceData.length; i+=2){
      res.push({
        col1: $scope.serviceData[i]['service-name'], 
        col2: $scope.serviceData[i+1] ? $scope.serviceData[i+1]['service-name'] : null
      });
    }
    return res;
};

$scope.structuredData = serviceDataView();

So that it can be easily used in the view: 这样就可以在视图中轻松使用它:

<table border="1px" width="100%">
    <tr ng-repeat="data in structuredData">
        <td>{{data.col1}}</td>
        <td>{{data.col2}}</td>
    </tr>
</table>

Plunker . 柱塞

The iterator that uses ng-repeat is $index. 使用ng-repeat的迭代器是$ index。

Others Iterator: https://docs.angularjs.org/api/ng/directive/ngRepeat 其他迭代器: https : //docs.angularjs.org/api/ng/directive/ngRepeat

Replace your table for: 将表替换为:

<table  border="1px" width="100%" ng-repeat="data in serviceData">
    <tr>
        <td>{{serviceData[$index]["service"]}}</td>
        <td>{{serviceData[$index]["service-name"]}}</td>
    </tr>
</table>

As per my review, $index is not required at all for this issue. 根据我的评论,这个问题根本不需要$ index。 You can use filter for table ngRepeat.check this [link] http://plnkr.co/edit/Hnb7hkjA16XDbzRT8VAt?p=preview 您可以对表ngRepeat使用过滤器。请检查此[链接] http://plnkr.co/edit/Hnb7hkjA16XDbzRT8VAt?p=preview

html code here: 
<table  border="1px" width="100%" ng-repeat="data in serviceData |limitTo:2 ">
 <tr>
     <td>{{data["service-name"]}}</td>
     <td>{{data.service}}</td>
</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM