Ответ 1
Попробуйте использовать приведенный ниже фрагмент кода.
<!DOCTYPE html>
<html>
<head>
<title>Jayesh Goyani</title>
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.1111/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.1111/styles/kendo.rtl.min.css">
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.1111/styles/kendo.default.min.css">
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2015.3.1111/styles/kendo.mobile.all.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2015.3.1111/js/angular.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2015.3.1111/js/jszip.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2015.3.1111/js/kendo.all.min.js"></script>
</head>
<body>
<div id="grid">
</div>
<script>
var sources = [];
var destinations = [];
var products = [{
ProductID: 1,
ProductName: "Chai",
SourceID: 1,
DestinationID: 1,
}, {
ProductID: 2,
ProductName: "Chang",
SourceID: 2,
DestinationID: 1,
}, {
ProductID: 3,
ProductName: "Aniseed Syrup",
SourceID: 3,
DestinationID: 2,
}, {
ProductID: 4,
ProductName: "Chef Anton Cajun Seasoning",
SourceID: 4,
DestinationID: 2,
}, {
ProductID: 5,
ProductName: "Chef Anton Gumbo Mix",
SourceID: 4,
DestinationID: 2,
}];
$(document).ready(function () {
$("#grid").kendoGrid({
dataSource: {
data: products,
schema: {
model: {
id: "ProductID",
fields: {
ProductName: { type: "string" }
}
}
},
pageSize: 10
},
sortable: true,
edit: onGridEdit,
filterable: true,
pageable: {
input: true,
numeric: false
},
columns: [
{ field: "ProductName" },
{ field: "SourceID", title: "SourceID", values: sources },
{ field: "DestinationID", title: "DestinationID", values: destinations },
{ command: ["edit", "destroy"], title: " " }
],
editable: "inline"
});
});
var destinationID = 0;
function onGridEdit(arg) {
destinationID = arg.model.DestinationID;
$.ajax({
url: "http://localhost:3470/Home/GetSource",
type: 'GET',
success: function (data) {
var sourceDDL = $(arg.container).find("select[name^='SourceID']").data("kendoDropDownList");
sourceDDL.bind("change", onChange);
sourceDDL.setDataSource(data);
sourceDDL.value(arg.model.SourceID);
onChange();
}
});
}
function onChange(arg) {
var sourceid = $("select[name^='SourceID']").data("kendoDropDownList").value();
$.ajax({
url: "http://localhost:3470/Home/GetDestination",
type: 'GET',
data: { SourceID: sourceid },
success: function (data) {
var destinationDDL = $("select[name^='DestinationID']").data("kendoDropDownList");
destinationDDL.setDataSource(data);
if (arg) {
// Please uncomment below code if you want to reset ddl value on sourceDDl value change
// destinationDDL.select(-1);
}
else {
destinationDDL.value(destinationID);
destinationID = 0;
}
}
});
}
</script>
</body>
</html>
Для справки: -
public class Source
{
public int value { get; set; }
public string text { get; set; }
}
public class Destination
{
public int value { get; set; }
public string text { get; set; }
}
.....
.....
public ActionResult GetSource()
{
List<Source> list = new List<Source>();
list.Add(new Source() { value = 1, text = "cat1" });
list.Add(new Source() { value = 2, text = "cat2" });
list.Add(new Source() { value = 3, text = "cat3" });
list.Add(new Source() { value = 4, text = "cat4" });
list.Add(new Source() { value = 5, text = "cat5" });
return Json(list, JsonRequestBehavior.AllowGet);
}
public ActionResult GetDestination(int? SourceID)
{
List<Destination> list = new List<Destination>();
list.Add(new Destination() { value = 1, text = "des1_" + Convert.ToString(SourceID) });
list.Add(new Destination() { value = 2, text = "des2_" });
list.Add(new Destination() { value = 3, text = "des3_" });
list.Add(new Destination() { value = 4, text = "des4_" });
list.Add(new Destination() { value = 5, text = "des5_" });
return Json(list, JsonRequestBehavior.AllowGet);
}
Обновление 1: (На основе вашего редактора у меня есть обновленная инструкция селектора jquery)
function onGridEdit(arg) {
var sourceDDL = $(arg.container).find("input[id^='sourcesDropDownList']").data("kendoDropDownList");
}
function onChange(arg) {
var sourceid = $("input[id^='sourcesDropDownList']").data("kendoDropDownList").value();
var destinationDDL = $("input[id^='destinationsDropDownList']").data("kendoDropDownList");
}
Сообщите мне, если какая-либо проблема.