【问题标题】:Dynamically add rows using jquery or javascript with contents from Model on client side使用 jquery 或 javascript 动态添加行,其中包含来自客户端模型的内容
【发布时间】:2015-10-05 06:49:30
【问题描述】:

我从我的 cshtml 文件中截取了以下代码

<div id="trows" class="table-responsive">
        <table class="table table-bordered" id="myTable">
</div>

并在 on add 按钮事件中添加一行我尝试如下:-

        var itemRSelected = $("#SelectedteRId option:selected").text();
        var itemNSelected = $("#SelectedName option:selected").text();

        debugger;
        //all ok till above. I verified in debuggger
        //but I am not able to add these values along with the Model rows to the table using below code. Any help? clue to fix the issue?
        @{var i = 0;}
        @foreach (MyNameSpace.Models.MyClassObj p in Model.myClassList)
        {
           if(itemRSelected == p.RowId && itemNSelected == p.NamePix) {
                          var row = "<tr><td>" + @(p.RowId) + "</td><td>" + @(p.NamePix) +
                "</td><td>" + @(p.Mi) + "</td><td>" + @(p.Name) +
                "</td><td>" + @(p.ExtraDate) +
                "</td><td><a class='btn btn-danger btn-xs btnDelete' title='Delete'><i class='fa fa-trash-o'></i></a></td><tr>" ; }

                @: <text>@row</text>
                @:$("#myTable").eq(@i++).after(@row);

           }

【问题讨论】:

  • 服务器端代码呈现逻辑时,您不能使用客户端 JavaScript 变量itemNSelected,即@ 987654326@。为什么使用jQuery Ajax 不能实现?
  • 建议您查看答案herehere 以将集合对象动态添加到视图中
  • @Satpal,我试图避免服务器跳闸,因为我希望我可以在第一次查看旅行本身时在客户端拥有所有模型数据。
  • 斯蒂芬,感谢您的建议,但我已经准备好与您提供的链接类似的逻辑。但它仍然无法正常工作。我不是从头开始问怎么做,但我写了一些东西并想知道错误是什么。如果专家看起来他们可能会立即发现错误并提出正确的语法/逻辑。就像上面的@Satpal 建议一样,但我正在尝试调用另一个 ajax 调用,因为我认为客户端在第一次查看视图时是与客户端一起调用的。
  • 你的逻辑甚至与我给你的链接都不远。我建议你再研究一下

标签: jquery asp.net-mvc model-view-controller


【解决方案1】:

要使用这些功能,您需要创建一个 HTML 表格并为其指定一个唯一 ID。示例代码使用 tblGrid 作为 ID,但您可以使用任何其他名称 - 只需确保修改函数以使用新名称。接下来,您需要修改 addRow 函数以确保添加正确数量的单元格并正确设置它们的 innerHTML。

 //add a new row to the table
function addRow()
{
    //add a row to the rows collection and get a reference to the newly added row
    var newRow = document.all("tblGrid").insertRow();

    //add 3 cells (<td>) to the new row and set the innerHTML to contain text boxes

    var oCell = newRow.insertCell();
    oCell.innerHTML = "<input type='text' name='t1'>";

    oCell = newRow.insertCell();
    oCell.innerHTML = "<input type='text' name='t2'>";

    oCell = newRow.insertCell();
    oCell.innerHTML = "<input type='text' name='t3'>&nbsp;&nbsp;<input" + 
                      " type='button' value='Delete' onclick='removeRow(this);'/>";   
}

//deletes the specified row from the table
function removeRow(src)
{
    /* src refers to the input button that was clicked. 
       to get a reference to the containing <tr> element,
       get the parent of the parent (in this case <tr>)
    */   
    var oRow = src.parentElement.parentElement;  

    //once the row reference is obtained, delete it passing in its rowIndex   
    document.all("tblGrid").deleteRow(oRow.rowIndex);  

}

可以参考这个链接Dynamically add and remove rows in an HTML table

【讨论】:

  • @Mohamed,感谢它适用于表格,但我需要调用 ajax 来取回行以添加到表格中。但是您的回答帮助我解决了这个问题,因此接受了您的回答。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 1970-01-01
相关资源
最近更新 更多