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.

Aasim Hussain Khan
  • 1,137
  • 3
  • 15
  • 33

1 Answers1

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!

Viplock
  • 3,259
  • 1
  • 23
  • 32