Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(demos): add hierarchy grid signalr example #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;

namespace Telerik.Examples.Mvc.Controllers.Grid
{
public class HierarchySignalRController : Controller
{
public IActionResult HierarchySignalR()
{
return View();
}
}
}
53 changes: 53 additions & 0 deletions Telerik.Examples.Mvc/Telerik.Examples.Mvc/Hubs/ChildGridHub.cs
@@ -0,0 +1,53 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Threading.Tasks;
using Telerik.Examples.Mvc.Models;

namespace Telerik.Examples.Mvc.Hubs
{
public class ChildGridHub : Hub
{
public override System.Threading.Tasks.Task OnConnectedAsync()
{
Groups.AddToGroupAsync(Context.ConnectionId, GetGroupName());
return base.OnConnectedAsync();
}

public override System.Threading.Tasks.Task OnDisconnectedAsync(Exception e)
{
Groups.RemoveFromGroupAsync(Context.ConnectionId, GetGroupName());
return base.OnDisconnectedAsync(e);
}

public IEnumerable<Person> Read(HierarchyRequestModel request)
=> Enumerable.Range(1, 10).Select(i => new Person
{
PersonID = i,
BirthDate = DateTime.Now.AddDays(i),
ProductID = i % 2 == 0 ? 1 : 2,
Name = "Name" + i
})
.Where(person => person.ProductID == request.ProductID)
.ToList();

public Person Create(Person person)
{
Clients.OthersInGroup(GetGroupName()).SendAsync("create", person);
return person;
}

public void Update(Person person)
=> Clients.OthersInGroup(GetGroupName()).SendAsync("update", person);

public void Destroy(Person person)
=> Clients.OthersInGroup(GetGroupName()).SendAsync("destroy", person);
public string GetGroupName()
=> GetRemoteIpAddress();

public string GetRemoteIpAddress()
=> Context.GetHttpContext()?.Connection.RemoteIpAddress.ToString();
}
}
60 changes: 60 additions & 0 deletions Telerik.Examples.Mvc/Telerik.Examples.Mvc/Hubs/ParentGridHub.cs
@@ -0,0 +1,60 @@
using Microsoft.AspNetCore.SignalR;
using System.Collections.Generic;
using System.Linq;
using System;
using Telerik.Examples.Mvc.Models;
using Product = Telerik.Examples.Mvc.Models.Product;

namespace Telerik.Examples.Mvc.Hubs
{
public class ParentGridHub : Hub
{
private Random _randomHelper { get; set; }

public ParentGridHub()
{
_randomHelper = new Random();
}

public override System.Threading.Tasks.Task OnConnectedAsync()
{
Groups.AddToGroupAsync(Context.ConnectionId, GetGroupName());
return base.OnConnectedAsync();
}

public override System.Threading.Tasks.Task OnDisconnectedAsync(Exception e)
{
Groups.RemoveFromGroupAsync(Context.ConnectionId, GetGroupName());
return base.OnDisconnectedAsync(e);
}

public IEnumerable<Product> Read()
=> Enumerable.Range(1, 2).Select(i => new Product
{
Discontinued = i % 2 == 1,
ProductID = i,
ProductName = "Product" + i,
UnitPrice = _randomHelper.Next(1, 1000),
UnitsInStock = _randomHelper.Next(1, 1000),
UnitsOnOrder = _randomHelper.Next(1, 1000)
}).ToList();

public Product Create(Product product)
{
Clients.OthersInGroup(GetGroupName()).SendAsync("create", product);
return product;
}

public void Update(Product product)
=> Clients.OthersInGroup(GetGroupName()).SendAsync("update", product);

public void Destroy(Product product)
=> Clients.OthersInGroup(GetGroupName()).SendAsync("destroy", product);

public string GetGroupName()
=> GetRemoteIpAddress();

public string GetRemoteIpAddress()
=> Context.GetHttpContext()?.Connection.RemoteIpAddress.ToString();
}
}
@@ -0,0 +1,7 @@
namespace Telerik.Examples.Mvc.Models
{
public class HierarchyRequestModel
{
public int ProductID { get; set; }
}
}
2 changes: 2 additions & 0 deletions Telerik.Examples.Mvc/Telerik.Examples.Mvc/Program.cs
Expand Up @@ -118,6 +118,8 @@
{
endpoints.MapHub<MeetingHub>("/meetingHub");
endpoints.MapHub<GridHub>("/gridHub");
endpoints.MapHub<ParentGridHub>("/parentGridHub");
endpoints.MapHub<ChildGridHub>("/childGridHub");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
Expand Down
@@ -0,0 +1,191 @@

<script src="~/js/signalr/dist/browser/signalr.min.js"></script>

<script>
// SignalR configuration.

var parentHubUrl = "/parentGridHub";

var parentGridConnection = new signalR.HubConnectionBuilder()
.withUrl(parentHubUrl, {
transport: signalR.HttpTransportType.LongPolling
})
.build();

var parentGridConnectionStart = parentGridConnection.start()

var childHubUrl = "/childGridHub";

var childGridConnection = new signalR.HubConnectionBuilder()
.withUrl(childHubUrl, {
transport: signalR.HttpTransportType.LongPolling
})
.build();

var childGridConnectionStart = childGridConnection.start();

</script>

<div id="notification-container"></div>
@(Html.Kendo().Notification() // Notification for CRUD operations of both the child and parent Grid components.
.Name("notification")
.Height("30%")
.Width("50%")
.AppendTo("#notification-container")
)

@(Html.Kendo().Grid<Product>() // Parent Grid.
.Name("grid")
.DataSource(dataSource => dataSource
.SignalR()
.AutoSync(false)
.Events(events => events.RequestEnd("onEnd"))
.Transport(tr => tr
.Promise("parentGridConnectionStart")
.Hub("parentGridConnection")
.Client(c => c
.Read("read")
.Create("create")
.Update("update")
.Destroy("destroy"))
.Server(s => s
.Read("read")
.Create("create")
.Update("update")
.Destroy("destroy"))
)
.Schema(schema => schema
.Model(model => {
model.Id(id => id.ProductID);
model.Field(field => field.ProductID).Editable(false);
model.Field(field => field.ProductName);
model.Field(field => field.UnitsInStock);
model.Field(field => field.Discontinued);
model.Field(field => field.UnitPrice);
model.Field(field => field.UnitsOnOrder);
})
)
.PageSize(10)
)
.Events(events => events.DetailInit("onDetailInit"))
.Editable(editable => editable.Mode(GridEditMode.InLine))
.ToolBar(t => t.Create())
.Columns(columns =>
{
columns.Bound(product => product.ProductID);
columns.Bound(product => product.ProductName);
columns.Bound(product => product.UnitsInStock);
columns.Bound(product => product.Discontinued);
columns.Bound(product => product.UnitPrice);
columns.Bound(product => product.UnitsOnOrder);
columns.Command(command =>
{
command.Edit();
command.Destroy();
});
})
.Pageable()
.Sortable()
.Filterable()
.Groupable()
.ClientDetailTemplateId("OrdersTemplate")
)

<script type="text/kendo" id="OrdersTemplate">
@(Html.Kendo().Grid<Person>() // Child Grid.
.Name("People#=ProductID#")
.Columns(columns =>
{
columns.Bound(o => o.PersonID);
columns.Bound(o => o.Name);
columns.Bound(o => o.BirthDate).Format("{0:d}");
columns.Command(command =>
{
command.Edit();
command.Destroy();
});
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable().Sortable().Filterable()
.AutoBind(false) // AutoBind is required to be set to false in order for the parent ID identifier to be supplemented programmatically.
.DataSource(dataSource => dataSource
.SignalR()
.AutoSync(false)
.Events(events => events.RequestEnd("onEnd2"))
.Transport(tr => tr
.ParameterMap("onMap")
.Promise("childGridConnectionStart")
.Hub("childGridConnection")
.Client(c => c
.Read("read")
.Create("create")
.Update("update")
.Destroy("destroy")
)
.Server(s => s
.Read("read")
.Create("create")
.Update("update")
.Destroy("destroy")
)
)
.Schema(schema => schema
.Model(model =>
{
model.Id("PersonID");
model.Field("PersonID", typeof(int)).Editable(false);
model.Field("Name", typeof(string));
model.Field("BirthDate", typeof(DateTime));
}))
.PageSize(10)
)
.ToClientTemplate()
)
</script>

<script>
var parentId; // Flag variable for Parent ID unique identifier.
function onDetailInit(e) { // Detail Init is required.
var dataItem = $("#grid").getKendoGrid().dataItem(e.masterRow);
parentId = dataItem.ProductID; // Gather the Parent id unique identifier.

$(e.detailRow).find(".k-grid").getKendoGrid().dataSource.read();
}
function onMap(data, type) {
switch (type) {
case "read": {
return readParameterMap(data, type, parentId)
}
default: {
return data;
}
}
}

function onEnd(e) { // Request End for Parent Grid.
if (e.type == "destroy") {
console.log("here");
$("#notification").getKendoNotification().show("Delete operation has occured for the parent Grid", "success")
} else if (e.type == "update") {
$("#notification").getKendoNotification().show("Update operation has occured for the parent Grid", "success")
} else if (e.type == "create") {
$("#notification").getKendoNotification().show("Create operation has occured for the parent Grid", "success")
}
}


function onEnd2(e) { // Request End for Child Grid.
if (e.type == "destroy") {
$("#notification").getKendoNotification().show("Delete operation has occured for the child Grid", "success")
} else if (e.type == "update") {
$("#notification").getKendoNotification().show("Update operation has occured for the child Grid", "success")
} else if (e.type == "create") {
$("#notification").getKendoNotification().show("Create operation has occured for the child Grid", "success")
}
}

function readParameterMap(data, operation, id) { // Paremeter Map handler for child Grid.
return { ProductID: id };
}
</script>