javascript - 在 Angular ui-grid 中选择一行时仅获取可见列

标签 javascript angularjs angular-ui-grid 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 Here

希望能解决您的问题!

关于javascript - 在 Angular ui-grid 中选择一行时仅获取可见列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43208087/

相关文章:

javascript - return 和 return() 有什么区别?

javascript - 如何在Javascript中使用reduce函数编写嵌套数组

javascript - angularjs:如何使用 $http 进行即发即弃

javascript - 如何将 ui-grid 导出到 excel 文件?

javascript - 切换效果 了解更多

javascript - AngularJS 动态下拉选项上的 glyphicon

javascript - 如何结合 Passport 和 Angular 用户界面路由

javascript - 通过绑定(bind)获取Protractor中的UI-Grid列数据

angularjs - Angular UI Grid 3.x 版本中的操作按钮

javascript - 单击另一个 DIV 重新加载一个 DIV