You can show any level of nested subgrids (master detail) inside a parent grid. Just set the HierarchySettings.HierarchyMode property of the parent grid to "Parent" for the parent grid and to "Child" for the child ones. If you have a grid that is a parent and a child at the same time (e.g. the second level grid in a 3 level hierarchy), set its HierarchySettings.HierarchyMode property to "ParentAndChild".
Then, hook the client-side ClientSideEvents.OnClientSubGridRowExpanded event of the grid and call a special javascript function, which is autogenerated by child grids and is called showSubGrid_{GridID}, where {GridID} is the ID of the child grid, e.g. showSubGrid_JQGrid2
It's as simple as that, this will in essense trigger the sever-side OnDataRequesting of the grid and you can get the ID of the parent row and bind the child grid accordingly.
Complete hierarchy is achieved in this example with just 2 lines of code - 1 javascript and 1 C# (check the ASPX and C# tabs for details).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Trirand.Web.Mvc;
using System.Web.UI.WebControls;
namespace JQGridMVCExamples.Models
{
public class TwoLevelHierarchyJqGridModel
{
public JQGrid CustomersGrid { get; set; }
public JQGrid OrdersGrid { get; set; }
public TwoLevelHierarchyJqGridModel()
{
CustomersGrid = new JQGrid
{
Columns = new List()
{
new JQGridColumn { DataField = "CustomerID",
HeaderText = "ID",
PrimaryKey = true,
Width = 50 },
new JQGridColumn { DataField = "CompanyName" },
new JQGridColumn { DataField = "ContactName" },
new JQGridColumn { DataField = "Phone" },
new JQGridColumn { DataField = "City" }
},
Width = Unit.Pixel(650),
Height = Unit.Pixel(350)
};
OrdersGrid = new JQGrid
{
Columns = new List()
{
new JQGridColumn { DataField = "OrderID",
// always set PrimaryKey for Add,Edit,Delete operations
// if not set, the first column will be assumed as primary key
PrimaryKey = true,
Editable = false,
Width = 50 },
new JQGridColumn { DataField = "OrderDate",
Editable = true,
Width = 100,
DataFormatString = "{0:d}" },
new JQGridColumn { DataField = "CustomerID",
Editable = true,
Width = 50 },
new JQGridColumn { DataField = "Freight",
Editable = true,
Width = 75 },
new JQGridColumn { DataField = "ShipName",
Editable = true,
Width = 100
}
},
Width = Unit.Pixel(450),
Height = Unit.Percentage(100)
};
}
}
}
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<JQGridMVCExamples.Models.TwoLevelHierarchyJqGridModel>" %>
<%@ Import Namespace="Trirand.Web.Mvc" %>
<%@ Import Namespace="JQGridMVCExamples.Models" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Two level hierarchy</title>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://www.trirand.net/aspnetmvc/Scripts/trirand/i18n/grid.locale-en.js"></script>
<script type="text/javascript" src="http://www.trirand.net/aspnetmvc/Scripts/trirand/jquery.jqGrid.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/themes/redmond/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="http://www.trirand.net/aspnetmvc/Content/themes/ui.jqgrid.css" />
</head>
<body>
<div>
<!-- The parent grid - Customers -->
<%= Html.Trirand().JQGrid(Model.CustomersGrid, "CustomersGrid") %>
<!-- The child grid - Orders for each parent customers -->
<%= Html.Trirand().JQGrid(Model.OrdersGrid, "OrdersGrid") %>
<script type="text/javascript">
function showOrdersSubGrid(subgrid_id, row_id) {
// the "showSubGrid_OrdersGrid" function is autogenerated and available globally on the page by the second child grid.
// Calling it will place the child grid below the parent expanded row and will call the action specified by the DaraUrl property
// of the child grid, with ID equal to the ID of the parent expanded row
showSubGrid_OrdersGrid(subgrid_id, row_id);
}
</script>
<br /><br />
<span style="font-size: 140%; font-family: Tahoma; font-weight: bold;">Hierarchy Settings:</span>
<hr />
<% Html.BeginForm("hierarchytwolevels", "grid"); %>
<span style="font-size: 130%; font-family: Tahoma;">
<table cellpadding="2" cellspacing="2" border="0">
<tr>
<td>Reload subgrid on expand</td>
<td><%= Html.CheckBox("reloadOnExpand", (bool)ViewData["reloadOnExpand"], new { onchange = "this.form.submit();" })%></td>
<td>Plus icon</td>
<td>
<%= Html.DropDownList(
"plusIcon",
new SelectList(
new List<SelectListItem>() {
new SelectListItem { Value = "ui-icon-plus" },
new SelectListItem { Value = "ui-icon-plusthick" },
new SelectListItem { Value = "ui-icon-carat-1-e" },
new SelectListItem { Value = "ui-icon-triangle-1-e" }
}, "Value", "Value", ViewData["plusIcon"]),
new { onchange="this.form.submit();" })%>
</td>
</tr>
<tr>
<td>Auto expand child rows on load</td>
<td><%= Html.CheckBox("expandOnLoad", (bool)ViewData["expandOnLoad"], new { onchange = "this.form.submit();" })%></td>
<td>Minus icon</td>
<td>
<%= Html.DropDownList(
"minusIcon",
new SelectList(
new List<SelectListItem>() {
new SelectListItem { Value = "ui-icon-minus" },
new SelectListItem { Value = "ui-icon-minusthick" },
new SelectListItem { Value = "ui-icon-carat-1-s" },
new SelectListItem { Value = "ui-icon-triangle-1-s" }
}, "Value", "Value", ViewData["minusIcon"]),
new { onchange="this.form.submit();" })%>
</td>
</tr>
<tr>
<td>Select parent row on expand</td>
<td><%= Html.CheckBox("selectOnExpand", (bool)ViewData["selectOnExpand"], new { onchange = "this.form.submit();" })%></td>
<td>Opened child grid icon</td>
<td>
<%= Html.DropDownList(
"openIcon",
new SelectList(
new List<SelectListItem>() {
new SelectListItem { Text = "ui-icon-carat-1-sw", Value = "ui-icon-carat-1-sw" },
new SelectListItem { Text = "ui-icon-arrow-1-e", Value = "ui-icon-arrow-1-e" }
}, "Text", "Value", ViewData["openIcon"]),
new { onchange="this.form.submit();" })%>
</td>
</tr>
</table>
</span>
<% Html.EndForm(); %>
</div>
<br /><br />
<div>
<% Html.RenderPartial("CodeTabs"); %>
</div>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JQGridMVCExamples.Models;
using Trirand.Web.Mvc;
using System.Web.UI.WebControls;
namespace JQGridMVCExamples.Controllers.Grid
{
public partial class GridController : Controller
{
// This is the default action for the View. Use it to setup your grid Model.
public ActionResult HierarchyTwoLevels(bool? reloadOnExpand, bool? expandOnLoad, bool? selectOnExpand,
string plusIcon, string minusIcon, string openIcon,
int? delayOnLoad)
{
// Get the model (setup) of the grid defined in the /Models folder.
var model = new TwoLevelHierarchyJqGridModel();
// set default values for various Hierarchy settings if not posted from server (changed by user)
ViewData["reloadOnExpand"] = reloadOnExpand ?? true;
ViewData["expandOnLoad"] = expandOnLoad ?? false;
ViewData["selectOnExpand"] = selectOnExpand ?? false;
ViewData["selectOnExpand"] = selectOnExpand ?? false;
ViewData["plusIcon"] = String.IsNullOrEmpty(plusIcon) ? "ui-icon-plus" : plusIcon;
ViewData["minusIcon"] = String.IsNullOrEmpty(minusIcon) ? "ui-icon-minus" : minusIcon;
ViewData["openIcon"] = String.IsNullOrEmpty(openIcon) ? "ui-icon-carat-1-sw" : openIcon;
SetUp_TwoLevelHierarchy_Grids(model);
// Pass the custmomized grid model to the View
return View(model);
}
public JsonResult TwoLevel_CustomersDataRequested()
{
// Get both the grid Model and the data Model
// The data model in our case is an autogenerated linq2sql database based on Northwind.
var northWindModel = new NorthwindDataContext();
var model = new TwoLevelHierarchyJqGridModel();
SetUp_TwoLevelHierarchy_Grids(model);
// return the result of the DataBind method, passing the datasource as a parameter
// jqGrid for ASP.NET MVC automatically takes care of paging, sorting, filtering/searching, etc
return model.CustomersGrid.DataBind(northWindModel.Customers);
}
// parentRowID is automatically passed from the parent grid to the child grid. Note: parentRowID is case sensitive
public JsonResult TwoLevel_OrdersDataRequested(string parentRowID)
{
// Get both the grid Model and the data Model
// The data model in our case is an autogenerated linq2sql database based on Northwind.
var northWindModel = new NorthwindDataContext();
var model = new TwoLevelHierarchyJqGridModel();
SetUp_TwoLevelHierarchy_Grids(model);
var orders = from o in northWindModel.Orders
where o.CustomerID == parentRowID
select o;
return model.OrdersGrid.DataBind(orders);
}
public void SetUp_TwoLevelHierarchy_Grids(TwoLevelHierarchyJqGridModel model)
{
var customersGrid = model.CustomersGrid;
var ordersGrid = model.OrdersGrid;
customersGrid.ID = "CustomersGrid";
customersGrid.DataUrl = Url.Action("TwoLevel_CustomersDataRequested");
customersGrid.ClientSideEvents.SubGridRowExpanded = "showOrdersSubGrid";
customersGrid.Height = Unit.Percentage(100);
customersGrid.PagerSettings.PageSize = 5;
customersGrid.HierarchySettings.HierarchyMode = HierarchyMode.Parent;
customersGrid.HierarchySettings.ReloadOnExpand = Convert.ToBoolean(ViewData["reloadOnExpand"]);
customersGrid.HierarchySettings.SelectOnExpand = Convert.ToBoolean(ViewData["selectOnExpand"]);
customersGrid.HierarchySettings.ExpandOnLoad = Convert.ToBoolean(ViewData["expandOnLoad"]);
customersGrid.HierarchySettings.PlusIcon = Convert.ToString(ViewData["plusIcon"]);
customersGrid.HierarchySettings.MinusIcon = Convert.ToString(ViewData["minusIcon"]);
customersGrid.HierarchySettings.OpenIcon = Convert.ToString(ViewData["openIcon"]);
ordersGrid.ID = "OrdersGrid";
ordersGrid.DataUrl = Url.Action("TwoLevel_OrdersDataRequested");
ordersGrid.HierarchySettings.HierarchyMode = HierarchyMode.Child;
}
}
}
Switch theme:
Theming is based on the very popular jQuery
ThemeRoller standard. This is the same theming mechanism used by jQuery UI and is now a de-facto standard for jQuery based components.
The benefits of using ThemeRoller are huge. We can offer a big set of ready to use themes created by professional designers, including Windows-like themes (Redmond), Apple-like theme (Cupertino), etc.
In addition to that any jQuery UI controls on the same page will pick the same theme.
Last, but not least, you can always roll your own ThemeRoller theme, using the superb
Theme Editor
To use a theme, simply reference 2 Css files in your Html <head> section - the ThemeRoller theme you wish to use, and the jqGrid own ThemeRoller Css file. For example (Redmond theme):
<link rel="stylesheet" type="text/css" media="screen" href="/themes/redmond/jquery-ui-1.8.2.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="/themes/ui.jqgrid.css" />