<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Batch_Hierarchy.aspx.cs" Inherits="Batch_Hierarchy" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            <Scripts>
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
            </Scripts>
        </telerik:RadScriptManager>

        <script>

            function ChangeDiscountFirstRow(sender, args) {
                var grid = $find('<%=  RadGrid1.ClientID %>');
                var batMan = grid.get_batchEditingManager();
                var customersTable = grid.get_masterTableView();
                var customers = customersTable.get_dataItems();

                if (customers.length > 0) {

                    var firstCustomer = customers[0];

                    var childTables = firstCustomer.get_nestedViews();

                    for (var childIndex = 0; childIndex < childTables.length; childIndex++) {
                        if (childTables[childIndex].get_name() == "Orders") {
                            var ordersTable = childTables[childIndex];

                            var orders = ordersTable.get_dataItems();

                            if (orders.length > 0) {
                                var firstOrder = orders[0];

                                var grandChildTables = firstOrder.get_nestedViews();

                                for (var grandChildIndex = 0; grandChildIndex < grandChildTables.length; grandChildIndex++) {

                                    if (grandChildTables[grandChildIndex].get_name() == "OrderDetails") {

                                        var orderDetailsTable = grandChildTables[grandChildIndex];

                                        var orderDetails = orderDetailsTable.get_dataItems();

                                        for (var orderDetailIndex = 0; orderDetailIndex < orderDetails.length; orderDetailIndex++) {
                                            var currentOrderDetail = orderDetails[orderDetailIndex];

                                            var discountCell = currentOrderDetail.get_cell("Discount");

                                            batMan.changeCellValue(discountCell, 4);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            function ChangeDiscountAllRows(sender, args) {
                var grid = $find('<%=  RadGrid1.ClientID %>');
                var batMan = grid.get_batchEditingManager();
                var customersTable = grid.get_masterTableView();
                var customers = customersTable.get_dataItems();


                for (var customerIndex = 0; customerIndex < customers.length; customerIndex++) {

                    var currentCustomer = customers[customerIndex];

                    var childTables = currentCustomer.get_nestedViews();

                    for (var childIndex = 0; childIndex < childTables.length; childIndex++) {
                        if (childTables[childIndex].get_name() == "Orders") {

                            var ordersTable = childTables[childIndex];

                            var orders = ordersTable.get_dataItems();

                            for (var orderIndex = 0; orderIndex < orders.length; orderIndex++) {
                                var currentOrder = orders[orderIndex];

                                var grandChildTables = currentOrder.get_nestedViews();

                                for (var grandChildIndex = 0; grandChildIndex < grandChildTables.length; grandChildIndex++) {

                                    if (grandChildTables[grandChildIndex].get_name() == "OrderDetails") {

                                        var orderDetailsTable = grandChildTables[grandChildIndex];

                                        var orderDetails = orderDetailsTable.get_dataItems();

                                        for (var orderDetailIndex = 0; orderDetailIndex < orderDetails.length; orderDetailIndex++) {
                                            var currentOrderDetail = orderDetails[orderDetailIndex];

                                            var discountCell = currentOrderDetail.get_cell("Discount");

                                            batMan.changeCellValue(discountCell, 4);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }


            function CheckMainRows(sender, args) {
                var grid = $find('<%=  RadGrid1.ClientID %>');
                var customersTable = grid.get_masterTableView();
                var customers = customersTable.get_dataItems();

                for (var customerIndex = 0; customerIndex < customers.length; customerIndex++) {
                    var currentCustomer = customers[customerIndex];

                    var childTables = currentCustomer.get_nestedViews();



                    var chkBox = $telerik.findControl(currentCustomer.get_cell("TemplateColumn"), "RadCheckBox1");

                    chkBox.set_checked(true);
                }
            }

            function CheckChildRows(sender, args) {
                var grid = $find('<%=  RadGrid1.ClientID %>');
                var customersTable = grid.get_masterTableView();

                var customers = customersTable.get_dataItems();

                for (var customerIndex = 0; customerIndex < customers.length; customerIndex++) {

                    var currentCustomer = customers[customerIndex];
                    var childTables = currentCustomer.get_nestedViews();

                    for (var childIndex = 0; childIndex < childTables.length; childIndex++) {
                        if (childTables[childIndex].get_name() == "Orders") {
                            var ordersTable = childTables[childIndex];

                            var orders = ordersTable.get_dataItems();

                            for (var orderIndex = 0; orderIndex < orders.length; orderIndex++) {
                                var currentOrder = orders[orderIndex];

                                var chkBox = $telerik.findControl(currentOrder.get_cell("TemplateColumn"), "RadCheckBox1");
                                chkBox.set_checked(true);
                            }
                        }
                    }
                }
            }

            function CheckGrandChildRows(sender, args) {
                var grid = $find('<%=  RadGrid1.ClientID %>');
                var customersTable = grid.get_masterTableView();

                var customers = customersTable.get_dataItems();

                for (var customerIndex = 0; customerIndex < customers.length; customerIndex++) {

                    var currentCustomer = customers[customerIndex];

                    var childTables = currentCustomer.get_nestedViews();

                    for (var childIndex = 0; childIndex < childTables.length; childIndex++) {
                        if (childTables[childIndex].get_name() == "Orders") {

                            var ordersTable = childTables[childIndex];

                            var orders = ordersTable.get_dataItems();

                            for (var orderIndex = 0; orderIndex < orders.length; orderIndex++) {
                                var currentOrder = orders[orderIndex];

                                var grandChildTables = currentOrder.get_nestedViews();

                                for (var grandChildIndex = 0; grandChildIndex < grandChildTables.length; grandChildIndex++) {

                                    if (grandChildTables[grandChildIndex].get_name() == "OrderDetails") {

                                        var orderDetailsTable = grandChildTables[grandChildIndex];

                                        var orderDetails = orderDetailsTable.get_dataItems();

                                        for (var orderDetailIndex = 0; orderDetailIndex < orderDetails.length; orderDetailIndex++) {
                                            var orderDetail = orderDetails[orderDetailIndex];

                                            var chkBox = $telerik.findControl(orderDetail.get_cell("TemplateColumn"), "RadCheckBox1");
                                            chkBox.set_checked(true);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        </script>


        <h4>Change the Discount of all items in the Innermost Table (Order Details) of the First Row</h4>
        <telerik:RadButton runat="server" ID="RadButton1" Text="Change" AutoPostBack="false" OnClientClicked="ChangeDiscountFirstRow" />
        <h4>Change the Discount of all items in the Innermost Table (Order Details) of all Rows</h4>
        <telerik:RadButton runat="server" ID="RadButton2" Text="Change" AutoPostBack="false" OnClientClicked="ChangeDiscountAllRows" />

        <h4>Check all CheckBoxes in the main Table Rows</h4>
        <telerik:RadButton runat="server" ID="RadButton3" Text="Check Customers" AutoPostBack="false"  OnClientClicked="CheckMainRows"/>

       <h4>Check all CheckBoxes in the Child Table Rows</h4>
        <telerik:RadButton runat="server" ID="RadButton4" Text="Check Orders" AutoPostBack="false" OnClientClicked="CheckChildRows" />

        <h4>Check all CheckBoxes in the Grand Child Table Rows</h4>
        <telerik:RadButton runat="server" ID="RadButton5" Text="Check Order Details" AutoPostBack="false" OnClientClicked="CheckGrandChildRows" />

        <br />
        <br />

        <telerik:RadGrid ID="RadGrid1" runat="server" RenderMode="Lightweight" OnBatchEditCommand="RadGrid1_BatchEditCommand"
            OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
            AutoGenerateDeleteColumn="true"
            OnNeedDataSource="RadGrid1_NeedDataSource">

            <MasterTableView Name="Customers" DataKeyNames="CustomerID" Caption="Customers Table" EditMode="Batch" CommandItemDisplay="Top" RetainExpandStateOnRebind="true" HierarchyDefaultExpanded="true" HierarchyLoadMode="Client">
                <CommandItemSettings ShowSaveChangesButton="true" />
                <DetailTables>
                    <telerik:GridTableView Name="Orders" DataKeyNames="OrderID" Caption="Orders Table" EditMode="Batch" CommandItemDisplay="Top" RetainExpandStateOnRebind="true" HierarchyDefaultExpanded="true" HierarchyLoadMode="Client">
                        <CommandItemSettings ShowSaveChangesButton="true" />
                        <DetailTables>
                            <telerik:GridTableView Name="OrderDetails" DataKeyNames="ProductID" Caption="Order Details Table" EditMode="Batch" CommandItemDisplay="Top" RetainExpandStateOnRebind="true" HierarchyDefaultExpanded="true" HierarchyLoadMode="Client">
                                <CommandItemSettings ShowSaveChangesButton="true" />
                                <Columns>
                                    <telerik:GridTemplateColumn UniqueName="TemplateColumn">
                                        <ItemTemplate>
                                            <telerik:RadCheckBox ID="RadCheckBox1" runat="server" Text="RadCheckBox" AutoPostBack="false"></telerik:RadCheckBox>
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                </Columns>
                            </telerik:GridTableView>
                        </DetailTables>

                        <Columns>
                            <telerik:GridTemplateColumn UniqueName="TemplateColumn">
                                <ItemTemplate>
                                    <telerik:RadCheckBox ID="RadCheckBox1" runat="server" Text="RadCheckBox" AutoPostBack="false"></telerik:RadCheckBox>
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                        </Columns>
                    </telerik:GridTableView>
                </DetailTables>
                <Columns>
                    <telerik:GridTemplateColumn UniqueName="TemplateColumn">
                        <ItemTemplate>
                            <telerik:RadCheckBox ID="RadCheckBox1" runat="server" Text="RadCheckBox" AutoPostBack="false"></telerik:RadCheckBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
    </form>
</body>
</html>
