2017-06-19 59 views
1

關鍵我收到AngularJs JSON數據並想用NG-重複顯示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 no |指定|

| 133077 |軟件工程師|

| 133079 |高級軟件工程師|

| 143688 |高級軟件工程師|

請任何幫助。 在此先感謝。

+1

你應該做的代碼學校AngularJS教程。本教程來幫助你完成這個https://www.codeschool.com/pages/angularjs-vs-angular – Adjit

回答

0

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>

+0

我想補充另一列SI.No. 這裏SI.No必須在行被創建時生成。 預期輸出: SI.NO | uniq no |指定| 1 | 133077 |軟件工程師| 2 | 133079 |高級軟件工程師| 3 | 143688 |高級軟件工程師|。 – siddharth

+0

@siddharth更新了我的答案。請upvote,如果它的解決方案,你正在尋找 –

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>

0

新的組件架構我的解決方案的基礎簡單的理解:

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

+0

@scrappedcola行!謝謝 – vanchelo

+0

@scrappedcola完成。 – vanchelo