简体   繁体   English

如何在最后一个表格行上插入新行?

[英]how to insert new row on last table row?

I want to show new row above on the total Price & how to insert the new row above on the last row when i click on the add button. 我想在总价上方显示新行,并在单击添加按钮时如何在最后一行上方插入新行。

Thanks You so much! 非常感谢!

Jquery jQuery的

//for addPrice click button
    $('#add_price').on('click',function(){
        if($('.supplier').is(":checked") == true){

           $('.addcost_table tbody').append('<tr><td>Hotel</td><td>Twin Room</td><td>TWN</td><td>5</td><td class="cost_price">100</td><td class="selling_price">120</td></tr>');
           $('table .selling_price').hide();
        }else{

        }

    });

HTML HTML

<table class="addcost_table table tablesorter">
            <thead>
                <tr class="header_row">
                    <th>Item Group</th>
                    <th>Name</th>
                    <th>Code</th>
                    <th>Quantity</th>
                    <th class="cost_price">Unit Cost Price</th>
                    <th class="selling_price">Unit Selling Price</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                   <td>Hotel</td>
                   <td>Twin Room</td>
                   <td>TWN</td>
                   <td>5</td>
                   <td class="cost_price">100</td>
                   <td class="selling_price">120</td>
                </tr>
                <tr>
                   <td>Hotel</td>
                   <td>Single Room</td>
                   <td>SGL</td>
                   <td>1</td>
                   <td class="cost_price">80</td>
                   <td class="selling_price">100</td>
                </tr>
                 <tr>
                   <td>Meals</td>
                   <td>Buffet Breakfast</td>
                   <td>BRE</td>
                   <td>2</td>
                   <td class="cost_price">10</td>
                   <td class="selling_price">10</td>
                </tr>
                <tr>
                   <td  colspan="4">Total Price X Qty</td>
                   <td class="cost_price">$500</td>
                   <td class="selling_price">$500</td>

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

what is the best to solve this problem?. 什么是解决此问题的最佳方法? Thanks u 谢谢你

$('.addcost_table > tbody:last').before('<tr><td>Hotel</td><td>Twin Room</td><td>TWN</td><td>5</td><td class="cost_price">100</td><td class="selling_price">120</td></tr>');

You can user before selector for that, Get the last tr of the tbody by .addcost_table tbody tr:last and then append your tr by using before key 您可以为此使用before选择器,通过.addcost_table tbody tr:last获取tbody的最后一个tr ,然后使用before键附加您的tr

Your script should like this, 您的script should like this,

$('#add_price').on('click',function(){
        if($('.supplier').is(":checked") == true){

           $('.addcost_table tbody tr:last').before('<tr><td>Hotel</td><td>Twin Room</td><td>TWN</td><td>5</td><td class="cost_price">100</td><td class="selling_price">120</td></tr>');
           $('table .selling_price').hide();
        }else{

        }

    });

Here is Example FIDDLE 这是示例FIDDLE

The existing answers stating to use tr:last as the selector and .before as the method are correct for what you're asking, but thought I'd throw this out as an alternative: Why not have a tfoot section that contains the total price row instead? 现有答案表明使用tr:last作为选择器,使用.before作为方法对您的要求是正确的,但我想将其作为替代方法:为什么不使用包含总价格的tfoot部分代替吗?

This can be more convenient if you'd like to style the total row differently. 如果您想对总行设置不同的样式,这会更方便。 For example, if you want a scrollbar for the line items (on just the tbody) but always want to display the thead and tfooter. 例如,如果您想为订单项使用滚动条(仅在tbody上),但始终希望显示thead和tfooter。 If you want a count of how many rows of data (sans total) then this would be more natural too, so you can just get a count of tbody tr rather than having to subtract the total row. 如果您想要统计数据的行数(无总值),那么这也将更加自然,因此您只需获得tbody tr的计数即可,而不必减去总行数。 It may also be slightly more readily understandable when looking at CSS styling (subjective.) 在查看CSS样式(主观)时,它可能也更容易理解。

In addition, your existing $('.addcost_table tbody').append would work as is. 另外,您现有的$('.addcost_table tbody').append$('.addcost_table tbody').append工作。

You can also try with eq and after like 您也可以尝试使用eq,然后点赞

var row = $('.addcost_table>tbody>tr').length;  //get the no of tr
$('.addcost_table>tbody>tr').eq(row-1); //total row -1 is above the Total price tr
     .before('<tr><td>Hotel</td><td>Twin Room</td><td>TWN</td><td>5</td><td class="cost_price">100</td><td class="selling_price">120</td></tr>');

JSFiddle 的jsfiddle

尝试这个

$('.addcost_table tbody tr:last').before($('.addcost_table tbody tr:eq(0)').clone(true));

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

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