【问题标题】:How to add row after last row in table如何在表格的最后一行之后添加行
【发布时间】:2017-03-06 18:32:43
【问题描述】:

表格包含隐藏的模板行和零个或多个可见数据行。 在表的末尾有一个添加按钮,应该将新的空行添加到表的末尾。

我试过了

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);
}

但它会将行 before 最后一行添加到表中。如何在表的最后一行之后添加行?

使用了ASP.NET MVC4应用,bootstrap 3 modal,jquery,jquery-ui。

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>

更新

我需要克隆位于正文中的第一个模板行并使新行可见。 我试过了:

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

这是最好的代码吗?也许最好在正文中找到模板行作为第一个?

【问题讨论】:

    标签: javascript jquery asp.net html twitter-bootstrap


    【解决方案1】:

    正如你提到的 JQuery 是一个选项,你可以使用这样的东西:

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

    【讨论】:

    • 我需要新行是空的。对于这个可能隐藏的模板行,应该克隆并使其可见。您对可能包含数据的最后一行进行代码克隆。我使用根据您的回答创建的代码更新了问题。这是最好的代码风格吗?
    • 第一行是表头。此编辑可能会克隆表头。如何获取表体的第一行?
    • @Andrus 再试一次... :)
    【解决方案2】:

    可以像这样通过jQuery来完成,

    $('#reportVariablesTable tr:last').after('&lt;tr&gt;...&lt;/tr&gt;&lt;tr&gt;...&lt;/tr&gt;');

    您可以在 after() 方法中包含任何内容,只要它是有效的 HTML,包括上面示例中的多行。

    【讨论】:

    • 如何从隐藏的模板行(表中的第一行)中获取 html 并添加此 html 以便新行包含数据输入元素?
    【解决方案3】:
    var t = document.getElementById("reportVariablesTable");
    var row = document.createElement("tr")
    t.appendChild(row)
    

    【讨论】:

    • 这会添加空行。如何从模板中添加行,使其包含模板中的输入、选择和 talbeare 元素。
    【解决方案4】:
    $('#reportVariablesTable').find('tbody:last')
    .append($('#rep‌​ortVariablesTable')
    .‌​find('tbody:first').clone())‌​;
    

    【讨论】:

    • 这会添加空行。如何从模板行(表中的第一行)获取html并添加包含输入元素的行?
    • 基本上你想要一个副本到最后。然后添加选择第一个 tr 的结果。我已经编辑了答案以包含您的要求
    • 更新的代码没有添加任何行。可能clone() 不见了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2018-03-20
    • 2018-01-13
    • 1970-01-01
    相关资源
    最近更新 更多