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

TreeGrid - Determine Level of Selected Record

Hi,

I have gone through the API of the TreeGrid in some detail and have not been able to work out how to determine the level of the currently selected record.

An example of how I would use this is that I would like to be able to select a record and know that it is at (say) Level 2 and then be able to collapse all records to the same level.

Can you please advise if it is possible to determine the indent level of a particular record?


4 Replies 1 reply marked as answer

PS Pon Selva Jeganathan Syncfusion Team October 6, 2022 11:13 PM

Hi Greg,


Thanks for contacting syncfusion forum.


We are working on this query with high priority. We need time to validate the issue. So, we will update further details on or before 10th October 2022. Until then we value your patience. In the meanwhile, we will contact if any details are required.


Regards,
Pon selva



PS Pon Selva Jeganathan Syncfusion Team October 10, 2022 01:24 PM

Hi Greg


Thanks for contacting syncfusion forum.


Query: if it is possible to determine the indent level of a particular record?


Based on your query, we understand you need to find the level of the selected record. We achieved your requirement by using the GetSelectedRecordsAsync method and the DataSource property of the treegrid.


Please refer to the below code example


<SfButton @ref="ToggleBtn" @onclick="onToggleClick" Content="@Content"></SfButton>

 

 

<SfTreeGrid @ref="TreeGrid" DataSource="@TreeData" AllowSelection=true IdMapping="TaskId" ParentIdMapping="ParentId" TreeColumnIndex="1">

    <……

</SfTreeGrid>

 

 

private void onToggleClick(Microsoft.AspNetCore.Components.Web.MouseEventArgs args)

    {

 

        //Here you can get the selected record using the GetSelectedRecordsAsync method

        var selected_primarykey = TreeGrid.GetSelectedRecordsAsync().Result[0];

 

      

        //Here find the all parent key value from datasource

        var All_Parent_keys = TreeGrid.DataSource.GroupBy(rec => rec.ParentId).Where(g => g.Key != null).ToDictionary(g => g.Key?.ToString(), g => g.ToList()).Keys.ToList();

       

        //Here find all the zeroth level parent key value from datasource

        var root_key = TreeGrid.DataSource.Where(rec => rec.ParentId == null).ToDictionary(rec =>rec.TaskId.ToString(), rec=>rec.TaskId.ToString()).Keys.ToList();

 

         if (All_Parent_keys.Contains(selected_primarykey.TaskId.ToString()))

        {

            //Here check selected primary key value is in the zeroth level key value or not

            if (root_key.Contains(selected_primarykey.TaskId.ToString()))

            {

 

                Console.WriteLine($" >>>> Selected level is 0");

            }

            else

            {

                Console.WriteLine($" >>>> Selected level is 1");

            }

 

        }

 

        else

        {

            //Here check the selected parentid value is in the zeroth level key value or not

            if (root_key.Contains(selected_primarykey.ParentId.ToString()))

            {

                Console.WriteLine($" >>>> Selected level is 1");

 

            }

            else

            {

 

                Console.WriteLine($" >>>> Selected level is 2");

            }

        }

        // var recs = TreeGrid.GetRows();

    }


In the above code example, first we get the selected record value. And we find the zeroth level and all parent key values using the ParentIdMapping property value. Based on this key value, we found the level of the selected records.


Please refer to the below sample,

https://www.syncfusion.com/downloads/support/directtrac/general/ze/TREEGR~1-254458926


Kindly get back to us for further assistance.


Regards,
Pon selva


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.








Marked as answer

GW Greg Wruck October 10, 2022 04:41 PM

Thanks for the reply Pon.  I was actually hoping that there might be an inbuilt function to do it.  I had already built a similar solution to yours to traverse the hierarchy, but it is quite inefficient and was hoping that there was an optimized way of doing it.



PS Pon Selva Jeganathan Syncfusion Team October 11, 2022 09:19 AM


Hi Greg


Thanks for the update.


We have considered this as an improvement feature from our end and logged task for the same as Need to provide support for public access to the level, index, and child record values. Thank you for taking the time to report this issue and helping us improve our product. At Syncfusion, we are committed to fixing all validated defects (subject to technical feasibility and Product Development Life Cycle) and this improvement feature will be included in our “Volume 1 2023” Main release which is expected to be rolled out in March 2023.


You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through the below feedback link, 

  

Feedback link: https://www.syncfusion.com/feedback/38346/need-to-provide-support-for-public-access-to-the-level-index-and-child-record


Please get back to us if you need any further assistance.


Regards,
Pon selva





Loader.
Live Chat Icon For mobile
Up arrow icon