繁体   English   中英

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

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

我在json中有数据,它将是动态的,我想在具有两列的表中显示数据。 我正在尝试,但是仅前两个记录在重复。 我没有在表中看到所有记录。。有什么帮助吗?

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"
         }
      ]
   }
}';

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

在控制器中转换数据:

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();

这样就可以在视图中轻松使用它:

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

柱塞

使用ng-repeat的迭代器是$ index。

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

将表替换为:

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

根据我的评论,这个问题根本不需要$ index。 您可以对表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