2017-04-04 63 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

感謝兄弟,正在尋找相同的:) –