【问题标题】:Accessing Table in Partial View在局部视图中访问表
【发布时间】:2018-08-01 00:47:30
【问题描述】:

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

我正在通过页面加载和 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,可以访问以追加(视图模型是视图模型的列表,使用第一个索引是获取日志列表唯一的数据);

<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

有没有办法在部分视图完全渲染后执行脚本?或者这个问题是其他问题而不是由于正在加载的视图?

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

【问题讨论】:

  • JavaScript `var tableId = "logTable_" + 44 + "False"; var newRow = document.getElementById(tableId).insertRow(); newRow.innerHTML="新行文本 新行第二单元格 请工作" 必须写在成功函数里面
  • 由于空错误的属性____仍然失败

标签: javascript jquery ajax asp.net-mvc partial-views


【解决方案1】:

由于您使用的是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);
});

如果你坚持使用 vanilla JS 添加行,请确保 all DOM objects are already loaded 如下例所示:

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 具有 null 值的原因是执行添加行脚本时可能未完全加载表格 DOM 元素,因此应在所有必需的 DOM 元素完成后运行行添加脚本。

演示示例:JSFiddle

【讨论】:

  • 我假设 $(document).ready(function() {} 写在局部视图的脚本部分?当写在那里时,我得到一个错误;加载资源失败:服务器响应 500
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多