如何为键值的键重复行

时间:2017-06-19 18:44:25

标签: html angularjs json

我在AngularJs中收到了json数据,并希望使用ng-repeat在表格中显示json数据的特定值。

json数据:

{  
   "15":{  
      "Unique number":"133077",
      "Designation":"Software Engineer",
   },

   "16":{  
      "Unique number":"133079",
      "Designation":"Senior Software Engineer",
   },

   "18":{  
      "Unique number":"143688",
      "Designation":"Senior Software Engineer",
   }

} 

我希望使用HTML显示在这样的表中:

| uniq没有|指定|

| 133077 |软件工程师|

| 133079 |高级软件工程师|

| 143688 |高级软件工程师|

请帮忙。 提前谢谢。

3 个答案:

答案 0 :(得分:0)

编辑: OP更改了添加额外字段以显示序列号的要求。

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.data = {"15":{
"Unique number":"133077", "Designation":"Software Engineer", }, 
"16":{
"Unique number":"133079", "Designation":"Senior Software Engineer", }, 
"18":{
"Unique number":"143688", "Designation":"Senior Software Engineer", }
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div ng-app="myApp" ng-controller="myCtrl">
<table class="table table-bordered table-striped">
  <thead>
    <th>Sl. No.</th>
    <th>Unique number</th>
    <th>Designation</th>
  </thead>
  <tr ng-repeat='(key, value) in data'>
    <td>
      {{$index + 1}}
    </td>
    <td>
      {{value["Unique number"]}}
    </td>
    <td>
      {{value["Designation"]}}
    </td>
  </tr>
</table>
</div>

答案 1 :(得分:0)

&#13;
&#13;
angular.module('app',[]).controller('myCtrl', function($scope){
  $scope.members = {  
   "15":{  
      "Unique number":"133077",
      "Designation":"Software Engineer",
   },

   "16":{  
      "Unique number":"133079",
      "Designation":"Senior Software Engineer",
   },

   "18":{  
      "Unique number":"143688",
      "Designation":"Senior Software Engineer",
   }

}; 
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="myCtrl">
 <table>
  <thead>
    <th>SI.NO</th>
    <th>Unique number</th>
    <th>Designation</th>
  </thead>
  <tbody>
    <tr ng-repeat="member in members">
      <td>{{$index + 1}}</td>
      <td>{{member['Unique number']}}</td>
      <td>{{member.Designation}}</td>
    </tr>
  </tbody>
 </table>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我的解决方案基于新组件架构,易于理解:

&#13;
&#13;
(function() {
  'use strict';

  // app.js
  angular
    .module('app', [])
    .component('myApp', {
      template: '<items-table items="$ctrl.items"></items-table>',
      bindings: {},
      controller: function() {
        this.items = {
          "15": {
            "Designation": "Software Engineer",
            "Unique number": "133077"
          },
          "16": {
            "Designation": "Senior Software Engineer",
            "Unique number": "133079"
          },
          "18": {
            "Designation": "Senior Software Engineer",
            "Unique number": "143688"
          }
        };
      }
    });

  const itemsTableTemplate = `
<div class="items">
  <h2>Items</h2>

  <table>
    <thead>
      <tr>
        <th>unique no</th>
        <th>designation</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="(id, item) in $ctrl.items track by id">
        <td ng-bind="item['Unique number']"></td>
        <td ng-bind="item['Designation']"></td>
      </tr>
    </tbody>
  </table>
</div>
`;

  // items-table.component.js
  angular
    .module('app')
    .component('itemsTable', {
      template: itemsTableTemplate,
      bindings: {
        items: '<'
      },
      controller: ItemsTableController,
      controllerAs: '$ctrl'
    });

  function ItemsTableController() {
    // Constructor
  }
  
  angular.bootstrap(document.getElementById('app'), ['app'], {
    strictDi: true
  })
  
})();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div id="app">
  <my-app>
    Loading...
  </my-app>
</div>
&#13;
&#13;
&#13;