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
Add a ment  | 

1 Answer 1

Reset to default 5

It 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