How can I remove the entire row from the onclick table of a button?
I have two buttons in each row of the table. Onclick any of the buttons the whole row should be removed from the table and also I am sending some value to the spring controller. It must be updated in the database and also delete the entire row. I am using angularjs, spring-mvc and 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);
});
}
+3
Girish
source
to share
2 answers
It's good that you use ng-repeat
for your strings, iterate over taskDetails
so you can just remove one entry from the array, like
$scope.rejectTask = function (index, task) {
$scope.taskDetails.splice(index, 1);
}
0
Dan Moldovan
source
to share
In your 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.
In your 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
});
}
0
Manish Kr. Shukla
source
to share