Select Row by Index or ID with data by Custom Adaptor

Hello,

I get the data by Custom Adaptor. The controller is creating the content with entiy framework and doing the filtering, sorting and paging. The treegrid is bind with self-referential uuid. Paging is enabled!

I want to reselect the last selected row after I reenter the page - the last selected uuid is stored in the adaptor as a service - this is already working.

But the problem is to select the row by the ID (uuid) or index (and the page!). Page and index can be "calculated" client- or server-side...

How can I select the correct row? The problem is, that the index in the flat list is not the correct index in the treegrid.

Can you help me please?


10 Replies

PS Pon Selva Jeganathan Syncfusion Team February 9, 2022 01:24 PM UTC

Hi Bernd, 
 
Thanks for contacting syncfusion forum. 
 

Query: Select Row by Index or ID with data by Custom Adaptor

Based on your query, We understand you need to select the row using index or id. We suggest you use the GetRowIndexByPrimaryKey and SelectRowAsync method. You can get the row index using GetRowIndexByPrimaryKey method of the treegrid and pass this index into the SelectRow method. 
 
Please refer to the below code snippet, 
 
 
<SfButton OnClick="SelectRow" Content="Navigate"></SfButton>  
<SfTreeGrid @ref="treeGrid" TValue="TreeData.BusinessObject" AllowPaging="true"   
            HasChildMapping="isParent" IdMapping="TaskId" ParentIdMapping="ParentId"  
            TreeColumnIndex="1" AllowSorting="true" 
            > 
    <SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager> 
    <TreeGridPageSettings PageSize="2"></TreeGridPageSettings> 
     
    …… 
 
private async Task SelectRow() 
    { 
        var inx =await TreeGrid.GetRowIndexByPrimaryKeyAsync(7);//get the index value 
        await TreeGrid.SelectRowAsync(inx); 
    }                                                                                       
                                                                        } 
 
Please refer to the below API documentation, 
 
Kindly get back to us for further assistance. 
 
Regards,   
Pon selva   



BP Bernd Parchmann February 9, 2022 03:15 PM UTC

I tested your code with button and calling in DataBound event.

Both time the calculated index is -1 (I am using UUID (string) as primary key).


And how treegrid selecting is working with paging as treegrid with custom adaptor is just working with PageSizeMode = Root? How the page can be calculated without knowing how many rows has a page?



PS Pon Selva Jeganathan Syncfusion Team February 11, 2022 04:05 AM UTC

Hi Bernd, 
  
Query: Both time the calculated index is -1 (I am using UUID (string) as primary key). 


If the selected row is not present on the page, it will return an index of -1. So we suggest you ensure the record is present on the page.

 

Note: If the row is collapsed, the collapsed child record index is always returned as -1.

 

Query: And how treegrid selecting is working with paging as treegrid with custom adaptor is just working with PageSizeMode = Root?

 

If the selection row is present in the dom or page it will select the row.

 

Please refer to the below help documentation,

https://blazor.syncfusion.com/documentation/treegrid/selection

 

Please refer to the below demo,

https://blazor.syncfusion.com/demos/tree-grid/selection?theme=bootstrap5

 

Query: How the page can be calculated without knowing how many rows has a page?

 

Paging options can be configured through the TreeGridPageSettings. If you define the page size property of pagesettings, the row rendered based on the page size.  If you not define the page size property, Tree grid paging to fetch only a pre-defined number of records from the data source.

 

Please refer to the below help documentation,

https://blazor.syncfusion.com/documentation/treegrid/paging     

 

Kindly get back to us for further assistance.

 

Regards, 
Pon selva 



BP Bernd Parchmann February 11, 2022 08:01 AM UTC



I added a breakpoint in the Select() method:



The adaptor result:



In the treeGrid.FlatData property the row is available, too:



Maybe something in the control configuration is wrong defined:




PS Pon Selva Jeganathan Syncfusion Team February 14, 2022 04:05 PM UTC

Hi Bernd, 
  
Thanks for the update. 
  
We were unable to reproduce the issue(The row selection always returned index as -1) at our end while preparing a sample based on your shared code example. 
  
Please refer to the below sample, 

https://www.syncfusion.com/downloads/support/forum/172695/ze/TreeGridCC-1315905446

 

In your shared code snippet, you are not bind primary key property in the column definition of the tree grid. By default, you need to pass the primary key value in GetRowIndexByPrimaryKey method. So, we suggest you add the primary key property in column definition and pass the primary key value in GetRowIndexByPrimaryKey method.

 

Please refer to the below code snippet,

 

<SfTreeGrid @ref="treeGrid" TValue="TreeData.BusinessObject" AllowPaging="true"   
            HasChildMapping="isParent" IdMapping="TaskId" ParentIdMapping="ParentId"  
            TreeColumnIndex="1" AllowSorting="true" 
            > 
    <SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager> 
    <TreeGridPageSettings  PageSizeMode="PageSizeMode.Root"></TreeGridPageSettings> 
     
    <TreeGridColumns> 
        <TreeGridColumn Field="TaskId" HeaderText="Task ID"  Width="80" TextAlign="TextAlign.Right"></TreeGridColumn> 
  
        <TreeGridColumn Field="Uuid" HeaderText="UUId" IsPrimaryKey="true" Width="100"></TreeGridColumn> 
    </TreeGridColumns> 

</SfTreeGrid>

 

 

public async Task DataHandler()  
    {  
        
            var inx =await treeGrid?.GetRowIndexByPrimaryKey("dsdadsafe-32fedt-fd34-df3gsad");//pass the primary key value 
        await treeGrid?.SelectRow(inx); 
  
            

        }

 

Please refer to the below screenshot,

 

 

Kindly get back to us for further assistance.

 

Regards, 
Pon selva 
 



BP Bernd Parchmann February 14, 2022 04:39 PM UTC

Hi,


yes, you are right! I thought that the IdMapping definition is enough!

With adding the ID column with attribute isPrimaryKey the selection is now working!

Nice - thx very much!


But can you tell me how I get the page of the item with the searched ID? Calculating with the page size is not working because of PageSizeMode is Root! 



PS Pon Selva Jeganathan Syncfusion Team February 15, 2022 05:29 PM UTC

Hi Bernd, 
  
Thanks for the update 
  
Query: selection issue 
  
We are glad to hear your query has been solved by our solution. 
  
Query: how I get the page of the item with the searched ID?  
  
We are working on this query with high priority. And we need time to find a feasible solution of your requirement and will update you with further details by 17th February, 2022 
  
In the meanwhile, we request you to share the detailed explanation of your issue and share video demo or screenshot of the requirement. Which is helpful provide you response as early as possible. 
  

Until then we value your patience.

     
Regards,   
Pon selva   



PS Pon Selva Jeganathan Syncfusion Team February 18, 2022 12:43 PM UTC

Hi Bernd, 
 
Sorry for the delayed response 

Query: how I get the page of the item with the searched ID?  

Based on your query, you need to handle this logic externally. If you have set Page mode as Root, by default we consider in the record count only the root level or 0th level records.

And if you do not set the pageSize property, by default the treegrid considers 12 records per page (i.e., 12 root parents are rendered). If you set the pageSize property, for example, you set 2, it will be considered two root parents.

 

In the below sample, we are using a button click event. First, we saved the results of the search in the read method.  In the button click event, first we filter the parent records from the data source. Then we find the parent index from the searched records. Based on the parent index, we calculate the page number.

Please refer to the below code snippet,

<SfTreeGrid @ref="treeGrid" TValue="TreeData.BusinessObject" AllowPaging="true"   
            HasChildMapping="isParent" IdMapping="TaskId" ParentIdMapping="ParentId"  
            TreeColumnIndex="1" AllowSorting="true" 
            > 
…… 

</SfTreeGrid>

public async Task DataHandler()  
    {  
       var dat = TreeData.GetSelfDataSource().ToList(); 
        for (var i = 0; i < 15; i++) 
        { 
            if (dat[i].ParentId == null){ 
       parentData.Add(dat[i]); // filter the parent records 
    } 
    } 
       
        var val = parentData.FindIndex(x => x.TaskId == searchval[0].ParentId); // filter the parent index from serched record 
 
        if (val >= 0 || val <= 11) 
        { 
           //the record in page 1 
        } 
        else if(val == 12 || val == 23) 
        { 
            // the record in page 2 
        } 
         
            

        }

public override object Read(DataManagerRequest dm, string key = null) 
        { 
            filterCount = null; 
            IEnumerable<TreeData.BusinessObject> DataSource = TreeGridData; 
            if (dm.Search != null && dm.Search.Count > 0) 
            { 
                // Searching 
                DataSource = DataOperations.PerformSearching(DataSource, dm.Search); 
                searchval = DataSource.ToList(); 
            } 
            if (dm.Sorted != null && dm.Sorted.Count > 0) 
            { 
                // Sorting 
                DataSource = DataOperations.PerformSorting(DataSource, dm.Sorted); 

            }

Kindly get back to us for further assistance.

Regards, 
Pon selva 




BP Bernd Parchmann February 18, 2022 08:41 PM UTC

thx for your proposal!

I have already solved it with calculating the page similar your hint:

I take all the roots, getting the root of the selected item I want to select and divide with the root page size (dm.Take value). Selecting this page and then GetRowIndexByPrimaryKey and SelectRowAsync(index)



PS Pon Selva Jeganathan Syncfusion Team February 21, 2022 08:46 AM UTC

Hi Bernd, 
 
Thanks for the update 
 
We are glad to hear your query has been solved. 
     
Regards,   
Pon selva  

Loader.
Up arrow icon