0

我有一个启用了批处理编辑的剑道网格,其中包含一个下拉列表。我可以在尝试保存批处理信息时读取网格的其他字段,但很难读取下拉列表的选定值。这是我的网格代码:

  $(document).ready(function () {
            $("#grid").kendoGrid({
                dataSource: datasource,
                navigatable: true,
                pageable: true,
                height: 430,
                sortable: true,
                editable: true,
                selectable: "multiple row",
                groupable: true,
                navigatable: true,
                filterable: true,
                toolbar: ["create", "cancel"],
                columns: [

                    { field: "EmployeeID", title: "Employee ID", width: 110 },
                    { field: "EmployeeName", title: "Employee Name", width: 110 },
                    { field: "Category", title: "Category", editor: categoryDropDownEditor, width: 50 },

                    { command: "destroy", title: "Delete", width: 90 }
                ],



            });
        });

这是下拉列表的代码:

 function categoryDropDownEditor(container, options) {
            $('<input required data-text-field="CategoryName" data-value-field="CategoryID" data-bind="value:' + options.field + '" id="test"/>')
                .appendTo(container)
                .kendoDropDownList({
                    dataTextField: "EmployeeName",
                    dataValueField: "EmployeeID",
                    optionLabel: "Select",
                    autoBind: false,

                    //index: 0,
                    //change: onChange,
                    dataSource: new kendo.data.DataSource( {

                        transport: {
                            read: {
                                url: "/Home/Category",
                                type: "GET"
                            },
                            schema:{
                                model: {
                                    ID: "CategoryID",
                                    Value: "CategoryName"
                                }
                            }
                        }
                    })
                });
        }

这是我试图保存值的代码:

function Save() {


        var EmployeeInfo = { "EmployeeID": "", "EmployeeName": "", "CategoryID": "" };

        var CompanyInfo = { "CompanyID": "", "CompanyName": "", "Employee": [] };

        CompanyInfo.CompanyID = $("#CompanyID").val();
        CompanyInfo.CompanyName = $("#CompanyName").val();

        var drop = $("#test").data("kendoDropDownList");

        var GridData = $("#grid").data("kendoGrid");

        var data = GridData.dataSource.data();
        for (var i = 0; i < data.length; i++) {

            var item = data[i]; //Got the dropdown selected value & text here, just need to assign that value to the CategoryID attribute!
            EmployeeInfo.EmployeeID = item.EmployeeID;
            EmployeeInfo.EmployeeName = item.EmployeeName; 
            EmployeeInfo.CategoryID = item.CategoryID[0];  //Problem is here in assinging data!!
            CompanyInfo.Employee.push(EmployeeInfo);
            EmployeeInfo = { "EmployeeID": "", "EmployeeName": "" };


        }


        $.ajax({
            url: '/Home/Create',
            data: JSON.stringify(CompanyInfo),
            type: 'POST',
            contentType: 'application/json;',
            dataType: 'json'

        });
    };

如何获取下拉列表的选定值?另一个问题是,在下拉列表中选择一个项目后,当我移动到另一行网格时,下拉列表中的选定文本会发生变化,而是显示 [object, object]。请帮忙。谢谢。

4

3 回答 3

1

问题是您尝试从Save函数访问 DropDownList 很可能input不再存在。您应该像访问其他字段一样简单地访问该字段。函数会更新editor字段,因为它是一个可观察对象。

尝试做:

for (var i = 0; i < data.length; i++) {
    var item = data[i];
    EmployeeInfo.EmployeeID = item.EmployeeID;
    EmployeeInfo.EmployeeName = item.EmployeeName; 
    EmployeeInfo.CategoryID = item.Category,
    CompanyInfo.Employee.push(EmployeeInfo);
    EmployeeInfo = { "EmployeeID": "", "EmployeeName": "" };
}

顺便说一句,我不确定在将数据发送到服务器之前要进行的处理,但通常人们使用create,updatedestroyin DataSource.transport。它用于简化您的开发并让您免于ajax拨打电话。

于 2013-07-16T07:19:08.710 回答
0

您可以通过使用 jquery 传递标签名称来获取选定的值。试试这个

$('#grid select option:selected').val(); // for val


$('#grid select option:selected').text(); // for text
于 2013-07-16T06:30:19.090 回答
0

当从下拉列表中选择一个值时,在 select 事件中,我们可以获得如下选择的值,

@(Html.Kendo().DropDownList()
              .Name("booksDropDown")
              .HtmlAttributes(new { style = "width:37%" })
              .DataTextField("BookName")
              .DataValueField("BookId")
              .Events(x => x.Select("onSelectBookValue"))
              .DataSource(datasource => datasource.Read(action => action.Action("ReadBookDropDow", "PlanningBook").Type(HttpVerbs.Get)))
              .OptionLabel("Select"))

javascript函数如下,

      function onSelectBookValue(e) {    

                    var dataItem = this.dataItem(e.item.index());
                    var bookId = dataItem.BookId;
//other user code
    }

我相信这会帮助某人

谢谢

于 2015-07-16T11:29:47.663 回答