简体   繁体   English

如何在表单内向 jquery 数据表添加行?

[英]How to add rows to jquery datatable inside a form?

I hope everyone had good holidays.我希望每个人都度过了愉快的假期。

I need to add rows to my datatable inside a modal window(partial view).我需要在模态窗口(部分视图)内向我的数据表添加行。 This modal window basically has a form for various fields and a datatable, together with two inputs and a button.这个模态 window 基本上有一个用于各种字段的表单和一个数据表,以及两个输入和一个按钮。

My idea was to fill the two input fields and when the button is pressed a new row is created in the table, with the values inserted in the two input fields.我的想法是填充两个输入字段,当按下按钮时,会在表中创建一个新行,并将值插入两个输入字段中。 But for some reason, when I click the button the values in the input fields are empty.但是由于某种原因,当我单击按钮时,输入字段中的值是空的。 The information on the table would very likely be stringified and sent, for example, to the field asp-for="test".表上的信息很可能会被字符串化并发送到例如字段 asp-for="test"。 So far this was what I thought.到目前为止,这就是我的想法。

Is this logic possible within a partial view?这种逻辑在局部视图中是否可行? Do I need an additional form to receive data from my two inputs?我需要额外的表格来接收来自我的两个输入的数据吗? I'm already inside another form and I know HTML5 doesn't allow for form nesting, but I get the nagging feeling that I would need an additional form for this logic.我已经在另一个表单中,我知道 HTML5 不允许表单嵌套,但我觉得我需要一个额外的表单来实现这个逻辑。 Here is the code:这是代码:

(js) (js)

$(document).ready(function () {
    $('#AddProdBtn').click(function () {
        $('#returnLinesGrid').dataTable().fnAddData( [
    $('#prodId').val(), $('#prodQty').val()] );
    });
});

(cshtml) (cshtml)

 <div id="return-edition-modal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content white-box white-box--lblreset">
            <div class="modal-header">
                <h5 class="modal-title create" style="display:none;">@TitleResource.ReturnCreate</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form asp-controller="Returns" asp-action="Create" method="post" autocomplete="off">
                <div asp-validation-summary="ModelOnly"></div>
                <a hidden id="create-url" asp-action="Create" asp-controller="Returns"></a>
                <input type="hidden" value="@Guid.NewGuid()" asp-for="Id" />
                <div class="modal-body row">
                    <div class="md-form col-6 create-only row">
                        <select asp-for="ReturnType" asp-items="Html.GetEnumSelectList<OrderReturnTypeEnum>()" required class="form-control" selectpicker>
                            <option value="">@LabelResource.SelectReturnType</option>
                        </select>
                        <span asp-validation-for="ReturnType"></span>
                    </div>

                    <div class="md-form col-6 create-only" id="ReturnMotivePlaceholder" style="margin-top: 6px">
                        <select disabled selectpicker>
                            <option value="">@LabelResource.SelectReturnMotive</option>
                        </select>
                    </div>

                    <div class="md-form col-6 create-only" data-dependency="@((int)OrderReturnTypeEnum.Recall)" style="margin-top:6px">
                        <select asp-for="ReturnMotive" asp-items="Html.GetEnumSelectList<OrderReturnMotiveEnumRecall>()" required class="form-control" selectpicker>
                            <option value="">@LabelResource.SelectReturnMotive</option>
                        </select>
                        <span asp-validation-for="ReturnMotive"></span>
                    </div>
                    <div class="md-form col-6 create-only" data-dependency="@((int)OrderReturnTypeEnum.Daily)" style="margin-top:6px">
                        <select asp-for="ReturnMotiveAux" asp-items="Html.GetEnumSelectList<OrderReturnMotiveEnumDaily>()" required class="form-control" selectpicker>
                            <option value="">@LabelResource.SelectReturnMotive</option>
                        </select>
                        <span asp-validation-for="ReturnMotiveAux"></span>
                    </div>
                </div>
                <div class="modal-body row">
                    <div class="md-form col-6 create-only" data-dependency="@((int)OrderReturnTypeEnum.Daily)">
                        <input type="number" value="" asp-for="InvoiceNumber" class="form-control" required placeholder="@LabelResource.SelectReturnInvoiceNumber" />
                    </div>
                    <div class="md-form col-6 create-only">
                        <input type="text" value="" asp-for="ClientReturnNumber" placeholder="@LabelResource.SelectClientReturnNumber" class="form-control" />
                    </div>
                </div>

                <div class="md-form col-3 create-only">
                    <input type="number" value="" id="prodId" placeholder="@LabelResource.SelectProdId" />
                </div>
                <div class="md-form col-3 create-only">
                    <input type="text" value="" id="prodQty" placeholder="@LabelResource.SelectProdQuantity" />
                </div>
                <div class="col-6>
                    <button type="button" id="AddProdBtn">@ButtonResource.AddProduct</button>
                </div>

                <div class="row">
                    <div class="col-12">
                        <div class="row">
                            <table class="dataTable" id="returnLinesGrid" asp-for="ReturnLines">
                                <thead>
                                    <tr>
                                        <th>@Html.Raw(LabelResource.ProductId)</th>
                                        <th>@Html.Raw(LabelResource.Quantity)</th>
                                    </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </div>

                <div class="row modal-footer message-footer pr-5">
                    <div class=" mr-md-0">
                        <a data-dismiss="modal" class="btn btn-danger w-100">@ButtonResource.Cancel</a>
                    </div>
                    <div class=" ml-3">
                        <button type="submit" class="btn btn-primary w-100">@ButtonResource.Save</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

But the value of "prodId" and "prodQty" is always empty when I click the "AddProdBtn" button.但是当我单击“AddProdBtn”按钮时,“prodId”和“prodQty”的值始终为空。 Almost as if it never.几乎就像从来没有一样。 I have no idea what to do.. Thank you for any and all help.我不知道该怎么办..感谢您的任何帮助。

Pictures图片

Modal window模态 window

Main window, modal is called by pressing "Nova devolução" button主 window,按“Nova devolução”按钮调用模态

You should add the js to the main view.您应该将 js 添加到主视图。 Below is a working demo:下面是一个工作演示:

Main View:主视图:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#return-edition-modal">
    Create
</button>

@await Html.PartialAsync("_ProductPartial")

@section scripts{
    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.min.css" />
    <script>
        $(document).ready(function () {
            var datatable = $('#returnLinesGrid').dataTable();
            $('#AddProdBtn').click(function () {
                datatable.fnAddData([
                    $('#prodId').val(), $('#prodQty').val()]);
            });
        });
    </script>
}

Partial View:部分视图:

<div id="return-edition-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Create</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form asp-controller="Returns" asp-action="Create" method="post" autocomplete="off">
                    <div asp-validation-summary="ModelOnly"></div>

                    <div class="md-form col-3 create-only">
                        <input type="number" value="" id="prodId" placeholder="SelectProdId" />
                    </div>
                    <div class="md-form col-3 create-only">
                        <input type="text" value="" id="prodQty" placeholder="SelectProdQuantity" />
                    </div>
                    <div class="col-6">
                        <button type="button" id="AddProdBtn">AddProduct</button>
                    </div>

                    <table id="returnLinesGrid">
                        <thead>
                            <tr>
                                <th>@Html.Raw("ProductId")</th>
                                <th>@Html.Raw("Quantity")</th>
                            </tr>
                        </thead>
                    </table>

                    <div class="modal-footer">
                        <div class="mr-md-0">
                            <a data-dismiss="modal" class="btn btn-danger">Cancel</a>
                        </div>
                        <div class=" ml-3">
                            <button type="submit" class="btn btn-primary">Save</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

Result:结果:

在此处输入图像描述

I finally got it, There is a typescript part of the code, which I had no idea would have a completely different behaviour than the javascript code, in the modal window.我终于明白了,代码中有一个 typescript 部分,我不知道在模态 window 中它的行为与 javascript 代码完全不同。 for some reason.由于某些原因。 I basically just had to put my jquery code in the.ts file associated(in this case Return.Index,ts) and then it finally recognized the values in my inputs, Thank you so much, everyone, and early happy new year!我基本上只需将我的 jquery 代码放入关联的 .ts 文件中(在本例中为 Return.Index,ts),然后它终于识别了我输入的值,非常感谢大家,新年快乐!

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

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