简体   繁体   English

Javascript 或 Jquery 将一个特定行的特定列从一个表克隆或复制到另一个表

[英]Javascript or Jquery to clone or copy specific columns of one particular rows from one table to other table

I need to clone rows (one at a time) from Table A to Table B. The columns Products and Quantity should remain as is.我需要将行(一次一个)从表 A 克隆到表 B。产品和数量列应保持原样。 Then I need to complete the third column of table B (Final Price) with the value, as text and not inside an input, from the multiplication between the columns Quantity and List Price of table A. And finally for the last column, i have to replace the green cart buttom with a new one to delete the row in case of mistake when i writed the quantity.然后我需要完成表 B 的第三列(最终价格)的值,作为文本,而不是在输入中,从表 A 的数量和标价列之间的乘法。最后对于最后一列,我有用新的替换绿色购物车按钮以删除行,以防我在写数量时出错。

With my java code I have only managed to clone all the columns and that's all I've been able to do.使用我的 java 代码,我只设法克隆了所有列,这就是我所能做的。

I share with you the images of each table and the html and javascript code used.我与您分享每张桌子的图像以及使用的 html 和 javascript 代码。

Table A表 A

<table class="table table-sm table-striped">
        <thead>
            <tr class="text-center">
                <th scope="col" style="width:50%">Product</th>
                <th scope="col" style="width:20%">List Price</th>
                <th scope="col" style="width:15%">Quantity</th>
                <th scope="col" style="width:15%">Add</th>
            </tr>
        </thead>
        <tbody id="searchbody">
            @foreach ($allProductPrice as $product)
            <tr data-id="" class="text-center">


                <td scope="col" class="align-middle">{{$product->Product}}</td>
                <td scope="col" class="align-middle">{{$product->Price}}</td>
                <td scope="col" class="align-middle">
                    <input type="number" class="form-control input-sm" min="0" value="0" name="quantity">
                </td>
                <td scope="col" class="align-middle">
                    <button type="button" class="btn btn-success btn-sm cart"><i class="fas fa-shopping-cart"></i></button>
                </td>

            </tr>
            @endforeach
        </tbody>
    </table>

Table B表 B

<table class="table table-sm table-striped" id="selection">
        <thead>
            <tr class="text-center">
                <th scope="col" style="width:50%">Product</th>
                <th scope="col" style="width:15%">Quantity</th>
                <th scope="col" style="width:20%">Final Price</th>
                <th scope="col" style="width:15%">Remove</th>
            </tr>
        </thead>
        <tbody>
            <tr data-id="" class="text-center">

                <td scope="col" class="align-middle"></td>
                <td scope="col" class="align-middle"></td>
                <td scope="col" class="align-middle"></td>
                <td scope="col" class="align-middle">
                    <button type="button" id="delete" class="btn btn-danger btn-sm"><i class="fas fa-trash"></i></button>
                </td>

            </tr>
        </tbody>
    </table>

Javascript Javascript

var items = [];

    $(".cart").on("click", function() {
        var newTr = $(this).closest("tr").clone();
        items.push(newTr);
        newTr.appendTo( $("#selection") );
    });

You can use .text() or val() to get value of quantity and price from your cloned row then we can update those value using td:eq("columnno") and to replace cart button with delete button,use .replaceWith() method.您可以使用.text()val()从克隆的行中获取数量和价格的值,然后我们可以使用td:eq("columnno")更新这些值并将购物车按钮替换为删除按钮,使用.replaceWith()方法。

Demo Code :演示代码

 $(".cart").on("click", function() { var newTr = $(this).closest("tr").clone(); //getting qty var name = newTr.find("td.name").text(); var exist=false; var qty = newTr.find("td input").val(); //getting price var price = newTr.find("td.price").text(); //add qty to column 1 newTr.find('td:eq(1)').text(qty); //add final price to column 2 newTr.find('td:eq(2)').text(qty * price); //replace cart buton with delete newTr.find("td:eq(3)").replaceWith('<button type="button" id="delete" class="btn btn-danger btn-sm"><i class="fa fa-trash"></i></button>'); //append to table newTr.appendTo($("#selection")); });
 <:-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https.//maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min:css"> <link rel="stylesheet" href="https.//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min:css"> <.-- jQuery library --> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery:min.js"></script> <.-- Latest compiled JavaScript --> <script src="https.//maxcdn.bootstrapcdn.com/bootstrap/3.4:1/js/bootstrap:min:js"></script> <table class="table table-sm table-striped"> <thead> <tr class="text-center"> <th scope="col" style="width:50%">Product</th> <th scope="col" style="width:20%">List Price</th> <th scope="col" style="width:15%">Quantity</th> <th scope="col" style="width:15%">Add</th> </tr> </thead> <tbody id="searchbody"> <tr data-id="" class="text-center"> <:--added class name and price--> <td scope="col" class="align-middle name" value="Abc">Abc</td> <td scope="col" class="align-middle price">15</td> <td scope="col" class="align-middle"> <input type="number" class="form-control input-sm" min="0" value="0" name="quantity"> </td> <td scope="col" class="align-middle"> <button type="button" class="btn btn-success btn-sm cart"><i class="fa fa-shopping-cart"></i></button> </td> </tr> <tr data-id="" class="text-center"> <td scope="col" class="align-middle name" value="Abcd">Abcd</td> <td scope="col" class="align-middle price">150</td> <td scope="col" class="align-middle"> <input type="number" class="form-control input-sm" min="0" value="0" name="quantity"> </td> <td scope="col" class="align-middle"> <button type="button" class="btn btn-success btn-sm cart"><i class="fa fa-shopping-cart"></i></button> </td> </tr> </tbody> </table> <table class="table table-sm table-striped" id="selection"> <thead> <tr class="text-center"> <th scope="col" style="width:50%">Product</th> <th scope="col" style="width:15%">Quantity</th> <th scope="col" style="width:20%">Final Price</th> <th scope="col" style="width:15%">Remove</th> </tr> </thead> <tbody> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM