简体   繁体   English

编辑事件时网格中的Kendo UI Web下拉列表

[英]Kendo UI web drop down list in grid on edit event

I have using the Kendo UI Web grid and when I edit a record in the grid I have a drop down list to select values. 我使用了Kendo UI Web网格,当我在网格中编辑记录时,我有一个下拉列表来选择值。 The problem that I can't seem to over come is how do I set the value of the drop down, when the edit button is clicked, to the value that was present in the table (grid) before the edit. 我似乎无法克服的问题是,当单击编辑按钮时,如何将下拉菜单的值设置为编辑前表(网格)中存在的值。

I have my code below that I have tried and every time it displays blank when I right it out to the console. 我在下面尝试了我的代码,每次将其移到控制台时,它每次都显示为空白。

    $("#project-numbers-grid").kendoGrid({
        dataSource: {
            transport: {
                read: {
                    url: "/Project/GetProjectNumbers",
                    dataType: "json",
                    data: {
                        q: function () {
                            var model = {
                                projectid: "@Model.Id"
                            };
                            return JSON.stringify(model);
                        }
                    }
                },
                update: {
                    url: "/Project/UpdateProjectNumber",
                    dataType: "json"
                },
                destroy: {
                    url: "/Project/DeleteProjectNumber",
                    dataType: "json"
                },
                create: {
                    url: "/Project/CreateProjectNumber",
                    dataType: "json",
                    complete: function (e) {
                        $("#project-numbers-grid").data("kendoGrid").dataSource.read();
                    }
                },
                parameterMap: function (options, operation) {
                    //if (operation !== "read" && options.models) {
                    //    return { models: kendo.stringify(options.models) };
                    //}
                    if (operation === "read") {
                        return options;
                    }

                    var model = {
                        id: options.Id,
                        projectid: "@Model.Id",
                        number: options.Number,
                        active: options.Active,
                        projectphase: options.ProjectPhase
                    };
                    return model;
                }
            },
            pageSize: 4,
            schema: {
                model: {
                    id: "Id",
                    fields: {
                        Id: { editable: false },
                        ProjectPhase: { validation: { required: true } },
                        Number: { validation: { required: true } },
                        Active: { type: "boolean" }
                    }
                }
            }
        },
        toolbar: ["create"],
        edit: function (e) {
            var d = $('#project-phases').data('kendoDropDownList');
            d.value(e.model.ProjectPhase);
            console.log(d.value());
        },
        columns: [
            {
                title: "Project Phase",
                field: "ProjectPhase",
                editor: projectPhaseEditor,
                template: "#= ProjectPhase #"
            },
            {
                title: "Project Number",
                field: "Number"
            },
            "Active",
            {
                title: "&nbsp",
                command: ["edit", "destroy"]
            }
        ],
        editable: "inline",
        pageable: {
            refresh: true
        }
    });
});

function projectPhaseEditor(container, options) {
    $('<input required id="project-phases" data-text-field="ProjectPhase" data-value-field="Id" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoDropDownList({
            autoBind: false,
            dataTextField: "ProjectPhase",
            dataValueField: "Id",
            dataSource: {
                transport: {
                    read: {
                        dataType: "json",
                        url: "/Project/GetProjectPhases"
                    }

                }
            }
        });
}

Here is some old code I found that were used with the telerik MVC helpers and I want this exact behavior except to work with the kendo code above. 这是我发现与telerik MVC助手一起使用的一些旧代码,除了要与上面的kendo代码一起使用外,我想要这种确切的行为。

<script type="text/javascript">
    function onProductManufacturerEdit(e) {
        $(e.form).find('#Manufacturer').data('tDropDownList').select(function (dataItem) {
            return dataItem.Value == e.dataItem['ManufacturerId'];
        });
    }
</script>

Any help on setting the drop down value from the table grid value on edit is much appreciated. 非常感谢您在编辑时从表格网格值设置下拉值的任何帮助。 Thanks 谢谢

EDIT: I have tried the following, without success... 编辑:我尝试了以下,但没有成功...

   $("#project-phases").attr("data-value-field", e.model.ProjectPhase);

EDIT: I have made the following changes without any success... 编辑:我进行了以下更改,没有任何成功...

 function projectPhaseEditor(container, options) {
    $('<input required id="project-phases" data-text-field="ProjectPhase" data-value-                                                                        
    field="ProjectPhaseId" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoDropDownList({
            autoBind: false,
            dataTextField: "ProjectPhase",
            dataValueField: "ProjectPhaseId",
            dataSource: {
                transport: {
                    read: {
                        dataType: "json",
                        url: "/Project/GetProjectPhases"
                    }
                }
            }
        });
}

And my updated code: 和我更新的代码:

 var d = $('#project-phases').data('kendoDropDownList');
            d.value(e.model.ProjectPhaseId);

The problem is there is no ProjectPhaseId on e.model. 问题是e.model上没有ProjectPhaseId。 There is an e.model.Id but that id is the one of the projectnumber entity from the database not the id of the project phase. 有一个e.model.Id,但是该id是数据库中的projectnumber实体之一,而不是项目阶段的id。

I was able to successfully select the value that was present in the table. 我能够成功选择表中存在的值。 I did this by changing my data model on the server side to include a "ProjectPhaseModel" and then the key on the client is when you are defining your columns to map the dropdown column to the property on your server model that is a class that holds the dropdown information. 我是通过在服务器端更改数据模型以使其包含“ ProjectPhaseModel”来实现的,然后,客户端上的键是在定义列以将下拉列映射到服务器模型上包含该类的属性时下拉信息。 Here is the column information on the client side: 这是客户端上的列信息:

           {
                title: "Project Phase",
                field: "ProjectPhase",
                editor: projectPhaseEditor,
                template: "#= ProjectPhase.ProjectPhase #"
            }

And the server side model: 和服务器端模型:

public class ProjectNumberModel : BaseModel
{
    public ProjectPhaseModel ProjectPhase { get; set; }

    public string Number { get; set; }

    public bool Active { get; set; }

    public int ProjectId { get; set; }

    public ProjectNumberModel()
    {
        ProjectPhase = new ProjectPhaseModel();
    }
}

public class ProjectPhaseModel
{
    public int ProjectPhaseId { get; set; }
    public string ProjectPhase { get; set; }
}

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

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