使用 jQuery 将数据从一个表的选定行复制到另一个表

2024-03-01

我有两张表,其中一张有我的产品数据,例如名称和条形码。 另一个是空的,我想复制产品表(仅限选定的行)通过 jQuery 进入第二个表。

<table id="exampleTable1" style="max-width:50%;">
    <thead>
        <tr>
            <th>bar-code</th>
            <th>product name</th>
        </tr>
    </thead>
    <tbody>
        <tr role="row" class="odd selected">
            <td class="sorting_1">545333456</td>
            <td>Galaxy S9</td>
        </tr>
        <tr role="row" class="even selected">
            <td class="sorting_1">876543</td>
            <td>Galaxy S6</td>
        </tr>
        <tr role="row" class="odd">
            <td class="sorting_1">407654</td>
            <td>SD 64G </td>
        </tr>
        <tr role="row" class="even selected">
            <td class="sorting_1">876543</td>
            <td>Galaxy S5</td>
        <tr role="row" class="odd">
            <td class="sorting_1">407654</td>
            <td>Iphone 7 </td>
        </tr>
    </tbody>
</table>

我的第二张桌子:

<table id="exampleTable2" style="max-width:50%;">
    <thead>
        <tr>
            <th>bar-code</th>
            <th>product name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
        </tr>
        <tr>
        </tr>
    </tbody>
</table>

<button class="btn btn-success" data-panelId="copy1" id="copy1">
    Copy from exampleTable1 To exampleTable1
</button>

有一些 jQuery 方法可以轻松做到这一点,即.clone() https://api.jquery.com/clone/ and .each() https://api.jquery.com/each/方法。您可以通过以下方式实现您想要的:

$('#copy1').click(function() {

$('tr.selected', '#exampleTable1').each(function() {

    // For each "selected" row of table1 ..
    var rowFromTable1 = $(this);

    // .. Take a clone/copy of it ..
    var clonedRowFromTable1 = rowFromTable1.clone();

    // .. And append the cloned row to the tbody of table2
    $('tbody', '#exampleTable2').append( clonedRowFromTable1 )
 })

 })
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 jQuery 将数据从一个表的选定行复制到另一个表 的相关文章

随机推荐