繁体   English   中英

在部分视图中访问表

[英]Accessing Table in Partial View

我正在尝试通过添加行来编辑表,但遇到部分视图无法完全呈现的问题(这是我的假设)

我通过页面加载和Ajax调用将部分加载到其div中。

<div id="preTestSteps">
</div>

<div id="mainTestSteps">
</div>

<div id="postTestSteps">
</div>

脚本;

    $(document).ready(function() {
        var testSuiteExecutionId = @(Model.TestSuiteExecutionId);
        var testSuiteId = @(Model.TestSuiteId);

        loadTestStepResultsPartialView(testSuiteExecutionId, testSuiteId, 1, "preTestSteps");
        loadTestStepResultsPartialView(testSuiteExecutionId, testSuiteId, 0, "mainTestSteps");
        loadTestStepResultsPartialView(testSuiteExecutionId, testSuiteId, 2, "postTestSteps");
    });

    function loadTestStepResultsPartialView( testSuiteExecutionId, testSuiteId, testStepType, divId) {
        $.ajax({
            type: 'POST',
            url: '@Url.Action("DetailsTestStepResults", "TestSuiteExecutions")',
            data: { 'testSuiteExecutionId': testSuiteExecutionId, 'testSuiteId': testSuiteId, 'testStepType': testStepType },
            success: function(data) {
                $("#" + divId).html(data);

            }
        });

在部分视图中,该表具有唯一的ID,可以访问该ID进行追加(视图模型是视图模型的列表,使用的第一个索引是获取对于日志列表而言唯一的数据);

<div id="@collapseStepItemName" class="collapse col-sm-12" role="tabpanel" aria-labelledby="headingOne">
                    <div class="card-body">
                        <table class="table" id="logTable_@Model[0].TestStepId@Model[0].MessageType">
                            <thead>
                            <tr>
                                <th width="5%"></th>
                                <th width="20% !important">Time</th>
                                <th width="75%">Message</th>
                            </tr>
                            </thead>
                            <tbody>
                                @foreach (var logEntry in Model)
                                {
                                    <tr id="tableRow_@logEntry.TestStepId@logEntry.MessageType">
                                        <td><img width="20" height="20" src="~/Content/Images/@HtmlUtilities.GetTestSuiteExecutionIconName(logEntry.LogType)" /></td>
                                        <td><i>@logEntry.TimeStamp</i></td>
                                        <td><i>@Html.Raw(HtmlUtilities.GetHtmlFormattedString(logEntry.Message))</i></td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>

当前的测试代码(出于测试目的,具有硬编码的tableID)如下;

    var tableId = "logTable_" + 44 + "False";

    var newRow = document.getElementById(tableId).insertRow();
    newRow.innerHTML="<td>New row text</td><td>New row 2nd cell</td><td>Please work</td>";

浏览器调试中引发以下错误;

Uncaught TypeError: Cannot read property 'insertRow' of null

部分视图完全渲染后,是否可以执行脚本? 还是这个问题是其他原因而不是由于加载了视图?

我通过在主视图中的表上对其进行测试来确保表附加脚本确实有效,并且按预期工作。

由于您使用的是jQuery ,因此请将以下代码放在document.ready函数中:

$(document).ready(function() {

    // other stuff

    var tableId = "logTable_" + @Model[0].TestStepId + @Model[0].MessageType;
    var row = $('<tr>').append('<td>New row text</td><td>New row 2nd cell</td><td>Please work</td>');

    $('#' + tableId).find('tbody').append(row);
});

如果您坚持使用香草JS添加行,请确保按照以下示例中的说明加载所有DOM对象

document.addEventListener("DOMContentLoaded", function (ev) { 
    var tableId = "logTable_" + @Model[0].TestStepId + @Model[0].MessageType;

    var newRow = document.getElementById(tableId).insertRow();
    newRow.innerHTML="<td>New row text</td><td>New row 2nd cell</td><td>Please work</td>";
}

insertRow后面具有空值的原因是执行添加行脚本时表DOM元素可能未完全加载,因此,当所有必需的DOM元素完成时,行添加脚本应运行。

演示示例: JSFiddle

暂无
暂无

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

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