admin管理员组文章数量:1410724
I am using this reference to copy my table(paginated) to clipboard- Select a plete table with Javascript (to be copied to clipboard) But the problem I am facing here is, it is just copying the first page data. I need all the rows to be copied, irrespective of which page I am on. I am using this in an angular application. Kindly provide me a work around for this.
I am using this reference to copy my table(paginated) to clipboard- Select a plete table with Javascript (to be copied to clipboard) But the problem I am facing here is, it is just copying the first page data. I need all the rows to be copied, irrespective of which page I am on. I am using this in an angular application. Kindly provide me a work around for this.
Share Improve this question asked Aug 28, 2018 at 16:58 Sanskriti RoutraySanskriti Routray 251 silver badge6 bronze badges 2- I am passing my table's id to the function. – Sanskriti Routray Commented Aug 28, 2018 at 16:59
- Please consider creating a StackBlitz. – SiddAjmera Commented Aug 28, 2018 at 17:02
1 Answer
Reset to default 5It can be done by JavaScript only.
It can be done in two step:
Step 1 : Select table get using selection mand
Step 2 : Apply clipboard using
document.execCommand("copy");
Please check below :
function selectElementContents(el) {
var body = document.body, range, sel;
if (document.createRange && window.getSelection) {
range = document.createRange();
sel = window.getSelection();
sel.removeAllRanges();
try {
range.selectNodeContents(el);
sel.addRange(range);
} catch (e) {
range.selectNode(el);
sel.addRange(range);
}
document.execCommand("copy");
} else if (body.createTextRange) {
range = body.createTextRange();
range.moveToElementText(el);
range.select();
range.execCommand("Copy");
}
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tableId">
<thead>
<tr><th>Heading 1</th><th>Heading 2</th></tr>
</thead>
<tbody>
<tr><td>cell 1</td><td>cell 2</td></tr>
<tr><td>cell 3</td><td>cell 4</td></tr>
</tbody>
</table>
<input type="button" value="select table"
onclick="selectElementContents( document.getElementById('tableId') );">
Now Ctrl+V
paste clipboard value as per your requirement
本文标签: javascriptCopy table rows to clipboard copying only the first pageStack Overflow
版权声明:本文标题:javascript - Copy table rows to clipboard- copying only the first page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745010765a2637539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论