简体   繁体   English

如何在表格的最后一行之后添加行

[英]How to add row after last row in table

Table contains hidden template row and zero or more visible data rows. 表包含隐藏的模板行和零个或多个可见数据行。 In end of table there is add button which shoud add new empty row to end of table. 在表格末尾有添加按钮,该按钮应将新的空行添加到表格末尾。

I tried 我试过了

function addVariablesRow() {
    var t = document.getElementById("reportVariablesTable");
    var rows = t.getElementsByTagName("tr");
    var r = rows[rows.length - 1];
    var x = rows[1].cloneNode(true);
    x.style.display = "";
    r.parentNode.insertBefore(x, r);
}

but it adds row before last row to table. 但会表格的最后一行之前添加一行。 How to add row after last row to table ? 如何在表的最后一行之后添加行?

ASP.NET MVC4 application, bootstrap 3 modal, jquery, jquery-ui are used. 使用ASP.NET MVC4应用程序,引导程序3模态,jquery,jquery-ui。

html: 的HTML:

<div class="modal" id="reportVariables" tabindex="-1" role="dialog" aria-labelledby="reportVariablesLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <table class="table table-condensed table-striped" id="reportVariablesTable">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Value</th>
                            <th>Calculate</th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- first row is hidden add template row -->
                        <tr style='display:none'>
                            <td>
                                <input type="text" name="name">
                            </td>

                            <td>
                                <textarea name="valuetostore"></textarea>
                            </td>

                            <td>
                                <select class="totaltype-select" id="totaltype" name="totaltype">
                                    <option value=""></option>
                                    <option value="Sum">Summary</option>
                                    <option value="Lowest">Smallest</option>
                                    <option value="Highest">Biggers</option>
                                </select>
                            </td>
                        </tr>

                        <!-- other are visible data rows -->
                        <tr>
                            <td>
                                <input type="text" name="name" value="variable1">
                            </td>

                            <td>
                                <textarea name="valuetostore">a-b</textarea>
                            </td>

                            <td>
                                <select class="totaltype-select" id="totaltype" name="totaltype">
                                    <option value=""></option>
                                    <option value="Sum">Summary</option>
                                    <option value="Lowest" selected>Smallest</option>
                                    <option value="Highest">Biggers</option>
                                </select>
                            </td>
                        </tr>
                        ... remaining rows
                    </tbody>
                </table>
                    <button onclick="addVariablesRow();">+</button>

            </div>
        </div>
    </div>
</div>

Update 更新资料

I ned to clone template row which is first in body and make new row visible. 我已克隆了模板行,它排在第一位,并使新行可见。 I tried: 我试过了:

function addVariablesRow() {
        var $t = $("#reportVariablesTable");
        var $row = $($t.find("tr")[1]);
        var r = $row.clone();
        r[0].style.display = "";
        r.appendTo($t);
}

Is this best code ? 这是最好的代码吗? Maybe it is bettet to find template row as first in body? 也许最好先找到模板行?

As you mentioned JQuery is an option, you could use something like this: 正如您提到的,JQuery是一个选项,您可以使用如下代码:

function addVariablesRow() {
    var $t = $("#reportVariablesTable");
    var $row = $t.find("tbody tr").first();
    var $newRow = $row.clone();
    $newRow.appendTo($t).show();
}

It can be done via jQuery like this, 可以像这样通过jQuery完成,

$('#reportVariablesTable tr:last').after('<tr>...</tr><tr>...</tr>');

You can include anything within the after() method as long as it's valid HTML, including multiple rows as per the example above. 只要它是有效的HTML,就可以在after()方法中包括任何内容,包括上面示例中的多行。

var t = document.getElementById("reportVariablesTable");
var row = document.createElement("tr")
t.appendChild(row)
$('#reportVariablesTable').find('tbody:last')
.append($('#rep‌​ortVariablesTable')
.‌​find('tbody:first').clone())‌​;

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

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