如何通过JS在HTML中复制多个选中的行?

huangapple go评论65阅读模式
英文:

how to copy multiple checked rows in html via JS

问题

我有一个包含表格的HTML Blade页面,我正在尝试使用以下脚本复制HTML中选中的行:

<script>
    document.addEventListener('DOMContentLoaded', function() {

        $('#checkAll').on('change', function() {
            $('.rowCheckbox').prop('checked', this.checked);
        });

        const copySelectedButton = document.getElementById('copySelected');
        copySelectedButton.addEventListener('click', function() {
            const selectedRows = document.querySelectorAll('input.rowCheckbox:checked');
            let textToCopy = '';

            selectedRows.forEach(row => {
                const text = row.getAttribute('data-clipboard-text');
                textToCopy += text + '\n';
            });

            if (selectedRows.length > 0) {
                copyToClipboard(textToCopy);

                iziToast.success({
                    title: 'Done',
                    message: 'Done',
                    position: 'bottomRight'
                });
            } else {
                iziToast.info({
                    title: 'No Records',
                    message: 'No Records',
                    position: 'bottomRight'
                });
            }
        });

        function copyToClipboard(text) {
            const tempTextarea = document.createElement('textarea');
            tempTextarea.value = text;
            document.body.appendChild(tempTextarea);
            tempTextarea.select();
            document.execCommand('copy');
            document.body.removeChild(tempTextarea);
        }
    });
</script>

它能够工作,但是每一行都返回空值,换句话说,我得到了复制的结果,但是没有得到每一行的值,而是得到了以下结果:

<br>
null
<br>
null
<br>
null
<br>
null
<br>
null

如果我选中了5行。

我正在使用Laravel,并且该代码位于视图中:

<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th style="width: 5%;"><input type="checkbox" id="checkAll"></th>
            <th style="width: 50%;">agent</th>
            <th style="width: 20%;">curr</th>
            <th style="width: 20%;">ras</th>
            <th style="width: 5%;">copy</th>
        </tr>
    </thead>
    <tbody>
        @foreach($result as $x)
        <tr>
            <td><input type="checkbox" class="rowCheckbox"></td>
            <td>{{ $x->AccName }}</td>
            <td>{{ $x->AccCurrName }}</td>
            <td>{{ number_format($x->AccRaseed, 0) }}</td>
            <td>
                <a class="btn btn-sm btn-info btncopy" data-clipboard-text="agent: {{ $x->AccName }}&#13;curr: {{ $x->AccCurrName }}&#13;ras: {{ number_format($x->AccRaseed, 0) }}">
                    <i class="las la-copy"></i>
                </a>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>

请问问题出在哪里?

英文:

I have a HTML blade page that contains a table and I'm trying to copy the checked rows in HTML by using the following script:

&lt;script&gt;
    document.addEventListener(&#39;DOMContentLoaded&#39;, function() {

        $(&#39;#checkAll&#39;).on(&#39;change&#39;, function() {
            $(&#39;.rowCheckbox&#39;).prop(&#39;checked&#39;, this.checked);
        });

        const copySelectedButton = document.getElementById(&#39;copySelected&#39;);
        copySelectedButton.addEventListener(&#39;click&#39;, function() {
            const selectedRows = document.querySelectorAll(&#39;input.rowCheckbox:checked&#39;);
            let textToCopy = &#39;&#39;;

            selectedRows.forEach(row =&gt; {
                const text = row.getAttribute(&#39;data-clipboard-text&#39;);
                textToCopy += text + &#39;\n&#39;;
            });

            if (selectedRows.length &gt; 0) {
                copyToClipboard(textToCopy);

                iziToast.success({
                    title: &#39;Done&#39;,
                    message: &#39;Done&#39;,
                    position: &#39;bottomRight&#39;
                });
            } else {
                iziToast.info({
                    title: &#39;No Records&#39;,
                    message: &#39;No Records&#39;,
                    position: &#39;bottomRight&#39;
                });
            }
        });

        function copyToClipboard(text) {
            const tempTextarea = document.createElement(&#39;textarea&#39;);
            tempTextarea.value = text;
            document.body.appendChild(tempTextarea);
            tempTextarea.select();
            document.execCommand(&#39;copy&#39;);
            document.body.removeChild(tempTextarea);
        }
    });
&lt;/script&gt;

it's working but returns Null values in each row, in the other saying I got the copied results but I don't got the values for each row I got instead
<br>
null
<br>
null
<br>
null
<br>
null
<br>
null

if I checked 5 rows

I'm using Laravel and that code inside view.

&lt;table id=&quot;example&quot; class=&quot;display&quot; style=&quot;width:100%&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th style=&quot;width: 5%;&quot;&gt;&lt;input type=&quot;checkbox&quot; id=&quot;checkAll&quot;&gt;&lt;/th&gt;
            &lt;th style=&quot;width: 50%;&quot;&gt;agent&lt;/th&gt;
            &lt;th style=&quot;width: 20%;&quot;&gt;curr&lt;/th&gt;
            &lt;th style=&quot;width: 20%;&quot;&gt;ras&lt;/th&gt;
            &lt;th style=&quot;width: 5%;&quot;&gt;copy&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        @foreach($result as $x)
        &lt;tr&gt;
            &lt;td&gt;&lt;input type=&quot;checkbox&quot; class=&quot;rowCheckbox&quot;&gt;&lt;/td&gt;
            &lt;td&gt;{{ $x-&gt;AccName }}&lt;/td&gt;
            &lt;td&gt;{{ $x-&gt;AccCurrName }}&lt;/td&gt;
            &lt;td&gt;{{ number_format($x-&gt;AccRaseed, 0) }}&lt;/td&gt;
            &lt;td&gt;
                &lt;a class=&quot;btn btn-sm btn-info btncopy&quot; data-clipboard-text=&quot;agent: {{ $x-&gt;AccName }}&amp;#13;curr: {{ $x-&gt;AccCurrName }}&amp;#13;ras: {{ number_format($x-&gt;AccRaseed, 0) }}&quot;&gt;
                    &lt;i class=&quot;las la-copy&quot;&gt;&lt;/i&gt;
                &lt;/a&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        @endforeach
    &lt;/tbody&gt;
&lt;/table&gt;

what's the problem please

答案1

得分: 0

我添加了一个变量来保存数据:

@php
    $lineBreak = "\n";
    $clipboardText = "姓名:" . $x->AccName . $lineBreak . "货币:" . $x->AccCurrName . $lineBreak . "余额:" . number_format($x->AccRaseed, 0). $lineBreak;
    @endphp

然后我将复选框更改为:

 <td><input type="checkbox" class="rowCheckbox" data-clipboard-text="{{ $clipboardText }}"></td>
英文:

I added variable to keep the data:

@php
    $lineBreak = &quot;\n&quot;;
    $clipboardText = &quot;الاسم: &quot; . $x-&gt;AccName . $lineBreak . &quot;العملة: &quot; . $x-&gt;AccCurrName . $lineBreak . &quot;الرصيد: &quot; . number_format($x-&gt;AccRaseed, 0). $lineBreak;
    @endphp

and then I changed the checkbox to be:

 &lt;td&gt;&lt;input type=&quot;checkbox&quot; class=&quot;rowCheckbox&quot; data-clipboard-text=&quot;{{ $clipboardText }}&quot;&gt;&lt;/td&gt;

huangapple
  • 本文由 发表于 2023年7月27日 17:42:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定