Jose Jose - 3 months ago 79
Javascript Question

How to properly delete selected items ui.grid Angular JS

I am having some difficulties understanding the properties/functions available through ui-grid. I often get confused with its previous version ng-grid. I am trying to find what the best way of deleting a checked-entry would be. Here is my markup, but due to not quite understanding if I have an index, or count available through a row entity:

HTML:

<div class="form-group">
<button type="button" id="addData" class="btn btn-success" ng-click="addData()">Add Data</button>
<button type="button" id="removeData" class="btn btn-success" ng-click="removeData()">Remove First Row</button>
<br>
<br>
<div id="grid1" ui-grid="gridOptions" ui-grid-edit ui-grid-selection external-scopes="myViewModel" class="grid"></div>
</div>


which lies under my controller:

$scope.removeData = function () {
console.log($scope.gridApi.selection.getSelectedRows());

var items = $scope.gridApi.selection.getSelectedRows();

angular.forEach($scope.myData, function (data, index) {
angular.forEach(items, function (item) {
if (item.displayValue = data.displayValue)
{
$scope.myData.splice(index, 1);
}

});


where
displayValue
is a property of my column and
$scope.myData
is my data. Is there a different way to send that selection to the controller for removal? The current way I have it does NOT work (obviously). Any help will be appreciated. If my markup is incomplete, I'll update it as necessary. Thank you!

Answer

Your solution looks a little complicated. Here is my delete function:

$scope.deleteSelected = function(){
  angular.forEach($scope.gridApi.selection.getSelectedRows(), function (data, index) {
    $scope.gridOptions.data.splice($scope.gridOptions.data.lastIndexOf(data), 1);
  });
}

Here is a plunker based on the 210_selection tutorial.