I have authorization.html with records listing as it’s generated in JHipster.
Each record has it’s own delete button, but if I need to delete many of them, it’s more convenient to have checkboxes assigned to each record and one button to delete selected records.
My entity is productDelivered not authorization.
My entity is productDelivered not authorization.
In html file, I added checkboxes:
<th><input type="checkbox" ng-click="vm.allNeedsClicked()" ng-checked="vm.allNeedsMet()"></th>
...
<tbody> <tr ng-repeat="productDelivered in vm.authorizations track by productDelivered.id"> <td><input type='checkbox' ng-model="productDelivered.checked"></td> <td><a ui-sref="product-delivered-detail({id:productDelivered.id})">{{productDelivered.id}}</a></td>
[ng-click="vm.allNeedsClicked()" ng-checked="vm.allNeedsMet()" are functions for selecting all checkboxes]
and below </table> added:
<br> <button ng-click="vm.deleteSelectedItems()" class="pull-left btn btn-danger" type="button" title="Delete"> Delete selected <span class="glyphicon glyphicon-trash"></span> </button>
In authorization.controller.js added:
function deleteSelectedItems() { // pressed button in the bottom Authorization.update({id: 7}); var deletedIndex=[]; angular.forEach(vm.authorizations, function(productDelivered) { if (productDelivered.checked) { deletedIndex.push(vm.authorizations.indexOf(productDelivered)); ProductDelivered.delete({id: productDelivered.id}); } }); // splice usuwa element tablicy przez co zmienia indeksację powodując że usuwany jest tylko co drugi element, dlatego przed tym tablica jest odwracana deletedIndex.reverse(); deletedIndex.forEach(function(elt) { vm.authorizations.splice(elt, 1); }) } function allNeedsClicked() { var newValue = !vm.allNeedsMet(); angular.forEach(vm.authorizations, function (productDelivered) { productDelivered.checked = newValue; }); }; // Returns true if and only if all todos are done. function allNeedsMet() { var needsMet = vm.authorizations.reduce(function (memo, productDelivered) { return memo + (productDelivered.checked ? 1 : 0); }, 0); return (needsMet === vm.authorizations.length); };
On the top we need also:
vm.deleteSelectedItems=deleteSelectedItems; vm.allNeedsClicked=allNeedsClicked; vm.allNeedsMet=allNeedsMet;
Warning! It deletes records without any confirmation!
Brak komentarzy:
Prześlij komentarz