jQuery中循环生成的Kendo DropDownListFor无法正确初始化 [英] Loop-generating Kendo DropDownListFor in jQuery doesn't initialize properly

查看:115
本文介绍了jQuery中循环生成的Kendo DropDownListFor无法正确初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为ViewModel创建一个编辑器模板,我发现Kendo控件在生成后会遇到问题.

I'm trying to create an Editor Template for a ViewModel and I see that Kendo controls are facing issues after being generated.

这是ParkingServiceDetail.cshtml文件:

@model List<DA.Services.CarPark.Presentation.Web.Models.ParkingServiceDetailViewModel>

<div id="@ViewData.TemplateInfo.HtmlFieldPrefix" style="margin-top:10px">
    <table class="table table-bordered table-striped table-hover" id="grid">
        <thead>
            <tr>
                <th data-field="TerminalId">Terminal</th>
                <th data-field="ServiceId">Service</th>
                <th data-field="ParkingCardNumber">Card Number</th>
                <th data-field="ParkingCardIssueDate">Issue Date</th>
                <th data-field="ParkingCardExpiryDate">Expiry Date</th>
                <th data-field="ParkingCardGroup">Card Group</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            @if (Model != null && Model.Any())
            {
                for (int i = 0; i < Model.Count; i++)
                {
                    <tr mjrole="@(Model[i].ParkingCardNumber == "viewtemplate" ? "template" : "row")" style="display:@(Model[i].ParkingCardNumber == "viewtemplate" ? "none" : "")">
                        <td>@Html.DropDownListFor(m => m[i].TerminalId, (IEnumerable<SelectListItem>)ViewBag.Terminals, "Select terminal", new { @class = "form-control", style = "width:150px", mjrole = ViewData.TemplateInfo.HtmlFieldPrefix + "-terminal" })</td>
                        <td>@Html.DropDownListFor(m => m[i].ServiceId, (IEnumerable<SelectListItem>)ViewBag.Services, "Select service", new { @class = "form-control", style = "width:150px", mjrole = ViewData.TemplateInfo.HtmlFieldPrefix + "-service" })</td>
                        <td>@Html.TextBoxFor(m => m[i].ParkingCardNumber,     new { @class = "k-textbox form-control" })</td>
                        <td>@Html.TextBoxFor(m => m[i].ParkingCardIssueDate,  new { @class = "form-control", style = "width:115px", mjrole = "dateIs" })</td>
                        <td>@Html.TextBoxFor(m => m[i].ParkingCardExpiryDate, new { @class = "form-control", style = "width:115px", mjrole = "dateEx" })</td>
                        <td>@Html.TextBoxFor(m => m[i].ParkingCardGroup,      new { @class = "k-textbox form-control", style = "width:100px" })</td>
                        <td>
                            <button command="@ViewData.TemplateInfo.HtmlFieldPrefix-delete" id="DeptFlightSearch" type="button" class="btn btn-danger @(Model[i].TerminalId > 0 ? "disabled" : "")">
                                <span class="glyphicon glyphicon-remove-circle"></span>
                                Delete
                            </button>
                        </td>

                        <script type="text/javascript">

                            var tempParent = $("select[mjrole='@ViewData.TemplateInfo.HtmlFieldPrefix-terminal']")
                                .kendoDropDownList({
                                    optionLabel: "Select terminal...",
                                    dataTextField: "TerminalName",
                                    dataValueField: "TerminalId",
                                    dataSource: {
                                        serverFiltering: true,
                                        transport: {
                                            read: {
                                                url: "Ajax/GetTerminals",
                                                dataType: "json"
                                            }
                                        }
                                    }
                                });

                            $("select[mjrole='@ViewData.TemplateInfo.HtmlFieldPrefix-service']")
                                .kendoDropDownList({
                                    autoBind: false,
                                    cascadeFrom: tempParent.id,
                                    optionLabel: "Select service...",
                                    dataTextField: "NameEnglish",
                                    dataValueField: "Code",
                                    dataSource: {
                                        serverFiltering: true,
                                        transport: {
                                            read: {
                                                url: "Ajax/GetTerminalServices",
                                                data: "filterServices",
                                                dataType: "json"
                                            }
                                        }
                                    }
                                });

                        </script>
                    </tr>
                }
            }

            <tr id="@ViewData.TemplateInfo.HtmlFieldPrefix-empty" style="display: @(Model != null && Model[0].ParkingCardNumber == "viewtemplate" && Model.Count > 1 ? "none": "")">
                <td colspan="7">
                    No services added
                </td>
            </tr>

            <tr mjrole="footer">
                <td colspan="7" style="text-align: right;">
                    <button id="@ViewData.TemplateInfo.HtmlFieldPrefix-AddNew" type="button" class="btn btn-outline btn-success btn-circle btn-lg">
                        <span class="fa fa-plus"></span>
                    </button>
                </td>
            </tr>

        </tbody>
    </table>
</div>

<div class="modal fade" id="@ViewData.TemplateInfo.HtmlFieldPrefix-popup" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="dialog" style="width:700px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h3 class="modal-title" id="myModalLabel">Confirmation</h3>
            </div>
            <div class="modal-body">
                <h5> Are you sure you want to delete the selected service? </h5>
                <table class="table table-bordered table-striped" id="deletegrid"></table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline btn-danger btn-circle btn-lg" data-dismiss="modal">
                    <span class="glyphicon glyphicon-remove"></span>
                </button>
                <button id="@ViewData.TemplateInfo.HtmlFieldPrefix-ok" type="button" class="btn btn-outline btn-success btn-circle btn-lg">
                    <span class="glyphicon glyphicon-ok"></span>
                </button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">

    $("#@ViewData.TemplateInfo.HtmlFieldPrefix").on("click", "button[command='@ViewData.TemplateInfo.HtmlFieldPrefix-delete']", function (arg) {
        var row = $(arg.target).parent().closest("tr");
        $('#@ViewData.TemplateInfo.HtmlFieldPrefix').prop("target", row);
        $('#@ViewData.TemplateInfo.HtmlFieldPrefix-popup').modal('show');
    });

    $("#@ViewData.TemplateInfo.HtmlFieldPrefix-ok").click(function () {
        var row = $('#@ViewData.TemplateInfo.HtmlFieldPrefix').prop("target");

        row.remove();

        $("#@ViewData.TemplateInfo.HtmlFieldPrefix").find("tr[mjrole='row']").each(function (index) {
            var tempControl;

            tempControl = $(this).find("[id$='TerminalId']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__TerminalId");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].TerminalId");

            tempControl = $(this).find("[id$='ServiceId']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ServiceId");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ServiceId");

            tempControl = $(this).find("[id$='ParkingCardNumber']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardNumber");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardNumber");

            tempControl = $(this).find("[id$='ParkingCardIssueDate']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardIssueDate");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardIssueDate");

            tempControl = $(this).find("[id$='ParkingCardExpiryDate']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardExpiryDate");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardExpiryDate");

            tempControl = $(this).find("[id$='ParkingCardGroup']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardGroup");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardGroup");
        });

        $('#@ViewData.TemplateInfo.HtmlFieldPrefix-popup').modal('hide');

        if ($("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='row']").length > 0) {
            $("#@ViewData.TemplateInfo.HtmlFieldPrefix-empty").hide();
        }
        else {
            $("#@ViewData.TemplateInfo.HtmlFieldPrefix-empty").show();
        }
    });

    $("#@ViewData.TemplateInfo.HtmlFieldPrefix-AddNew").click(function () {
        var row;

        if ($("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='template']").length > 0) {
            row = $("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='template']").clone();
        }
        else {
            row = $("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='row']").first().clone();
        }

        row.attr("mjrole", "row").show();
        row.find("input[name$='ParkingCardNumber']").val("");
        row.insertAfter($("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr").not($("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='footer']")).last());

        if ($("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='row']").length > 0) {
            $("#@ViewData.TemplateInfo.HtmlFieldPrefix-empty").hide();
        }
        else {
            $("#@ViewData.TemplateInfo.HtmlFieldPrefix-empty").show();
        }

        $("#@ViewData.TemplateInfo.HtmlFieldPrefix").find("tr[mjrole='row']").each(function (index) {
            var tempControl;

            tempControl = $(this).find("[id$='TerminalId']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__TerminalId");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].TerminalId");

            tempControl = $(this).find("[id$='ServiceId']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ServiceId");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ServiceId");

            tempControl = $(this).find("[id$='ParkingCardNumber']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardNumber");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardNumber");

            tempControl = $(this).find("[id$='ParkingCardIssueDate']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardIssueDate");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardIssueDate");

            tempControl = $(this).find("[id$='ParkingCardExpiryDate']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardExpiryDate");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardExpiryDate");

            tempControl = $(this).find("[id$='ParkingCardGroup']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardGroup");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardGroup");
        });

        $("input[mjrole='dateIs']").kendoDatePicker({ culture: "ar-AE" });
        $("input[mjrole='dateEx']").kendoDatePicker({ culture: "ar-AE" });

    });

    function filterServices() {
        return {
            companyId: '@ViewData["CompanyId"]',
            terminalId: $("#TerminalId").val()
        };
    }

</script>

这似乎令人生畏,但这仅是由于列表排序的解决方法.这是非常基本的东西.

It seem intimidating but only because of list ordering workaround. This is pretty basic stuff.

我最初使用的是Html.Helpers控件,它们工作正常.但是,我需要在每行中管理一些级联的下拉菜单.这就是为什么您在生成后立即看到script标签的原因.

I was originally using the Html.Helpers controls and they work OK. However, I need to manage a few cascading drop-downs in each row. And that's why you see the script tag right after the generation.

问题是所有Kendo控件的小部件初始化均失败.我通过在JS中的添加按钮"代码之后启动它们来解决了DatePicker.但是他们并不是真正的依赖者,所以很容易.但是,对于两个下拉菜单,我不能做同样的事情.

Issue is that the widget initiation was failing for all Kendo controls. I solved the DatePicker by initiating them after the Add Button code in JS. But they are not really dependent so that was easy. I cannot do the same for the two drop-downs though.

当前,在运行时,这两个下拉菜单都转换为Kendo,处于一种禁用状态,不接受任何输入或通过浏览器调试器响应任何属性更改.

Currently, at runtime, the two drop-downs get converted to Kendo and are in a sort of disabled state and do not accept any inputs or respond to any attribute changes through browser debuggers.

并且我已经确认,如果将JS的一部分从tr生成移到添加"按钮单击之后,它可以工作,但是我丢失了需要创建级联依赖项的ID.

And I have confirmed that if I move the bit of JS from tr generation to after Add button click, it works but I lose ids that I need create cascading dependencies.

这里是否存在一些线程时序问题,这些问题在剑道而不是常规控件的情况下失败?

Is there some threading timing issues here that fail in case of Kendo but not the regular controls?

更新

我确定的主要问题是Kendo控件的名称不是唯一的.我下面的答案中有新代码.这样可以解决50%的问题.

Main issue I identified was the names of the Kendo controls were not unique. New code in my answer below. This resolves 50% of the question.

推荐答案

我能够通过正确命名它们来解决被屏蔽的kendo控件.现在,对于现有记录(如果我在List<>中发送一些记录),则使用适当的kendo控件可以正确显示行.但是,对于新插入的行,同样的问题又回来了,因为我还没有弄清楚在这种情况下如何重命名.

I was able to solve the blocked out kendo controls by naming them properly. Now for the existing records (if I send a few in the List<>), the rows render correctly with proper kendo controls. However, for new inserted rows, the same issue comes back as I have yet to figure out how to rename in that scenario.

此外,级联组合也不起作用.

Also, the cascading combos are not working either.

这是工作代码:

@model List<DA.Services.CarPark.Presentation.Web.Models.ParkingServiceDetailViewModel>

<div id="@ViewData.TemplateInfo.HtmlFieldPrefix" style="margin-top:10px">
    <table class="table table-bordered table-striped table-hover" id="grid">
        <thead>
            <tr>
                <th data-field="TerminalId">Terminal</th>
                <th data-field="ServiceId">Service</th>
                <th data-field="ParkingCardNumber">Card Number</th>
                <th data-field="ParkingCardIssueDate">Issue Date</th>
                <th data-field="ParkingCardExpiryDate">Expiry Date</th>
                <th data-field="ParkingCardGroup">Card Group</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            @if (Model != null && Model.Any())
            {
                for (var i = 0; i < Model.Count; i++)
                {
                    <tr mjrole="@(Model[i].ParkingCardNumber == "viewtemplate" ? "template" : "row")" style="display: @(Model[i].ParkingCardNumber == "viewtemplate" ? "none" : "")">
                        <td>@(Html.Kendo().DropDownListFor(m => m[i].TerminalId)
                            .Name(ViewData.TemplateInfo.HtmlFieldPrefix + "[" + (i + 1) + "].TerminalId")
                            .OptionLabel("Select terminal...")
                            .DataTextField("TerminalName")
                            .DataValueField("TerminalId")
                            .DataSource(source => source
                                .Read(read => read
                                    .Action("GetTerminals", "Ajax")
                                    .Type(HttpVerbs.Post)
                                )
                            )
                            .HtmlAttributes(new {
                                @class = "form-control", style = "width:150px",
                                mjrole = ViewData.TemplateInfo.HtmlFieldPrefix + "-terminal"
                            })
                            )
                        </td>
                        <td>@(Html.Kendo().DropDownListFor(m => m[i].ServiceId)
                            .Name(ViewData.TemplateInfo.HtmlFieldPrefix + "[" + (i + 1) + "].ServiceId")
                            .OptionLabel("Select service...")
                            .DataTextField("NameEnglish")
                            .DataValueField("Code")
                            .AutoBind(false)
                            .CascadeFrom(ViewData.TemplateInfo.HtmlFieldPrefix + "_" + (i + 1) + "__TerminalId")
                            .DataSource(source => source
                                .Read(read => read
                                    .Action("GetTerminalServices", "Ajax")
                                    .Type(HttpVerbs.Post)
                                )
                            )
                            .HtmlAttributes(new {
                                 @class = "form-control", style = "width:150px",
                                 mjrole = ViewData.TemplateInfo.HtmlFieldPrefix + "-service"
                            })
                            )
                        </td>
                        <td>@(Html.Kendo().TextBoxFor(   m => m[i].ParkingCardNumber)    .Name(ViewData.TemplateInfo.HtmlFieldPrefix + "[" + (i + 1) + "].ParkingCardNumber")    .HtmlAttributes(new { @class = "form-control" }))</td>
                        <td>@(Html.Kendo().DatePickerFor(m => m[i].ParkingCardIssueDate) .Name(ViewData.TemplateInfo.HtmlFieldPrefix + "[" + (i + 1) + "].ParkingCardIssueDate") .Culture("ar-AE").HtmlAttributes(new { @class = "form-control", style = "width:115px", mjrole = "dateIs" }))</td>
                        <td>@(Html.Kendo().DatePickerFor(m => m[i].ParkingCardExpiryDate).Name(ViewData.TemplateInfo.HtmlFieldPrefix + "[" + (i + 1) + "].ParkingCardExpiryDate").Culture("ar-AE").HtmlAttributes(new { @class = "form-control", style = "width:115px", mjrole = "dateEx" }))</td>
                        <td>@(Html.Kendo().TextBoxFor(   m => m[i].ParkingCardGroup)     .Name(ViewData.TemplateInfo.HtmlFieldPrefix + "[" + (i + 1) + "].ParkingCardGroup")     .HtmlAttributes(new { @class = "form-control", style = "width: 100px" }))</td>
                        <td>
                            <button command="@ViewData.TemplateInfo.HtmlFieldPrefix-delete" id="DeptFlightSearch" type="button" class="btn btn-danger @(Model[i].TerminalId > 0 ? "disabled" : "")">
                                <span class="glyphicon glyphicon-remove-circle"></span>
                                Delete
                            </button>
                        </td>

                        <script>
                            var tempDropdown = $('#@ViewData.TemplateInfo.HtmlFieldPrefix' + '_@(i + 1)__TerminalId').data("kendoDropDownList");
                            $("#" + @ViewData.TemplateInfo.HtmlFieldPrefix + "_" + (i + 1) + "__ServiceId").data("kendoDropDownList").dataSource().read().transport().data(function(){return {companyId : @ViewBag.CompanyId, terminalId : tempDropdown.value() }} );
                        </script>
                    </tr>
                }
            }

            <tr id="@ViewData.TemplateInfo.HtmlFieldPrefix-empty" style="display: @(Model != null && Model[0].ParkingCardNumber == "viewtemplate" && Model.Count > 1 ? "none": "")">
                <td colspan="7">
                    No services added
                </td>
            </tr>

            <tr mjrole="footer">
                <td colspan="7" style="text-align: right;">
                    <button id="@ViewData.TemplateInfo.HtmlFieldPrefix-AddNew" type="button" class="btn btn-outline btn-success btn-circle btn-lg">
                        <span class="fa fa-plus"></span>
                    </button>
                </td>
            </tr>

        </tbody>
    </table>
</div>

<div class="modal fade" id="@ViewData.TemplateInfo.HtmlFieldPrefix-popup" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="dialog" style="width:700px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h3 class="modal-title" id="myModalLabel">Confirmation</h3>
            </div>
            <div class="modal-body">
                <h5> Are you sure you want to delete the selected service? </h5>
                <table class="table table-bordered table-striped" id="deletegrid"></table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline btn-danger btn-circle btn-lg" data-dismiss="modal">
                    <span class="glyphicon glyphicon-remove"></span>
                </button>
                <button id="@ViewData.TemplateInfo.HtmlFieldPrefix-ok" type="button" class="btn btn-outline btn-success btn-circle btn-lg">
                    <span class="glyphicon glyphicon-ok"></span>
                </button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">

    $("#@ViewData.TemplateInfo.HtmlFieldPrefix").on("click", "button[command='@ViewData.TemplateInfo.HtmlFieldPrefix-delete']", function (arg) {
        var row = $(arg.target).parent().closest("tr");
        $('#@ViewData.TemplateInfo.HtmlFieldPrefix').prop("target", row);
        $('#@ViewData.TemplateInfo.HtmlFieldPrefix-popup').modal('show');
    });

    $("#@ViewData.TemplateInfo.HtmlFieldPrefix-ok").click(function () {
        var row = $('#@ViewData.TemplateInfo.HtmlFieldPrefix').prop("target");

        row.remove();

        $("#@ViewData.TemplateInfo.HtmlFieldPrefix").find("tr[mjrole='row']").each(function (index) {
            var tempControl;

            tempControl = $(this).find("[id$='TerminalId']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__TerminalId");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].TerminalId");

            tempControl = $(this).find("[id$='ServiceId']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ServiceId");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ServiceId");

            tempControl = $(this).find("[id$='ParkingCardNumber']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardNumber");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardNumber");

            tempControl = $(this).find("[id$='ParkingCardIssueDate']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardIssueDate");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardIssueDate");

            tempControl = $(this).find("[id$='ParkingCardExpiryDate']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardExpiryDate");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardExpiryDate");

            tempControl = $(this).find("[id$='ParkingCardGroup']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardGroup");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardGroup");
        });

        $('#@ViewData.TemplateInfo.HtmlFieldPrefix-popup').modal('hide');

        if ($("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='row']").length > 0) {
            $("#@ViewData.TemplateInfo.HtmlFieldPrefix-empty").hide();
        }
        else {
            $("#@ViewData.TemplateInfo.HtmlFieldPrefix-empty").show();
        }
    });

    $("#@ViewData.TemplateInfo.HtmlFieldPrefix-AddNew").click(function () {
        var row;

        if ($("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='template']").length > 0) {
            row = $("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='template']").clone();
        }
        else {
            row = $("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='row']").first().clone();
        }

        row.attr("mjrole", "row").show();
        row.find("input[name$='ParkingCardNumber']").val("");
        row.insertAfter($("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr").not($("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='footer']")).last());

        if ($("#@ViewData.TemplateInfo.HtmlFieldPrefix table tr[mjrole='row']").length > 0) {
            $("#@ViewData.TemplateInfo.HtmlFieldPrefix-empty").hide();
        }
        else {
            $("#@ViewData.TemplateInfo.HtmlFieldPrefix-empty").show();
        }

        $("#@ViewData.TemplateInfo.HtmlFieldPrefix").find("tr[mjrole='row']").each(function (index) {
            var tempControl;

            tempControl = $(this).find("[id$='TerminalId']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__TerminalId");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].TerminalId");

            tempControl = $(this).find("[id$='ServiceId']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ServiceId");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ServiceId");

            tempControl = $(this).find("[id$='ParkingCardNumber']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardNumber");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardNumber");

            tempControl = $(this).find("[id$='ParkingCardIssueDate']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardIssueDate");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardIssueDate");

            tempControl = $(this).find("[id$='ParkingCardExpiryDate']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardExpiryDate");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardExpiryDate");

            tempControl = $(this).find("[id$='ParkingCardGroup']");
            tempControl.attr("id",   "@(ViewData.TemplateInfo.HtmlFieldPrefix)_" + (index + 1) + "__ParkingCardGroup");
            tempControl.attr("name", "@(ViewData.TemplateInfo.HtmlFieldPrefix)[" + (index + 1) + "].ParkingCardGroup");
        });
    });

    function filterServices() {
        return {
            companyId: '@ViewData["CompanyId"]',
            terminalId: $("#TerminalId").val()
        };
    }

</script>

这篇关于jQuery中循环生成的Kendo DropDownListFor无法正确初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆