Skip to content

Commit 413b992

Browse files
chore(docs): add Grid Editing with Cascading DropDownLists telerik/kendo#21409 (#87)
1 parent 7f24eaf commit 413b992

File tree

10 files changed

+302
-0
lines changed

10 files changed

+302
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using Kendo.Mvc.Extensions;
2+
using Kendo.Mvc.UI;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Telerik.Examples.Mvc.Models;
7+
8+
namespace Telerik.Examples.Mvc.Controllers.Grid
9+
{
10+
public class EditingWithCascadingDropDownListsController : Controller
11+
{
12+
private readonly IEnumerable<Vendor> vendors;
13+
private readonly IEnumerable<Customer> customers;
14+
private readonly IEnumerable<Item> items;
15+
16+
public ActionResult EditingWithCascadingDropDownLists()
17+
{
18+
return View();
19+
}
20+
21+
public EditingWithCascadingDropDownListsController()
22+
{
23+
this.items = Enumerable.Range(1, 125)
24+
.Select(i => new Item
25+
{
26+
ItemId = i,
27+
ItemName = "ItemName " + i,
28+
VendorId = (i - 1) / 5 + 1,
29+
});
30+
31+
this.vendors = Enumerable.Range(1, 25)
32+
.Select(i => new Vendor
33+
{
34+
VendorId = i,
35+
VendorName = "VendorName " + i,
36+
CustomerId = (i - 1) / 5 + 1
37+
});
38+
39+
this.customers = Enumerable.Range(1, 5)
40+
.Select(i => new Customer
41+
{
42+
CustomerId = i,
43+
CustomerName = "CustomerName " + i,
44+
});
45+
}
46+
47+
[HttpPost]
48+
public JsonResult Read([DataSourceRequest] DataSourceRequest request)
49+
{
50+
var licenses = Enumerable.Range(1, 50)
51+
.Select(i => new License
52+
{
53+
Customer = new Customer { CustomerId = 1, CustomerName = "CustomerName 1" },
54+
Vendor = new Vendor { VendorId = 1, VendorName = "VendorName 1", CustomerId = 1 },
55+
LicenseId = i,
56+
Item = new Item { ItemId = 1, ItemName = "ItemName 1", VendorId = 1 }
57+
});
58+
59+
return Json(licenses.ToDataSourceResult(request));
60+
}
61+
62+
[HttpPost]
63+
public JsonResult Create([DataSourceRequest] DataSourceRequest request, License license)
64+
{
65+
return Json(new[] { license }.ToDataSourceResult(request, ModelState));
66+
}
67+
68+
[HttpPost]
69+
public JsonResult Update([DataSourceRequest] DataSourceRequest request, License license)
70+
{
71+
return Json(new[] { license }.ToDataSourceResult(request, ModelState));
72+
}
73+
74+
public JsonResult GetCustomers()
75+
{
76+
return Json(customers);
77+
}
78+
79+
public JsonResult GetVendors(int customerId)
80+
{
81+
return Json(vendors.Where(f => f.CustomerId == customerId));
82+
}
83+
84+
public JsonResult GetItems(int vendorId)
85+
{
86+
return Json(items.Where(f => f.VendorId == vendorId));
87+
}
88+
}
89+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Telerik.Examples.Mvc.Models
2+
{
3+
public class Customer
4+
{
5+
public int CustomerId { get; set; }
6+
public string CustomerName { get; set; }
7+
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Telerik.Examples.Mvc.Models
2+
{
3+
public class Item
4+
{
5+
public int ItemId { get; set; }
6+
public string ItemName { get; set; }
7+
public int VendorId { get; set; }
8+
}
9+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Telerik.Examples.Mvc.Models
4+
{
5+
public class License
6+
{
7+
public int LicenseId { get; set; }
8+
9+
[UIHint("CustomerId")]
10+
public Customer Customer { get; set; }
11+
12+
[UIHint("VendorId")]
13+
public Vendor Vendor { get; set; }
14+
15+
[UIHint("ItemId")]
16+
public Item Item { get; set; }
17+
}
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Telerik.Examples.Mvc.Models
2+
{
3+
public class Vendor
4+
{
5+
public int VendorId { get; set; }
6+
public int CustomerId { get; set; }
7+
public string VendorName { get; set; }
8+
}
9+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
@using Telerik.Examples.Mvc.Models
2+
3+
<h1>Popup Editing</h1>
4+
5+
@(Html.Kendo().Grid<License>()
6+
.Name("popupGrid")
7+
.Columns(columns =>
8+
{
9+
columns.Bound(p => p.Customer).Width(20).HeaderHtmlAttributes(new { @title = "Customer" }).ClientTemplate("#=Customer.CustomerName#");
10+
columns.Bound(p => p.Vendor).Width(20).HeaderHtmlAttributes(new { @title = "Vendor" }).ClientTemplate("#=Vendor == null ? '' : Vendor.VendorName#");
11+
columns.Bound(p => p.Item).Width(20).HeaderHtmlAttributes(new { @title = "Item" }).ClientTemplate("#=Item == null ? '' : Item.ItemName#");
12+
columns.Command(p => p.Edit().Text("Edit").HtmlAttributes(new { @title = "Edit" })).Width(80);
13+
})
14+
.ToolBar(toolbar => toolbar.Create().Text("Add").HtmlAttributes(new { @title = "Add" }))
15+
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("CustomCascadeDropDownListPopUp"))
16+
.Events(e => e.Edit("onEdit"))
17+
.Pageable()
18+
.DataSource(dataSource => dataSource
19+
.Ajax()
20+
.Model(model =>
21+
{
22+
model.Id(p => p.LicenseId);
23+
model.Field(f => f.Customer).DefaultValue(new Customer { CustomerId = 1, CustomerName = "CustomerName 1" });
24+
model.Field(f => f.Vendor).DefaultValue(new Vendor { VendorId = 1, VendorName = "VendorName 1", CustomerId = 1 });
25+
model.Field(f => f.Item).DefaultValue(new Item { ItemId = 1, ItemName = "ItemName 1", VendorId = 1 });
26+
})
27+
.Create(create => create.Action("Create", "EditingWithCascadingDropDownLists").Type(HttpVerbs.Post))
28+
.Read(read => read.Action("Read", "EditingWithCascadingDropDownLists").Type(HttpVerbs.Post))
29+
.Update(update => update.Action("Update", "EditingWithCascadingDropDownLists").Type(HttpVerbs.Post))
30+
)
31+
)
32+
33+
<script>
34+
function onEdit(e) {
35+
//attach validator to the PopUp window
36+
$("[data-role=window]").kendoValidator();
37+
}
38+
</script>
39+
40+
<h1>InLine Editing</h1>
41+
42+
@(Html.Kendo().Grid<License>()
43+
.Name("inlineGrid")
44+
.Columns(columns =>
45+
{
46+
columns.Bound(p => p.Customer).Width(20).HeaderHtmlAttributes(new { @title = "Customer" }).ClientTemplate("#=Customer.CustomerName#");
47+
columns.Bound(p => p.Vendor).Width(20).HeaderHtmlAttributes(new { @title = "Vendor" }).ClientTemplate("#=Vendor.VendorName#");
48+
columns.Bound(p => p.Item).Width(20).HeaderHtmlAttributes(new { @title = "Item" }).ClientTemplate("#=Item.ItemName#");
49+
columns.Command(p => p.Edit().Text("Edit").HtmlAttributes(new { @title = "Edit" })).Width(80);
50+
})
51+
.ToolBar(toolbar => toolbar.Create().Text("Add").HtmlAttributes(new { @title = "Add" }))
52+
.Editable(editable => editable.Mode(GridEditMode.InLine))
53+
.Pageable()
54+
.DataSource(dataSource => dataSource
55+
.Ajax()
56+
.Model(model =>
57+
{
58+
model.Id(p => p.LicenseId);
59+
model.Field(f => f.Customer).DefaultValue(new Customer { CustomerId = 1, CustomerName = "CustomerName 1" });
60+
model.Field(f => f.Vendor).DefaultValue(new Vendor { VendorId = 1, VendorName = "VendorName 1", CustomerId = 1 });
61+
model.Field(f => f.Item).DefaultValue(new Item { ItemId = 1, ItemName = "ItemName 1", VendorId = 1 });
62+
})
63+
.Create(create => create.Action("Create", "EditingWithCascadingDropDownLists").Type(HttpVerbs.Post))
64+
.Read(read => read.Action("Read", "EditingWithCascadingDropDownLists").Type(HttpVerbs.Post))
65+
.Update(update => update.Action("Update", "EditingWithCascadingDropDownLists").Type(HttpVerbs.Post))
66+
)
67+
)
68+
69+
<script>
70+
function filterVendors() {
71+
return {
72+
customerId: $("#Customer").data("kendoDropDownList").value()
73+
};
74+
}
75+
76+
function filterItems() {
77+
return {
78+
vendorId: $("#Vendor").data("kendoDropDownList").value()
79+
};
80+
}
81+
</script>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
@model License
2+
3+
@(Html.Kendo().DropDownListFor(m => m.Customer)
4+
.OptionLabel("Select Customer...")
5+
.Label(l => l.Content("Customer"))
6+
.DataTextField("CustomerName")
7+
.DataValueField("CustomerId")
8+
.DataSource(dataSource =>
9+
{
10+
dataSource.Read(read => read.Action("GetCustomers", "EditingWithCascadingDropDownLists"))
11+
.ServerFiltering(true);
12+
})
13+
)
14+
15+
@(Html.Kendo().DropDownListFor(m => m.Vendor)
16+
.AutoBind(false)
17+
.OptionLabel("Select Vendor...")
18+
.Label(l => l.Content("Vendor"))
19+
.DataTextField("VendorName")
20+
.DataValueField("VendorId")
21+
.DataSource(dataSource =>
22+
{
23+
dataSource.Read(read => read.Action("GetVendors", "EditingWithCascadingDropDownLists").Data("filterVendors"))
24+
.ServerFiltering(true);
25+
})
26+
.CascadeFrom("Customer")
27+
)
28+
29+
@(Html.Kendo().DropDownListFor(m => m.Item)
30+
.AutoBind(false)
31+
.OptionLabel("Select Item...")
32+
.Label(l => l.Content("Item"))
33+
.DataTextField("ItemName")
34+
.DataValueField("ItemId")
35+
.DataSource(dataSource =>
36+
{
37+
dataSource.Read(read => read.Action("GetItems", "EditingWithCascadingDropDownLists").Data("filterItems"))
38+
.ServerFiltering(true);
39+
})
40+
.CascadeFrom("Vendor")
41+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@model Customer
2+
3+
@(Html.Kendo().DropDownListFor(m => m)
4+
.OptionLabel("Select Customer...")
5+
.DataTextField("CustomerName")
6+
.DataValueField("CustomerId")
7+
.DataSource(dataSource =>
8+
{
9+
dataSource.Read(read => read.Action("GetCustomers", "EditingWithCascadingDropDownLists"))
10+
.ServerFiltering(true);
11+
})
12+
)
13+
14+
@Html.ValidationMessageFor(m => m)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@model Item
2+
3+
@(Html.Kendo().DropDownListFor(m => m)
4+
.AutoBind(false)
5+
.OptionLabel("Select Item...")
6+
.DataTextField("ItemName")
7+
.DataValueField("ItemId")
8+
.DataSource(dataSource =>
9+
{
10+
dataSource.Read(read => read.Action("GetItems", "EditingWithCascadingDropDownLists").Data("filterItems"))
11+
.ServerFiltering(true);
12+
})
13+
.CascadeFrom("Vendor")
14+
)
15+
16+
@Html.ValidationMessageFor(m => m)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@model Vendor
2+
3+
@(Html.Kendo().DropDownListFor(m => m)
4+
.AutoBind(false)
5+
.OptionLabel("Select Vendor...")
6+
.DataTextField("VendorName")
7+
.DataValueField("VendorId")
8+
.DataSource(dataSource =>
9+
{
10+
dataSource.Read(read => read.Action("GetVendors", "EditingWithCascadingDropDownLists").Data("filterVendors"))
11+
.ServerFiltering(true);
12+
})
13+
.CascadeFrom("Customer")
14+
)
15+
16+
@Html.ValidationMessageFor(m => m)

0 commit comments

Comments
 (0)