Multiple child grid elements

Hi, im need some help with child elements of a grid.

Im working on a proyect wich in one element, has multiple grids. Let me show a little example, this is my grid code:

<ejs-grid id="Grid" dataSource="@ViewBag.Detalle" allowFiltering="true" height="500" detailDataBound="detailDataBound" detailTemplate="#detailtemplate1">

    <e-grid-filterSettings type="CheckBox"></e-grid-filterSettings>

    <e-grid-columns>

        <e-grid-column field="Info.Id" headerText="Id" width="40" textAlign="Left"></e-grid-column>

        <e-grid-column field="Info.name" headerText="Nombre" width="40" textAlign="Left"></e-grid-column>

        <e-grid-column field="Info.targets" headerText="Targets" width="40" textAlign="Left"></e-grid-column>

        <e-grid-column field="Info.status" headerText="Estado" width="40" textAlign="Left"></e-grid-column>

        <e-grid-column field="Info.scan_type" headerText="Scan Type" width="40" textAlign="Left"></e-grid-column>

    </e-grid-columns>

</ejs-grid>

<ejs-scripts></ejs-scripts>


But that grid shows only 1 element. That element has a lot of objects inside and im using tabs and nested childs with this script:

<script type="text/x-template" id="detailtemplate1">

    <div id="childGrids">

        <div class="e-tab-header">

            <div>Severity Base Selections</div>

            <div>Filters </div>

            <div>Summary</div>

        </div>

        <div class="e-content">

            <div>

                <div class="detalleSeverity"></div>

            </div>

            <div>

                <div class="detalleFiltros"></div>

            </div>

            <div>

                <div class="detalleSummary"></div>

            </div>

        </div>

    </div>

</script>


<script>

    function detailDataBound(e) {

        var ordersDataSource = @Html.Raw(JsonConvert.SerializeObject(lista));

        // Crear el Grid de Syncfusion

        var detalleSeverity = new ej.grids.Grid({

            dataSource: ordersDataSource[0].info.severity_base_selections, // Usar los datos de "filters" directamente

            // toolbar: ['Severity base selections'], // Encabezado personalizado

            columns: [

                { field: "Id", headerText: "ID", width: 80 },

                { field: "display", headerText: "Nombre", width: 200 },

                { field: "value", headerText: "Valor", width: 100 }

            ]

        });

        var detalleFiltros = new ej.grids.Grid({

            dataSource: ordersDataSource[0].filters, // Usar los datos de "filters" directamente

            // toolbar: ['Filters'], // Encabezado personalizado

            columns: [

                { field: 'Id', headerText: 'ID', width: 100 },

                { field: 'name', headerText: 'Nombre', width: 150 },

                { field: 'readable_name', headerText: 'Nombre legible', width: 200 }

            ]

        });

        var detalleSummary = new ej.grids.Grid({

            dataSource: [ordersDataSource[0].summary.policy_settings], // Usar los datos de "filters" directamente

            // toolbar: ['Filters'], // Encabezado personalizado

            columns: [

                { field: 'Id', headerText: 'ID', width: 100 },

                { field: 'Whoami', headerText: 'Ejecutado por', width: 150 },

                { field: 'name', headerText: 'Nombre', width: 200 }

            ],

            childGrid: {

                dataSource: [ordersDataSource[0].summary.stats],

                queryString: 'Id',

                columns: [

                    { field: 'Id', headerText: 'Id', textAlign: 'Right', width: 90 },

                    { field: 'hostMedScanTime', headerText: 'hostMedScanTime', width: 100 },

                    { field: 'hostCredsSuccessCount', headerText: 'hostCredsSuccessCount', width: 100 },

                    { field: 'hostCredsWithoutCount', headerText: 'hostCredsWithoutCount', width: 100 },

                    { field: 'hostMaxScanTime', headerText: 'hostMaxScanTime', textAlign: 'Right', width: 90 },

                    { field: 'hostMaxScanTimeHosts', headerText: 'hostMaxScanTimeHosts', width: 100 },

                    { field: 'hostCredsFailedCount', headerText: 'hostCredsFailedCount', width: 100 },

                    { field: 'hostAvgScanTime', headerText: 'hostAvgScanTime', width: 100 }

                ],

            },

            childGrid: {

                dataSource: [ordersDataSource[0].summary.families],

                queryString: 'Id',

                columns: [

                    { field: 'Id', headerText: 'Id', textAlign: 'Right', width: 90 },

                    { field: 'name', headerText: 'name', width: 100 },

                    { field: 'status', headerText: 'status', width: 100 }

                ],

            }

        });

        detalleSummary.appendTo(e.detailElement.querySelector('.detalleSummary'));

        detalleSeverity.appendTo(e.detailElement.querySelector('.detalleSeverity'));

        detalleFiltros.appendTo(e.detailElement.querySelector('.detalleFiltros'));


        var tabObj = new ej.navigations.Tab({

            heightAdjustMode: ""

        });

        tabObj.appendTo('#childGrids');

    }

</script>


The code marked as bold, for one element of the nested grid has two child elements but i never can see the data. If i use only child element works fine.

What im doing wrong? because in the documentation says multiple child elements.


Anyone can help me? thanks!


3 Replies

VK Vasanthakumar K Syncfusion Team December 10, 2024 01:55 PM UTC

Hi Bryan,


Greetings from Syncfusion support


After reviewing the information you provided, it appears that two childGrid definitions have been specified for a single Grid. We would like to clarify that if an object contains duplicate keys, the last defined key takes precedence. This occurs because, in JavaScript, properties with the same key are overwritten during object creation. This behavior is inherent to JavaScript. Please refer to the screenshot below for further clarification.



By default, the EJ2 Grid allows defining a single childGrid configuration for the parent Grid. Within this childGrid configuration, you can specify a nested childGrid to display the Grid in a hierarchical structure. For more details, please refer to the code example and documentation below.


var detailtwo = new ej.grids.Grid({

    dataSource: childtwo,

    childGrid: {

        queryString: 'Id',

        dataSource: childthree,

        //inside the childGrid definition you can define the nested childGrid to display the Grid in hierarchy structure

        childGrid: {

            dataSource: childfour,

            queryString: 'Id'

        }

    }

           

});


Documentation: https://ej2.syncfusion.com/aspnetcore/documentation/grid/hierarchy-grid


API: https://help.syncfusion.com/cr/aspnetcore-js2/Syncfusion.EJ2.Grids.Grid.html#Syncfusion_EJ2_Grids_Grid_ChildGrid


If you would like to display two childGrids separately within a parent Grid instead of using a hierarchical structure, we recommend using the detailTemplate feature to achieve this. For your convenience, we have prepared a sample based on the information you provided, which demonstrates how to display the two Grids using the detailTemplate feature. Please refer to the code example and sample below for further details.


Index.cshtml

 

<script id='detailtemplate' type="text/x-template">

    <div id="childGrids">

        <div class="e-tab-header">

            <div>Severity Base Selections</div>

            <div>Filters </div>

        </div>

        <div class="e-content">

            <div>

                <div class="detalleSeverity"></div>

            </div>

            <div>

                <div class="detalleFiltros"></div>

            </div>

        </div>

    </div>

</script>

 

<script id='displaytwoGrids' type="text/x-template">

    <div class='custom-gridone'></div>

     <div class='custom-gridtwo'></div>

</script>

 

 

 

<script>

   

    function detailDataBound(e) {

        var childone = @Html.Raw(Json.Serialize(ViewBag.child));

        var childtwo = @Html.Raw(Json.Serialize(ViewBag.childtwo));

        var childthree = @Html.Raw(Json.Serialize(ViewBag.childthree));

        var childfour = @Html.Raw(Json.Serialize(ViewBag.childfour));

 

            var detailone = new ej.grids.Grid({

                dataSource: childone,

                columns: [

                    { field: 'OrderID', headerText: 'Order ID', width: 110 },

                    { field: 'EmployeeID', headerText: 'Employee ID', width: 140 },

                    { field: 'FirstName', headerText: 'First Name', width: 140 },

                ]

            });

            detailone.appendTo(e.detailElement.querySelector('.detalleSeverity'));

 

        var detailtwo = new ej.grids.Grid({

            dataSource: childtwo,

            detailTemplate: '#displaytwoGrids',

            detailDataBound: displaytwoGriddetaildataBound

           

        });

        detailtwo.appendTo(e.detailElement.querySelector('.detalleFiltros'));

 

        function displaytwoGriddetaildataBound(args) {

            var detailChildone = new ej.grids.Grid({

                dataSource: childthree

 

            });

            detailChildone.appendTo(e.detailElement.querySelector('.custom-gridone'));

 

            var detailChildtwo = new ej.grids.Grid({

                dataSource: childfour

 

            });

            detailChildtwo.appendTo(e.detailElement.querySelector('.custom-gridtwo'));

        }

 

        var tabObj = new ej.navigations.Tab({

 

            heightAdjustMode: ""

 

        });

 

        tabObj.appendTo('#childGrids');

    }

</script>


Note: The code above which was highlighted in bold is used to render two grids within a detailTemplate inside the FILTERS tab.


Sample: Check the attachment.


Documentation: https://ej2.syncfusion.com/aspnetcore/documentation/grid/row/detail-template#rendering-custom-component


Screenshot:



Regards,

Vasanthakumar K


Attachment: 664432,195451sample_5901879c.zip


BF Bryan Formento December 10, 2024 08:13 PM UTC

Hi Vasanthakumar


Thank you for your help, with that script and explain the problem has been solved.

Now i have another question, if i want to export to Excel/PDF how i can export all the grids maked with detailTemplate?


Thank you for your time.



VK Vasanthakumar K Syncfusion Team December 16, 2024 01:58 PM UTC

Hi Bryan Formento,


Query: How can I export all the n-nested level grids made with a detailTemplate to Excel/PDF?


We would like to clarify that the Syncfusion Grid currently supports exporting a detailed structure to Excel or PDF only for one detail level. The exportDetailTemplate event is triggered only during the export of the parent row, and it doesn't automatically recognize your custom user template. This is because the grid is unaware of the custom components you might use inside the detail template.


Essentially, the grid's detail template expansion must be customized via the exportDetailTemplate event, as mentioned in the documentation, but it only supports one detail level at a time. This requirement is only possible on default hierarchy relational binding alone. But you are rendering nested level multiple grids without any relation (relational binding hierarchy grid) to the parent grid. So, unfortunately, it does not support exporting multiple nested levels (or grids) within the detail template for Excel/PDF export.


Important Notes:


  • Hierarchy Relational binding in Syncfusion supports only one n-level nested grid rendering, not multiple grids on a single detail row.
  • Exporting multiple nested levels (beyond the first level) in the Excel/PDF format is not currently supported unless you are using a default hierarchical relational binding with one grid at the detail level.


Regards,

Vasanthakumar K


Loader.
Up arrow icon