2016-05-14 55 views
0

我有一個簡單的角度智能表。它正確加載數據。如果我刪除一些數據表正確更新,但是如果我添加數據,表不會更新。有誰知道我做錯了什麼。幫助將非常感謝!如何添加數據到角度智能表

HTML:

<div class="container"> 
    <!-- Angular Grid --> 
    <div ng-controller="MainCtrl"> 
     <table st-table="displayedCollection" class="table table-striped" st-safe-src="rowCollection"> 
     <thead> 
     <tr> 
      <th>first name</th> 
      <th>last name</th> 
      <th>birth date</th> 
      <th>balance</th> 
      <th>email</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr ng-repeat="row in displayedCollection"> 
      <td>{{row.naam}}</td> 
      <td>{{row.naam2}}</td>   
      <td> 
      <button type="button" ng-click="removeItem(row)" class="btn btn-sm btn-danger"> 
        <i class="glyphicon glyphicon-remove-circle"> 
        </i> 
        </button></td> 

     </tr> 
     </tbody> 
     </table>  
    </div> 
    <button type="button" id="addData" class="btn btn-success" ng-click="addRandomItem(row)">Add Data</button> 
    <div ng-show='busy'>Loading data...</div> 
    </div> 
</div> 

和控制器:

(function() { 
    'use strict'; 

    angular 
    .module('app') 
    .controller('MainCtrl', MainCtrl); 

    MainCtrl.$inject = ['$scope', '$state', 'Auth', '$modal', 'scrapeAPI', '$http', '$alert', 'recommendationsAPI', 'Upload']; 

    function MainCtrl($scope, $state, Auth, $modal, scrapeAPI, $http, $alert, recommendationsAPI, Upload) { 
    $scope.displayedCollection = []; 
    $scope.user = Auth.getCurrentUser(); 
    $scope.rowCollection = []; 
    $scope.recommendation = {}; 
    $scope.recommendations = []; 
    $scope.recommendationPostForm = true; 
    $scope.busy = true; 
    $scope.allData = []; 
    var i = 0; 
    var page = 0; 
    var step = 3; 
    var data1 = []; 

//gets current data 

    recommendationsAPI.getAllRecommendations() 
     .then(function(data) { 
     console.log('looks found '); 
     console.log(data); 
     $scope.recommendations = data.data; 
     for (i = 0; i < $scope.recommendations.length; i++) { 
      $scope.rowCollection.push($scope.recommendations[i]); 
     } 
     $scope.busy = false; 
     }) 
     .catch(function(err) { 
     console.log('failed to get looks ' + err); 
     }); 

    $scope.addRandomItem = function addRandomItem() { 
     $scope.recommendation = { 
      naam: 'tje', 
      naam2: 'tje' 
     }; 
     $scope.recommendations.push($scope.recommendation); 
     for (var j = 1; j < $scope.recommendations.length; j++) { 
      alert ($scope.recommendations[j].guideline); 
     } 
     $scope.rowCollection.push($scope.recommendation); 

    }; 

    // Verwijder recommendation volledig 
    $scope.removeItem = function removeItem(row) { 
      var index = $scope.rowCollection.indexOf(row); 
      if (index !== -1) { 
       recommendationsAPI.deleteRecommendation($scope.rowCollection[index]._id) 
       .then(function(data) { 
       console.log('delete at backend: success');     
       $scope.rowCollection.splice(index, 1); 
       }); 
      } 
     }  

    } 
})(); 
+0

在[plunker](http://plnkr.co/edit/?p=catalogue)中複製問題的演示將會很有幫助。還要注意''displayedCollection''和'rowCollection'是獨立的數組 – charlietfl

+0

您在表格中顯示'{{row.naam}}'並用'name:'tje','推送對象。您需要將'name'更新爲'naam'。 – dipesh

回答

1

你的按鈕 「addRandomItem」 是哪些握着你的控制器的div,因此它不能訪問其適用範圍

<div class="container"> 
    <!-- Angular Grid --> 
    <div ng-controller="MainCtrl"> 
    <table st-table="displayedCollection" class="table table-striped" st-safe-src="rowCollection"> 
     <thead> 
     <tr> 
      <th>first name</th> 
      <th>last name</th> 
      <th>birth date</th> 
      <th>balance</th> 
      <th>email</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr ng-repeat="row in displayedCollection"> 
      <td>{{row.naam}}</td> 
      <td>{{row.naam2}}</td>   
      <td> 
      <button type="button" ng-click="removeItem(row)" class="btn btn-sm btn-danger"> 
       <i class="glyphicon glyphicon-remove-circle"> 
       </i> 
       </button> 
      </td> 
     </tr> 
    </tbody> 
    </table> 
    <button type="button" id="addData" class="btn btn-success" ng-click="addRandomItem(row)">Add Data</button>  
    </div> 
    <div ng-show='busy'>Loading data...</div> 
</div> 

+0

謝謝,如果我把這個按鈕放在桌子上的確是有效的。但是,是否也可以將使用模態輸入的外部數據加載到表格中? – Mihaly

+0

只需要在DIV中調用模態,並且完美地工作。愚蠢的我,非常高興,它的作品。 – Mihaly