From 0e5d4862161db11f1675e6689f75e3999ed62986 Mon Sep 17 00:00:00 2001 From: Georgi Denchev Date: Wed, 2 Dec 2020 19:11:28 +0200 Subject: [PATCH] feat: add TreeList CRUD example --- .../Models/EmployeeDirectoryModel.cs | 48 +++++ .../TreeList/TreeListCrudOperations.cshtml | 64 ++++++ .../TreeList/TreeListCrudOperations.cshtml.cs | 183 ++++++++++++++++++ 3 files changed, 295 insertions(+) create mode 100644 Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Models/EmployeeDirectoryModel.cs create mode 100644 Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Pages/TreeList/TreeListCrudOperations.cshtml create mode 100644 Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Pages/TreeList/TreeListCrudOperations.cshtml.cs diff --git a/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Models/EmployeeDirectoryModel.cs b/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Models/EmployeeDirectoryModel.cs new file mode 100644 index 0000000..07da6ad --- /dev/null +++ b/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Models/EmployeeDirectoryModel.cs @@ -0,0 +1,48 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace Telerik.Examples.RazorPages.Models +{ + public class EmployeeDirectoryModel + { + [ScaffoldColumn(false)] + public int EmployeeId { get; set; } + + [Required] + [Display(Name = "First name")] + public string FirstName { get; set; } + + [Display(Name = "Last name")] + public string LastName { get; set; } + + [ScaffoldColumn(false)] + public int? ReportsTo { get; set; } + + public string Position { get; set; } + + [ScaffoldColumn(false)] + public bool hasChildren { get; set; } + + private DateTime? hireDate; + [DataType(DataType.Date)] + [Display(Name = "Hire Date")] + public DateTime? HireDate + { + get + { + return hireDate; + } + set + { + if (value.HasValue) + { + hireDate = value.Value; + } + else + { + hireDate = null; + } + } + } + } +} diff --git a/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Pages/TreeList/TreeListCrudOperations.cshtml b/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Pages/TreeList/TreeListCrudOperations.cshtml new file mode 100644 index 0000000..5188beb --- /dev/null +++ b/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Pages/TreeList/TreeListCrudOperations.cshtml @@ -0,0 +1,64 @@ +@page +@model Telerik.Examples.RazorPages.Pages.TreeList.TreeListCrudOperationsModel +@{ + ViewData["Title"] = "TreeListCrudOperations"; +} + +@using Telerik.Examples.RazorPages.Models + +@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf +@Html.AntiForgeryToken() + +@(Html.Kendo().TreeList() + .Name("treelist") + .Toolbar(toolbar => toolbar.Create()) + .Columns(columns => + { + columns.Add().Field(e => e.FirstName).Title("First Name").Width(220); + columns.Add().Field(e => e.LastName).Title("Last Name").Width(200); + columns.Add().Field(e => e.Position); + columns.Add().Field(e => e.HireDate).Format("{0:MMMM d, yyyy}"); + columns.Add().Width(350).Command(c => + { + c.CreateChild().Text("Add child"); + c.Edit(); + c.Destroy(); + }) + .HtmlAttributes(new + { + style = "text-align: center;" + }); + }) + .Editable() + .DataSource(dataSource => dataSource + .Read(r => r.Url("/TreeList/TreeListCrudOperations?handler=Read").Data("forgeryToken")) + .Update(u => u.Url("/TreeList/TreeListCrudOperations?handler=Update").Data("forgeryToken")) + .Create(c => c.Url("/TreeList/TreeListCrudOperations?handler=Create").Data("forgeryToken")) + .Destroy(d => d.Url("/TreeList/TreeListCrudOperations?handler=Destroy").Data("forgeryToken")) + .Model(m => + { + m.Id(f => f.EmployeeId); + m.ParentId(f => f.ReportsTo); + m.Expanded(true); + m.Field(f => f.FirstName); + m.Field(f => f.LastName); + m.Field(f => f.ReportsTo); + m.Field(f => f.HireDate); + m.Field(f => f.Position); + }) + ) + .Height(540) +) + + + + \ No newline at end of file diff --git a/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Pages/TreeList/TreeListCrudOperations.cshtml.cs b/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Pages/TreeList/TreeListCrudOperations.cshtml.cs new file mode 100644 index 0000000..431964e --- /dev/null +++ b/Telerik.Examples.RazorPages/Telerik.Examples.RazorPages/Pages/TreeList/TreeListCrudOperations.cshtml.cs @@ -0,0 +1,183 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Kendo.Mvc.Extensions; +using Kendo.Mvc.UI; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Telerik.Examples.RazorPages.Models; + +namespace Telerik.Examples.RazorPages.Pages.TreeList +{ + public class TreeListCrudOperationsModel : PageModel + { + private static IList employees; + + public void OnGet() + { + GetDirectory(); + } + + public JsonResult OnPostRead([DataSourceRequest] DataSourceRequest request) + { + var result = employees.ToTreeDataSourceResult(request, + e => e.EmployeeId, + e => e.ReportsTo, + e => e + ); + + return new JsonResult(result); + } + + public JsonResult OnPostCreate([DataSourceRequest] DataSourceRequest request, EmployeeDirectoryModel employee) + { + employee.EmployeeId = employees.Count + 2; + employees.Add(employee); + + return new JsonResult(new[] { employee }.ToTreeDataSourceResult(request, ModelState)); + } + + public JsonResult OnPostUpdate([DataSourceRequest] DataSourceRequest request, EmployeeDirectoryModel employee) + { + var target = employees.FirstOrDefault(x => x.EmployeeId == employee.EmployeeId); + + if(target != null) + { + target.FirstName = employee.FirstName; + target.LastName = employee.LastName; + target.HireDate = employee.HireDate; + target.Position = employee.Position; + target.ReportsTo = employee.ReportsTo; + } + + return new JsonResult(new[] { employee }.ToTreeDataSourceResult(request, ModelState)); + } + + public JsonResult OnPostDestroy([DataSourceRequest] DataSourceRequest request, EmployeeDirectoryModel employee) + { + employees.Remove(employees.FirstOrDefault(x => x.EmployeeId == employee.EmployeeId)); + + return new JsonResult(new[] { employee }.ToTreeDataSourceResult(request, ModelState)); + } + + private IList GetDirectory() + { + if (employees == null) + { + employees = new List(); + employees.Add(new EmployeeDirectoryModel + { + EmployeeId = 1, + FirstName = "Daryl", + LastName = "Sweeney", + ReportsTo = null, + HireDate = DateTime.Now.AddDays(1), + Position = "CEO", + }); + + employees.Add(new EmployeeDirectoryModel + { + EmployeeId = 2, + FirstName = "Guy", + LastName = "Wooten", + ReportsTo = 1, + HireDate = DateTime.Now.AddDays(2), + Position = "Chief Technical Officer" + }); + + employees.Add(new EmployeeDirectoryModel + { + EmployeeId = 3, + FirstName = "Buffy", + LastName = "Weber", + ReportsTo = 2, + HireDate = DateTime.Now.AddDays(3), + Position = "VP, Engineering" + }); + + employees.Add(new EmployeeDirectoryModel + { + EmployeeId = 4, + FirstName = "Hyacinth", + LastName = "Hood", + ReportsTo = 3, + HireDate = DateTime.Now.AddDays(4), + Position = "Team Lead" + }); + + employees.Add(new EmployeeDirectoryModel + { + EmployeeId = 5, + FirstName = "Akeem", + LastName = "Carr", + ReportsTo = 4, + HireDate = DateTime.Now.AddDays(5), + Position = "Junior Software Developer" + }); + + employees.Add(new EmployeeDirectoryModel + { + EmployeeId = 6, + FirstName = "Rinah", + LastName = "Simon", + ReportsTo = 4, + HireDate = DateTime.Now.AddDays(6), + Position = "Software Developer" + }); + + employees.Add(new EmployeeDirectoryModel + { + EmployeeId = 7, + FirstName = "Gage", + LastName = "Daniels", + ReportsTo = 3, + HireDate = DateTime.Now.AddDays(7), + Position = "Software Architect", + }); + + employees.Add(new EmployeeDirectoryModel + { + EmployeeId = 8, + FirstName = "Constance", + LastName = "Vazquez", + ReportsTo = 3, + HireDate = DateTime.Now.AddDays(8), + Position = "Director, Engineering", + }); + + employees.Add(new EmployeeDirectoryModel + { + EmployeeId = 9, + FirstName = "Darrel", + LastName = "Solis", + ReportsTo = 8, + HireDate = DateTime.Now.AddDays(9), + Position = "Team Lead", + }); + + employees.Add(new EmployeeDirectoryModel + { + EmployeeId = 10, + FirstName = "Brian", + LastName = "Yang", + ReportsTo = 9, + HireDate = DateTime.Now.AddDays(10), + Position = "Senior Software Developer", + }); + + employees.Add(new EmployeeDirectoryModel + { + EmployeeId = 11, + FirstName = "Lillian", + LastName = "Bradshaw", + ReportsTo = 9, + HireDate = DateTime.Now.AddDays(11), + Position = "Software Developer", + }); + } + + return employees; + } + } +}