2

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It works! I needed to add $scope.gridApi.selection.unSelectRow(selRow); to avoid the "selected items" count being ever increasing. I just wonder whether there is a better solution.
It's probably a design decision to optimise not updating anything on a grid row, if the identity matches with the previous data set. Only the developers know ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.