@using Nested_WebGrid_MVC.Models
@model IEnumerable<CustomerModel>
@{
Layout = null;
WebGrid webGrid = new WebGrid(source: Model, canSort: false, canPage: false);
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
.Grid { border: 1px solid #ccc; border-collapse: collapse; background-color: #fff; }
.Grid th { background-color: #B8DBFD; color: #333; font-weight: bold; }
.Grid th, .Grid td { padding: 5px; border: 1px solid #ccc; }
.Grid img { cursor:pointer; }
.ChildGrid { width: 100%; }
.ChildGrid th { background-color: #6C6C6C; color: #fff; font-weight: bold; }
</style>
</head>
<body>
@webGrid.GetHtml(
htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
columns: webGrid.Columns(
webGrid.Column(null, null, format: @<text><img src="~/Images/plus.png"/><div style="display:none"></div></text>),
webGrid.Column("ContactName", "Contact Name"),
webGrid.Column("City", "City"),
webGrid.Column(format: (item) =>
{
WebGrid childGrid = new WebGrid(source: item.Orders, canSort: false, canPage: false);
return childGrid.GetHtml(
htmlAttributes: new { @class = "ChildGrid" },
columns: childGrid.Columns(
childGrid.Column("OrderId", "OrderId"),
childGrid.Column("OrderDate", "OrderDate")
));
})
))
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
//Loop through all Child Grids.
$("#WebGrid .ChildGrid").each(function () {
//Copy the Child Grid to DIV.
var childGrid = $(this).clone();
$(this).closest("TR").find("TD").eq(0).find("DIV").append(childGrid);
//Remove the Last Column from the Row.
$(this).parent().remove();
});
//Remove Last Column from Header Row.
$("#WebGrid TH:last-child").eq(0).remove();
});
//Assign Click event to Plus Image.
$("body").on("click", "img[src*='plus.png']", function () {
$(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>");
$(this).attr("src", "/images/minus.png");
});
//Assign Click event to Minus Image.
$("body").on("click", "img[src*='minus.png']", function () {
$(this).attr("src", "/images/plus.png");
$(this).closest("tr").next().remove();
});
</script>
</body>
</html>