在角度 ui-grid 中选择行时仅获取可见列



我想在 angular-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();
  };

普伦克:http://plnkr.co/edit/dcj7DUWHyA3u1bouxRhI?p=preview

但是,我只想复制可见列,但我正在获取 JSON 中的所有列。我不想要隐藏的列。我该怎么做?请帮忙。

您可以在所选列/可见列的基础上调制列。 你可以有这样的代码——

 $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

希望它能解决您的问题!

最新更新