2015-10-02 42 views
1

我是新角色,我想要獲取特定行的索引來執行函數hidestuff(),我將item.id傳遞給該函數並我想隱藏行女巫包含此ID ..但我怎麼傳遞行索引刪除整個行?在ng-repeat中獲取行索引(或id)在特定行上執行某些操作

HTML:

    <tr ng-repeat="item in ItemsByPage[currentPage]"> 
         <td> 
          <div ng-model="tid" 
           ng-hide="hidden" 
           ng-class="{fade: startFade}"> 

           {{item.id}} 
          </div>         
         </td> 
         <td> 
          <div editable-text="item.name" 
           onaftersave='inlineupdateName(item.id,item.name)' 
           ng-model="tname" data-n='item.name'> 

           {{item.name}} 
          </div> 
         </td> 
         <td> 
          <div editable-text="item.phone" 
           onaftersave='inlineupdatephone(item.id,item.phone)' 
           ng-model="tphone"> 
           {{item.phone}} 
          </div> 
         </td> 
        </tr> 
       <input type="text" ng-model="delId" class="form-control" 
         placeholder="Enter user id to delete th user">       
       <button ng-click="deleteuser(delId)" type="button" 
         class="btn btn-primary"> 

         Delete User 
       </button> 

JS:

$scope.hideStuff = function (delId) { 
       $scope.startFade = true; 
       //here i want to use the index to delete the row 
       $scope.hidden = true; 
      }; 
      $scope.deleteuser = function (dalId) {     
       var data = {delId : $scope.delId}; 
       $http.post('delete.php', data) 
        .success(function(response) { 
        $scope.hideStuff(delId); 
        }); 

      }; 
+0

您可以用$指數看文檔https://docs.angularjs.org/api/ng/directive/ngRepeat – Overmachine

+0

@Overmachine我讀它,得到的指數,但如何獲得在控制器中的函數中使用'delId'的索引 –

回答

2

您可以使用$索引請仔細閱讀本作的詳細信息: https://docs.angularjs.org/api/ng/directive/ngRepeat

我我你應該做這樣的觀點:

  <tr id="tr-{{item.id}}" ng-repeat="item in ItemsByPage[currentPage]"> 
        <td> 
         <div> 
          {{item.id}} 
         </div>         
        </td> 
        <td> 
         <div editable-text="item.name" 
          onaftersave='inlineupdateName(item.id,item.name)' 
          ng-model="tname" data-n='item.name'> 

          {{item.name}} 
         </div> 
        </td> 
        <td> 
         <div editable-text="item.phone" 
          onaftersave='inlineupdatephone(item.id,item.phone)' 
          ng-model="tphone"> 
          {{item.phone}} 
         </div> 
        </td> 
       </tr> 

      <input type="text" ng-model="delId" class="form-control" 
        placeholder="Enter user id to delete th user">       
      <button ng-click="deleteuser(delId)" type="button" 
        class="btn btn-primary"> 

        Delete User 
      </button> 

JS

 $scope.hideStuff = function (delId) { 
      $("#tr-"+delId).hide(); 
      //the entire tr (line table) will be hidden. 
      // you don't need those $scope variables to hide the elements 
     }; 
     $scope.deleteuser = function (dalId) {     
      var data = {delId : $scope.delId}; 
      $http.post('delete.php', data) 
       .success(function(response) { 
       $scope.hideStuff(delId); 
       }); 

     }; 
+0

如何通過$ scope.hideStuff = function(delId){ $ scope.startFade = true; //這裏我想使用索引刪除行 $ scope.hidden = true; };' thanx so much –

+0

@hey你不需要$ scope.hidden = true;只需使用$(「tr - 」+ delId).hide();請嘗試這種方式 –

+0

修改爲使用'{{item.id}}'作爲'' –

相關問題