AngularJS:如何将选定的表格行复制到剪贴板?

我创建了一个表,其中有一个选项可以通过复选框选择行,效果很好。


现在我正在寻找一些东西——想法、例子、类似的案例——来帮助我将那些选定的行复制到剪贴板,以便将它们一个接一个地粘贴到 Excel 文件中。


checked我可以通过控制器中的属性或通过 AngularJS 的filter功能过滤所有项目列表。


我的表:


<button ng-click="copySelected()" type="button">Copy selected to clipboard</button>


<table>

<thead>

    <tr>

        <th><input type="checkbox" ng-model="selectAll" ng-change="toggleAll()"></th>

        <th>ID</th>

        <th>Date</th>

        <th>Subject</th>

    </tr>

</thead>

<tbody ng-repeat="item in items">

    <tr>

        <td><input type="checkbox" id="chk-{{item.id}}" ng-model="options[$index]" ng-value="item.id" 

                ng-change="toggleItem($index)"></td>

        <td>{{item.id}}</td>

        <td>{{item.issueDate}}</td>

        <td>{{item.subject}}</td>

    </tr>

</tbody>

</table>

AngularJS 控制器:


$scope.selectAll = false;

$scope.options = [];



$scope.toggleItem = function(index) {

   $scope.items[index].checked = !$scope.items[index].checked;

   if (!$scope.items[index].checked) {

      $scope.selectAll = false;

   }

};

  

$scope.toggleAll = function() {

   var checked = $scope.selectAll;

   for (var i = 0; i < $scope.items.length; i++) {

     $scope.options[i] = checked;

     $scope.items[i].checked = checked;

   }

};


// this is where I don't know how to progress from

$scope.copySelected = function() {

    

    if ($scope.selectAll) {

        

    } else {

        for (var i = 0; i < $scope.items.length; i++) {

            if ($scope.items[i].checked) {

                


            }

        }

    }

};


慕虎7371278
浏览 171回答 1
1回答

慕码人2483693

一种可能性是以CSV格式转换所选项目数组。然后你可以使用浏览器剪贴板 api将 csv 有效负载写入剪贴板。当我在 Libreoffice 中执行此操作时,应用程序会提示我在复制和粘贴到工作簿时导入文本格式。在 Excel 中,这可能需要一些调整,甚至需要用户格式化数据。您也可以只使用SheetJs生成一个 Excel 文件——但对于您的用例来说可能会显得臃肿。您可以将生成的 CSV 直接发送到浏览器。剪贴板 api 在 IE 中不可用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript