JavaScript Table.Rowd Collection Undefined

时间:2017-05-02 15:53:24

标签: javascript

我试图通过克隆行并从模型中替换它的值来将行插入表中。我可以通过其他方式做到这一点。我是JavaScript的新手,这段代码来自我发现的一个例子。

function createNewRow(model)
{
    // Make a copy of the first row
    var table = document.getElementById('divData');
    var new_row = table.rows[1].cloneNode(true);        // <======  Error occurs here. The Table exists, the rows collection does not.
    var len = table.rows.length;
    new_row.cells[0].innerHTML = len;

    // Copy the model's data to the new row
    new_row.cells[1].value = model.SiteId;
    new_row.cells[2].value = model.InstrumentId;
    new_row.cells[3].value = model.TowerLocation;
    new_row.cells[4].value = model.BayLocation;
    new_row.cells[5].value = model.BaySerialNo;
    new_row.cells[6].value = model.BayStatus;
    new_row.cells[7].value = model.AccessionId;
    new_row.cells[8].value = model.Result;
    new_row.cells[9].value = model.AssayName;
    new_row.cells[10].value = model.Started;
    new_row.cells[11].value = model.Completed;
    new_row.cells[12].value = model.TestSummary;

    // Add the new row to the table
    table.appendChild(new_row);
}

[更新] - 添加了表格代码

@using (@Html.BeginForm("Index", "Home"))
{
    <div id="divData">
        @{
            var grid = new WebGrid(Model, rowsPerPage: 10, ajaxUpdateContainerId: "divData", canPage: true);

            @grid.GetHtml(
            tableStyle: "webgrid-table",
            headerStyle: "webgrid-header",
            footerStyle: "webgrid-footer",
            alternatingRowStyle: "webgrid-alternating-row",
            selectedRowStyle: "webgrid-selected-row",
            rowStyle: "webgrid-row-style",
            mode: WebGridPagerModes.All,
            columns:
            grid.Columns
            (
                grid.Column(columnName: "RowId", header: "Row Id", format: @<text><span data-row-id="@item.RowId">@item.RowId</span></text>),
                grid.Column(columnName: "SiteId", header: "Site Id", format: @<text><span class="x-SiteId">@item.SiteId</span></text>),
            ))
        }
    </div>
}

表格确实存在,表格中有行,但在我上面指出的行上我得到了#34;无法读取属性&#39; 1&#39;未定义&#34;

当我console.log(table.Rows)时,我得到了“未定义的”#。

任何人都明白了什么?

谢谢

0 个答案:

没有答案