我有一个启用批量编辑的kendo网格,其中包含一个下拉列表。我可以在尝试保存批处理信息时读取网格的其他字段,但却很难读取下拉列表的选定值。这是我的网格代码:
$(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'
});
};
如何获取下拉列表的选定值?另一个问题是,在下拉列表中选择项目后,当我移动到另一行网格时,所选文本在下拉列表中更改,显示[对象,对象]。请帮忙。感谢。
答案 0 :(得分:1)
问题是,当Save
很可能不再存在时,您正试图从input
函数访问DropDownList。您应该像访问其他字段一样访问该字段。 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
,update
和destroy
中使用{} {1}}。它用于简化您的开发并使您免于进行DataSource.transport
调用。
答案 1 :(得分:0)
您可以使用jquery传递标记名称来获取所选值。 试试这个
$('#grid select option:selected').val(); // for val
$('#grid select option:selected').text(); // for text
答案 2 :(得分:0)
从下拉列表中选择一个值时,在selec事件中,我们可以获得如下所选的值,
@(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
}
我相信这会对某人有所帮助
由于