如何删除表格中的整行按钮?

时间:2015-05-21 09:46:25

标签: angularjs spring-mvc

我在表格的每一行都有两个按钮。点击任何一个按钮,应该从表中删除整行,并且我还要向弹簧控制器发送一些值。它应该在数据库中更新,并且应该删除整行。我使用的是angularjs,spring-mvc和mongodb。

 //   .html file

    <table class="table table-bordered">
        <thead>
            <tr>
                <th style="text-align: center;">Task name</th>
                <th style="text-align: center;">Owner name</th>
                <th style="text-align: center;">Authorize</th>
            </tr>
        </thead>
            <tbody>
                <tr ng-repeat="task in taskDetails">
                    <td style="text-align: center;">{{task.name}}</td>
                    <!-- <td style="text-align: center;">{{task.owners}}</td> -->
                    <td style="text-align: center;">
                        <span ng-repeat="owner in task.owners">{{owner.ownerName.name}}{{$last ? '' : ', '}}</span>
                    </td>
                    <td  style="text-align:center;">
                    <!-- <button class="btn btn-mini btn-primary" ng-click="approveTask(taskDetails.indexOf({{task.id}}), task)" value="approveTask">Approve</button>
                    <button class="btn btn-mini btn-danger" ng-click="rejectTask(taskDetails.indexOf({{task.id}}), task)" value="approveTask">Reject</button>
                     -->
                     <button class="btn btn-mini btn-primary" ng-click="approveTask(task)" value="approveTask">Approve</button>
                    <button class="btn btn-mini btn-danger" ng-click="rejectTask(task)" value="rejectTask">Reject</button>

                     </td>
                </tr>

            </tbody>
      </table>



//controller.js

$scope.approveTask = function(task) {
          $http.put("/task/approve/"+ task.id).success(function(data) {
             alert("Approved! "+ data);
          });
        }

        $scope.rejectTask = function(task) {
          $http.put("/task/reject/"+ task.id).success(function(data) {
             alert("Rejected! "+ data);
          });
        }

2 个答案:

答案 0 :(得分:0)

你正在使用ng-repeat代替你的行,迭代taskDetails,这样你就可以从数组中删除那一个条目

$scope.rejectTask = function (index, task) {
     $scope.taskDetails.splice(index, 1);
}

答案 1 :(得分:0)

在您的HTML中:

    <button class="btn btn-mini btn-danger" ng-click="rejectTask($index)" value="rejectTask">Reject</button>

    // We just need to pass the index for accessing the relevant object.

在您的Javascript中:

    $scope.rejectTask = function($index) {
        $scope.currentIndex = $index;

        // You may want to show some screen loader to prevent user intervention during the ajax call.

      $http.put("/task/reject/"+ task.id).success(function(data) {
         // AS the ajax call is complete, it is now safe to splice the array.
         $scope.taskDetails.splice($scope.currentIndex, 1);
         $scope.currentIndex = -1;
         //Remove the loader here
      });
    }