We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

An array within an array data source

Hi Team,

I have a data source that is an array within array. I want the array to be the data source of the parent grid and the inner array to the data source of the child grid. Is that possible to do?


Thanks.

3 Replies

RU Ragavee U S Syncfusion Team October 2, 2015 10:35 AM UTC

Hi Lory,

We can achieve your requirement using the detailsExpand event of the parent grid.

In the detailsExpand event of the grid, we can get the inner array object corresponding to the parent record and assign that data to the childGrid in the create event of the childgrid.

Please refer to the following code example.

    <script type="text/javascript">

        var data = [

             { OrderID: 1, EmployeeName: 'James', Details: [{ "CustomerName": "Sam", CustomerID: 1 }, { "CustomerName": "Nancy", CustomerID: 2 }] },

          . . .

        ]

        $(function () {

            $("#Grid").ejGrid({

                dataSource: data,

                . . . .

                detailsExpand: expandDetails,

                childGrid: {

                    dataSource: [],

                    queryString: "EmployeeID",

                    allowPaging: true,

                    create: 'childCreate',

                    . . . .

                },

            });


        });

//detail expand event of the parent grid

        function expandDetails(args) {

            window.childData = args.masterData.Details; //get the inner array object and store in a variable

        }


//create event of the childgrid

        function childCreate(args) {

            this.dataSource(window.childData); //assigning the child grid dataSource using the dataSource method of the grid

        }


    </script>


For your convenience, we have created a sample with the above solution, which can be downloaded from the below location.

Sample Link: http://jsplayground.syncfusion.com/05l5riis

Regards,
Ragavee U S.


LO Lory October 8, 2015 04:54 PM UTC

That's great. It is exactly what I required! Follow up question, how do I disable or remove the expansion of a row if the inner array is empty?

Thanks.



SR Sellappandi Ramu Syncfusion Team October 9, 2015 05:58 AM UTC

Hi Lorry,

Thanks for the update.

We have created a sample to hide the details row if it has the empty array by using detailsExpand event.

Please refer to the below document for detailsExpand event, 

http://help.syncfusion.com/js/api/ejgrid#events:detailsexpand

Please find the code example and sample.

        $(function () {

            $("#Grid").ejGrid({

                dataSource: data,

                allowSorting: true,

                columns: [

                       ….

                ],

                detailsExpand: expandDetails,

                childGrid: {

                    dataSource: [],

                    queryString: "EmployeeID",

                    allowPaging: true,

                    create: 'childCreate',

                    columns: [

                                ….


                    ]

                },


            });


        });

        function expandDetails(args) {

            window.childData = args.masterData.Details;

            if (window.childData.length == 0) {

                args.cancel = true;

                args.detailsRow.hide();

                if ($(args.masterRow.context).hasClass("e-icon"))

                    args.masterRow.context.remove();

            }

        }

                ….


    </script>


Sample Link: http://jsplayground.syncfusion.com/wmbyiu1s

Regards,
Sellappandi R

Loader.
Live Chat Icon For mobile
Up arrow icon