Kendo Grid with Kendo Dropdownlist所选值未更新

时间:2014-01-27 06:27:40

标签: javascript jquery asp.net-mvc-4 kendo-ui kendo-grid

在我的kendo网格中,我为每列提供了kendodropdownlist。选定的项目应该解析并显示模板文本

我一直在关注此示例 http://jsfiddle.net/jddevight/Ms3nn/

更新

我已在此处 http://jsfiddle.net/BlowMan/mf434/

简化了我的问题

问题

当我在下拉列表中选择一个项目时,它不会返回所选项目的值。它宁愿返回null。

/// <reference path="../../jquery-1.8.2.js" />
/// <reference path="../../kendo.web.min.js" />
$(function () {
var menuModel = kendo.data.Model.define({
    fields: {
        "MenuId": { type: "number",editable:false },
        "DisplayText":{type:"string"},
        "MenuOrder": { type: "number" },
        "MenuStatus": { type: "boolean" },
        "HasKids": { type: "boolean" },
        "ParentMenu": { type: "number" }
    }
});

var menuDataSource = new kendo.data.DataSource({

    data:[{"MenuId":1,
    "DisplayText":"Home",
    "MenuOrder":0,
    "MenuStatus":true,
    "HasKids":false,
    "ParentMenu":null},
    {"MenuId":2,
    "DisplayText":"Finance",
    "MenuOrder":1,
    "MenuStatus":true,
    "HasKids":false,
    "ParentMenu":null}]
    schema: {
        model: menuModel
     }
});
var vm = kendo.observable({
    menus: menuDataSource,
    parentItem: [{ Id: 2, 
                    Name: "Finance" },
                { Id: 3, 
                Name: "Corp Services" }],
    getMenuName: function (pMenu) {
        var menuName = "";
        $.each(this.parentItem, function (idx, menu) {
            if (menu.Id == pMenu) {
                menuName = menu.Name;
                return false;
            }
        });
        return menuName;
    }
});
kendo.bind($("#menuItems"), vm);

var parentMenuEditor = function (container, options) {
    $("<input name='" + options.field + "'/>")
    .appendTo(container)
    .kendoDropDownList({
        dataSource: {
           data:[{ Id: 2, Name: "Finance" }, { Id: 3, Name: "Corp Services" }],
        },
        dataTextField: "Name",
        dataValueField: "Id"
    });
};

var grid = $("div[data-role='grid']").data("kendoGrid");
$.each(grid.columns, function (idx, column) {
    if (column.field == "ParentMenu") {
        column.editor = parentMenuEditor;

        return false;
    }
});

});

以下视图部分

        @{
            ViewBag.Title = "Menu System Index";
        }

        <h2>Menu System Index</h2>
        <div id="menuItems">
            <div class="k-toolbar k-grid-toolbar">
                <a class="k-button k-button-icontext k-grid-add" href="#">
                    <span class="k-icon k-add"></span>
                    Add Person
                </a>
             </div>
            <div data-role="grid" 
                 data-bind="source: menus" 
                 data-editable="true" 
                 data-filterable="true"
                 data-columns='[{"field": "MenuId", "title": "MenuId"}, 
                                {"field": "DisplayText", "title": "DisplayText"}, 
                                {"field": "MenuOrder", "title": "MenuOrder"}, 
                                {"field": "MenuStatus", "title": "MenuStatus"},
                                {"field":"HasKids","title":"HasKids"},                
                                {"field":"ParentMenu","title":"ParentMenu","template":"#= parent().parent().getMenuName(ParentMenu) #"},
                {"command": "destroy", "title": " ", "width": "110px"}]'>
            </div>
        </div>

        <script type="text/x-kendo-template" id="toolbar-template">
            <a class="k-button k-button-icontext k-grid-add" href="\#"><span class="k-icon k-add"></span>Add new record</a>
        </script>

        @section scripts{

            <script src="~/Scripts/Custom/MenuSystem/Index.js"></script>
        }

非常感谢任何帮助。这个问题让我跪了下来。

1 个答案:

答案 0 :(得分:1)

我更新了您的JsFiddle演示版。请检查一下,如果有任何疑虑,请告诉我。

var roleEditor = function(container, options) {
$("<input name='" + options.field + "' data-text-field='name' data-value-field='id' data-bind='value:" + options.field + "'/>")

    .appendTo(container)
    .kendoDropDownList({
        dataSource: {
            data: vm.roles
        },
        dataTextField: "name",
        dataValueField: "id"
    });
};