如何解决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) {
}
}
}
};
解决方法
一种可能性是以CSV格式转换所选项目数组。然后,您可以使用浏览器clipboard api将csv有效内容写入剪贴板。
当我在Libreoffice中执行此操作时,应用程序会提示我在复制并将其粘贴到工作簿时导入文本格式。在Excel中,这可能需要一些调整,或者甚至需要用户格式化数据。
- 您也可以只使用SheetJs生成Excel文件-但可能会因您的用例而肿。
- 您可以将生成的CSV直接发送到浏览器。
剪贴板api在IE中不可用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。