Ed Griffin
Ed Griffin

Reputation: 486

Preserve selection in angular ui-grid while updating data

http://plnkr.co/edit/r9hMZk?p=preview

I have a ui-grid where I have enabled multi selection. I want to be able to update the data whilst preserving the selection. If I just update the data then the selection is not preserved.

$scope.gridOpts.data = data2;

However I have defined a rowIdentity function, so that the id column uniquely identifies a row.

"rowIdentity" : function(row) {
  return row.id;
}

Now if I select rows with id=Bob and id=Lorraine, then update the data, the rows are still selected. However the other fields in those rows are not updated.

How can I both preserve the selection and update all the data?

Upvotes: 2

Views: 1176

Answers (1)

Mads Nielsen
Mads Nielsen

Reputation: 644

I think you need to keep track of you IDs yourself. So, you should remove the rowIdentifier, and instead add this piece at the beginning of your swapData function.

    $scope.selIds = [];
    for (var selRow of $scope.gridApi.selection.getSelectedRows()) {
      $scope.selIds.push(selRow.id);
    }

In addition to that, add an event handler on rowsRendered to re-select the previously selected rows

  gridApi.core.on.rowsRendered($scope,function() {
    for (var selId of $scope.selIds) {
      for (var row of $scope.gridOpts.data) {
        if (selId == row.id) {
          $scope.gridApi.selection.selectRow(row);
        }
      }
    }
  });

You can put this in your registerApi callback.

Upvotes: 3

Related Questions