2017-05-31 116 views
0

我創建爲我從控制器獲取數據的一個動態表,以前我是用jquery ajax加載表格內容如何動態地添加按鈕角

success: function (response) { 
for (var i = 0; i < List.length; i++) { 

        var table = '<tr id="' + List[i].Id + '"><td>' + (i + 1) + '</td><td id="name' + i + '">' + List[i].name + '</td><td><i class="edit fa fa-pencil-square-o" id="edit' + i + '"></i><i class="update fa fa-floppy-o" id="update' + i + '"></i><i class="editCancel fa fa-times" id="editCancel' + i + '" ></i></td><tr>'; 
        $('#content').append(table); 
        editUpdate(); 
       } 
} 

現在,我嘗試使用角度

同樣的事情
<script> 
    var app=angular 
       .module("intranet_App", []) 
       .controller('myCtrl', function ($scope, $http) { 
        $http.post("/Admin/getRolesList") 
        .then(function (response) {      
         $scope.roleList = response.data; 
         }); 
       }) 
</script> 

我正在獲取表數據,但如何動態地添加按鈕(對於每行我需要添加按鈕的動作列)使用角度的表數據?
HTML:

<table class="table table-hover table-bordered" id="mydata" ng-controller="myCtrl"> 
        <thead class="colorBlue"> 
         <tr> 
          <th>S.No</th> 
          <th>Role Name</th> 
          <th>Action</th> 
         </tr> 
        </thead> 
        <tbody id=""> 
         <tr ng-repeat="x in roleList | filter:searchText"> 
          <td>{{x.Id}}</td> 
          <td>{{x.name}}</td> 
         </tr> 
        </tbody> 
       </table> 
+2

您可以添加這個元素在你的第3列​​ NKDev

回答

1

將創建動態通過使用ng-repeat基於的RoleList。如果用按鈕添加新欄目td,則會爲每一行添加它們。

<tbody id=""> 
    <tr ng-repeat="x in roleList | filter:searchText"> 
    <td>{{x.Id}}</td> 
    <td>{{x.name}}</td> 
    <td><button></button></td> 
    </tr> 
</tbody> 
+0

是否可以初始化爲'i = 0'並在每個按鈕中使用'i'的迭代。 – user7397787

+1

你可以使用$ index進行迭代https://docs.angularjs.org/api/ng/directive/ngRepeat – NKDev

1
<table class="table table-hover table-bordered" id="mydata" ng-controller="myCtrl"> 
    <thead class="colorBlue"> 
     <tr> 
      <th>S.No</th> 
      <th>Role Name</th> 
      <th>Action</th> 
     </tr> 
    </thead> 
    <tbody id=""> 
     <tr ng-repeat="x in roleList | filter:searchText"> 
      <td>{{x.Id}}</td> 
      <td>{{x.name}}</td> 
      <td> 
       <i class="edit fa fa-pencil-square-o" id="edit{{x.Id}}"></i> 
       <i class="update fa fa-floppy-o" id="update{{x.Id}}"></i> 
       <i class="editCancel fa fa-times" id="editCancel{{x.Id}}" ></i> 
      </td> 
     </tr> 
    </tbody> 
</table> 

然後對他們的點擊事件,你可以在他們做ng-click S,例如:

<td> 
    <i class="edit fa fa-pencil-square-o" id="edit{{x.Id}}" ng-click="edit(x.Id)"></i> 
    <i class="update fa fa-floppy-o" id="update{{x.Id}}" ng-click="update(x.Id)"></i> 
    <i class="editCancel fa fa-times" id="editCancel{{x.Id}}" ng-click="cancel(x.Id)"></i> 
</td> 
+0

你可以在這裏檢查'edit(x.Id)'...這是正確的語法嗎? – user7397787

+0

這是因爲'ng-click'裏面使用普通的JavaScript,所以你可以直接使用'ng-repeat'中的''x'變量並且訪問它的屬性,所以這會調用一個函數'edit' 'x.Id'。只要確保在範圍內有一個稱爲編輯的功能 – Chifilly