Hi,
i have organization structure that needs to be display in hirachical gird. i do not know the depth. i have seem the example where three level hierarchi is define at code level. is there a way i can do this at runtime.
Is There any solution for my issue?
View:
@(Html.Infragistics().Grid(Model))
Controller:
using System.Web.Mvc;using System.Collections.Generic;using Infragistics.Web.Mvc;using Recon.Models;
namespace Recon.Controllers{ public class TestController : Controller { public ActionResult List() { GridModel grid = CreateGridModel(); return View(grid); }
private GridModel CreateGridLoadOnDemand() { GridModel grid = CreateGridModel(); grid.ID = "gridLoadOnDemand"; grid.LoadOnDemand = true; grid.DataSourceUrl = this.Url.Action("BindParent"); grid.ColumnLayouts[0].DataSourceUrl = this.Url.Action("BindChild"); grid.ColumnLayouts[0].ColumnLayouts[0].DataSourceUrl = this.Url.Action("BindInnerChild"); return grid; }
// Take grid creation outside as it will be used in all methods: private GridModel CreateGridModel() { GridModel grid = new GridModel(); // Set up properties and columns: grid.AutoGenerateColumns = false; grid.PrimaryKey = "CustomerID"; grid.Columns = new List<GridColumn>(); grid.Columns.Add(new GridColumn("Customer", "CustomerID", "string", "300px")); grid.Columns.Add(new GridColumn("Company Name", "CompanyName", "string", "150px")); grid.Columns.Add(new GridColumn("Country", "Country", "string", "150px")); grid.AutoGenerateLayouts = false;
// Create child layout GridColumnLayoutModel layout = new GridColumnLayoutModel(); layout.Key = "Orders"; layout.ForeignKey = "CustomerID"; layout.PrimaryKey = "OrderID"; layout.AutoGenerateColumns = false;
layout.Columns = new List<GridColumn>(); layout.Columns.Add(new GridColumn("Order ID", "OrderID", "number", "100px")); layout.Columns.Add(new GridColumn("Customer", "CustomerID", "string", "100px")); layout.Columns.Add(new GridColumn("Order Date", "OrderDate", "date", "150px")); layout.Columns.Add(new GridColumn("Shipped Date", "ShippedDate", "date", "150px")); // Create child layout GridColumnLayoutModel layout2 = new GridColumnLayoutModel(); layout2.Key = "Orders"; layout2.ForeignKey = "OrderID"; layout2.PrimaryKey = "SupplierID"; layout2.AutoGenerateColumns = false;
layout2.Columns = new List<GridColumn>(); layout2.Columns.Add(new GridColumn("Supplier ID", "SupplierID", "number", "100px")); layout2.Columns.Add(new GridColumn("Order ID", "OrderID", "number", "100px")); layout2.Columns.Add(new GridColumn("Supplier Name", "SupplierName", "string", "100px")); //add that layout to the grid layout.ColumnLayouts.Add(layout2); grid.ColumnLayouts.Add(layout);
// Add paging to child layouts (different from the parent's) GridPaging layoutPaging = new GridPaging(); layoutPaging.VisiblePageCount = 2; layoutPaging.PageSize = 5; //layoutPaging.Type = OpType.Remote; layout.Features.Add(layoutPaging);
//And lastly set ID so the client widget can be normally interacted with (see the events in the view) grid.ID = "hierarchicalGrid"; //most importantly turn Load On demand on and define response Urls: grid.LoadOnDemand = true; grid.DataSourceUrl = this.Url.Action("BindParent"); grid.ColumnLayouts[0].DataSourceUrl = this.Url.Action("BindChild"); grid.ColumnLayouts[0].ColumnLayouts[0].DataSourceUrl = this.Url.Action("BindInnerChild");
var dataContext = new NorthwindDataContext(); var dataSource = dataContext.Customers; var ds = dataSource.Take(30); grid.DataSource=ds;
//Add features: GridSorting sorting = new GridSorting(); sorting.Type = OpType.Remote; grid.Features.Add(sorting); GridPaging paging = new GridPaging(); paging.PageSize = 10; paging.Type = OpType.Remote; grid.Features.Add(paging);
// Return the finished model return grid; }
public JsonResult BindParent() { //create a model GridModel grid = CreateGridModel(); //set its data source var dataContext = new NorthwindDataContext(); var dataSource = dataContext.Customers; grid.DataSource = dataSource.Take(30); //and use the GetData() method to return the results return grid.GetData(); }
public JsonResult BindChild(string path, string layout) { //create a model GridModel grid = CreateGridModel(); //set its data source var dataContext = new NorthwindDataContext(); var dataSource = dataContext.Orders; grid.DataSource = dataSource; //and use the GetData() method to return the results return grid.GetData(path, layout); } public JsonResult BindInnerChild(string path, string layout) { //create a model GridModel grid = CreateGridModel(); //set its data source var dataContext = new NorthwindDataContext(); var dataSource = dataContext.Orders; grid.DataSource = dataSource; //and use the GetData() method to return the results return grid.GetData(path, layout); } }
}
Hello cmm ,
I’m just following up to see if you’ve been able to resolve your issue. If you have any questions or concerns or if you need further assistance please let me know.
Best Regards,
Maya Kirova
Developer Support Engineer
Infragistics, Inc.
http://es.infragistics.com/support
Unfortunately it’s not possible to auto generate the layouts when you use load on demand. That’s due to the limitation that the grid needs to have for each layout a method that returns the required data as is described in the documentation. However if you define the model in the controller you should be able to check the data before you’re going to pass it to the view and based on that create the needed number of child layouts.
Let’s say that you’re going to pass a data source with a 5 level hierarchy. So you should create 5 layouts for each of those and set the DataSourceUrl of each to a method that returns a Json result needed for each.
For example as is described in the documentation
For the parent:
public JsonResult BindParent()
{
var ctx = new AdventureWorksDataContext(@"ConnString");
var ds = ctx.Products.Take(3);
grid.DataSource = ds;
return grid.GetData();
For the child:
public JsonResult BindChild(string path, string layout)
var ds = ctx.ProductInventories;
return grid.GetData(path, layout);
Where the “path” and “layout” get passed from the client side when you expand a parent row. The grid’s GetData method of the grid will manage the getting of the specific data for each such request.
All this is explained in more details in the link I previously referred to.
Let me know if you have any questions.
Developer Support Engineer II
Hi ,
i undestand this example. but this s for two level Hierarch. but i have n -level Hierarch. e.g.
Org1-->ORG3-->org4-->org5...so on.
ORg2-->org6-->org7
ORg9
in case when i do not know the bindingdepth from the begining how will i do it. cuase all them example requrires, defining column layout or all level upfront.
thank you