简体   繁体   English

在 MVC4 上向 DOM 表添加行的问题

[英]Issue adding rows to DOM table on MVC4

I have a "DOM" list in MVC 4 using jQuery to add elements dynamically but at the moment of removing all the elements The table no longer allows me to add more elements, I have tried to use length and comparisons but it does not work.我在 MVC 4 中有一个“DOM”列表,使用 jQuery 动态添加元素,但在删除所有元素时该表不再允许我添加更多元素,我尝试使用长度和比较,但它不起作用。

Am probably missing something obvious but can't see what it is.我可能遗漏了一些明显的东西,但看不到它是什么。 Any ideas?有任何想法吗?

Example例子

在此处输入图像描述

Here is my table code这是我的表格代码

    <div><a href="#" id="addNew"><img src="~/Images/splus.png" alt="Add Product" style="border:0"/></a></div>
        <table id="dataTable" border="0" cellpadding="0" cellspacing="0">
            <thead>
            <tr>
                <th></th>
                <th></th>
               <th>Product</th>
                <th>Quantity</th>
                <th></th>
            </tr>
                </thead>
            <tbody>
            @if (Model != null && Model.Count > 0)
            {
                int j = 0;
                int index = 1;
                foreach (var i in Model)
                {
                   <tr>
                        <td>@index</td>
                        <td>@Html.HiddenFor(a => a[j].SEQ_NO, new { id = "nrow", Value = index })</td>
                        <td>@Html.DropDownList("PRODUCTS", "Select Product")</td>
                        <td>@Html.TextBoxFor(a => a[j].QUANTITY)</td>                 
                        <td>   
                                <a href="#" id="remove" class="remove"><img src="~/Images/sminus.png" alt="Add Product" style="border:0"/></a>    
                        </td>                      
                   </tr>
                    index += 1;
                    j++;
                }
            }
         </tbody>
        </table>

Here is my jQuery Code when i click on Add New这是我点击添加新的 jQuery 代码

            $("#addNew").click(function (e) {
            e.preventDefault();
            var last = $('#dataTable>tbody>tr:last');
            if (last.length > 0) {
                var name = last.children().find('input,select')[0].name;
                var index = Number(name.replace(/[^0-9]/gi, '')) + 1;
                var tr = ('<tr>' + last.html().replace(/[0-9]+__/gi, index + '__') + '</tr>').replace(/\[[0-9]+\]+[.]/gi, '[' + index + '].');
                numberRows($('#dataTable tbody').append(tr));
             }

            });

UPDATE:更新:

Add "REMOVE CODE"添加“删除代码”

            $(document).on('click', '#remove', function (e) {
            e.preventDefault();
            $(this).parent().parent().remove();
            numberRows($('#dataTable tbody'));

        });

Am probably missing something obvious but can't see what it is.我可能遗漏了一些明显的东西,但看不到它是什么。 Any ideas?有任何想法吗?

var name = last.children().find('input,select')[0].name;

This line of code requires one last child, which means after you removed the last under, the name will be undefined.这行代码需要最后一个孩子,这意味着在您删除最后一个孩子之后, name将是未定义的。 In fact, the Add New function won't even pass the if (last.length > 0) since when the last got removed, the last.length becomes 0.事实上, Add New function 甚至不会通过if (last.length > 0) ,因为当 last 被删除时, last.length变为 0。

You can debug in devtools, and will see:你可以在 devtools 中调试,会看到:

BEFORE the last got removed:在最后一个被删除之前:

在此处输入图像描述

AFTER the last got removed:最后一个被删除后:

在此处输入图像描述

Thus, to fix this issue, the simplest solution is to not remove the last but hide it.因此,要解决此问题,最简单的解决方案是不删除最后一个,而是隐藏它。 Please check below code:请检查以下代码:

    $(document).on('click', '#remove', function (e) {
        e.preventDefault();
        if ($('#dataTable>tbody>tr').length > 1) {
            $(this).parent().parent().remove();
            //numberRows($('#dataTable tbody'));
        }
        else {
            $(this).parent().parent().hide();
        }
    });

Ps.附言。 Not sure what happens in numberRows() function so I commented it.不确定 numberRows() function 会发生什么,所以我评论了它。

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

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