1

I want to select a row in angular-ui grid and copy the row to clip board.

This is my code:

  $scope.copySelection = function() {
    $scope.retainSelection = $scope.gridApi.selection.getSelectedRows();
    alert(JSON.stringify($scope.retainSelection));
    var input = document.createElement("input");
    input.type = "text";
    document.getElementsByTagName('body')[0].appendChild(input);
    input.value = JSON.stringify($scope.retainSelection);
    input.select();
    document.execCommand("copy");
    input.hidden = true;
    $scope.gridApi.selection.clearSelectedRows();
  };

Plunker: http://plnkr.co/edit/dcj7DUWHyA3u1bouxRhI?p=preview

However, I just want to copy the visible columns, but I am getting all the columns which are in the JSON. I dont want the hidden columns. How do I do that? Please help.

2
  • can u add a fiddle? Commented Apr 4, 2017 at 13:09
  • @MuhammedNeswine I have edited my question Commented Apr 4, 2017 at 13:20

1 Answer 1

2

You can modulate the columns on the bases of selected columns/ visible columns. you can have a code like this -

 $scope.copySelection = function() {

    $scope.retainSelection =angular.copy($scope.gridApi.selection.getSelectedRows());

    angular.forEach($scope.retainSelection,function(value,key){
       var columndef=angular.copy( $scope.gridOptions.columnDefs);
      for (var property in value) {
       if (!(value.hasOwnProperty(property) && columndef.filter(function(a){return a.name.split('.')[0]===property}).length>0 )) {
        delete value[property];
      }
    }

    });
    alert(JSON.stringify($scope.retainSelection));
    var input = document.createElement("input");
    input.type = "text";
    document.getElementsByTagName('body')[0].appendChild(input);
    input.value = JSON.stringify($scope.retainSelection);
    input.select();
    document.execCommand("copy");
    input.hidden = true;
    $scope.gridApi.selection.clearSelectedRows();
  };

Find Updated Plunker Here

Hope it will solve your problem!

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

1 Comment

Thanks Bro, was looking for the same :)

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.