2017-04-04 67 views
1

我想在angular-ui网格中选择一行并将该行复制到剪贴板。在角度ui网格中选择一行时只获取可见列

这是我的代码:

$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

不过,我只是想复制的可见列,但我得到所有这一切都在JSON列。我不想要隐藏的列。我怎么做?请帮忙。

+0

可以添加小提琴吗? –

+0

@MuhammedNeswine我编辑了我的问题 –

回答

2

您可以在选定列/可见列的基础上调制列。你可以有一个这样的代码 -

$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(); 
    }; 

查找更新Plunker Here

希望这将解决您的问题!

+0

感谢兄弟,正在寻找相同的:) –