【问题标题】:how to repeat the rows for the key of key value如何重复键值键的行
【发布时间】:2017-06-19 18:44:25
【问题描述】:

我在 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 像这样在表格中显示:

|唯一编号 |指定 |

| 133077 |软件工程师 |

| 133079 |高级软件工程师 |

| 143688 |高级软件工程师 |

请帮忙。 提前致谢。

【问题讨论】:

标签: html angularjs json


【解决方案1】:

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",
   }

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

【讨论】:

  • 我想添加另一列作为 SI.No。在这里,必须在创建行时生成 SI.No。预期输出:SI.NO |唯一编号 |指定 | 1 | 133077 |软件工程师 | 2 | 133079 |高级软件工程师 | 3 | 143688 |高级软件工程师 |.
  • @siddharth 更新了我的答案。如果它是您正在寻找的解决方案,请投票
【解决方案2】:

编辑: 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>

【讨论】:

    【解决方案3】:

    我的解决方案基于简单易懂的新组件架构:

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

    【讨论】:

    • @scrappedcola 好的!谢谢
    • @scrappedcola 完成。
    猜你喜欢
    • 1970-01-01
    • 2018-01-04
    • 2021-03-31
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    相关资源
    最近更新 更多