How to select newly created / or modified row

Hi,

I am using operations like "await this.TreeGrid.AddRecord(data, row, RowPosition.Above)" to add nodes to my treegrid.  I do that also for RowPosition.Below and RowPosition.Child.  These operations are called from a splitbutton in each row of the grid.

My questions are:

1) How could I have the inserted (or updated) row selected automatically?  I know that by using the built-in CRUD functions, it gets selected by default, but not using code;
2) Is there any built-in way to manage adding rows before, after or as a child on request or is this way the best one?

Thanks for your help,

ma

3 Replies 1 reply marked as answer

PK Padmavathy Kamalanathan Syncfusion Team February 19, 2021 12:35 PM UTC

Hi Martin, 
 
Thanks for contacting Syncfusion Forums. 
 
Query 1: How could I have the inserted (or updated) row selected automatically?   
 
By default, edited row will be selected after performing editing functionality. In order to select the newly added row immediately after adding it, we suggest you to follow the below steps, 
  1. OnActionComplete” event, with RequestType “Save” will be triggered when you add new record and save it. In that event, we need to collect the primary key id of newly added record.
  2. We can find the index of newly added record using method “GetRowIndexByPrimaryKey” by passing the primary key of the newly added row.
  3. Then we can select newly added row by calling “SelectRow” method and passing index of newly added record as parameter to it.
 
Please check the below code snippet, 
 
 
<SfTreeGrid @ref="TreeGrid" DataSource="@TreeGridData" IdMapping="TaskId" ParentIdMapping="ParentId"  
            TreeColumnIndex="1" Toolbar="@ToolbarItems" 
            AllowPaging="true" > 
 
    <TreeGridEvents TValue="TreeData" RowSelected="RowSelectHandler" OnActionComplete="ActionComplete"  
                    ></TreeGridEvents> 
    <TreeGridColumns> 
        <TreeGridColumn Field="TaskId" HeaderText="Task ID" IsPrimaryKey="true" Width="80" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn> 
        <TreeGridColumn Field="TaskName" HeaderText="Task Name" Width="160"></TreeGridColumn> 
        <TreeGridColumn HeaderText="Add New Record" Width="80"> 
            <Template> 
                <SfSplitButton IconCss="e-btn-icons e-paste"> 
                    <SplitButtonEvents ItemSelected="ItemSelected"> 
                    </SplitButtonEvents> 
                    <DropDownMenuItems> 
                        <DropDownMenuItem Text="Add Above" Id="Above"></DropDownMenuItem> 
                        <DropDownMenuItem Text="Add Below" Id="Below"></DropDownMenuItem> 
                        <DropDownMenuItem Text="Add Child" Id="Child"></DropDownMenuItem> 
                    </DropDownMenuItems> 
                </SfSplitButton> 
            </Template> 
        </TreeGridColumn> 
</SfTreeGrid> 
@code{ 
 
    SfTreeGrid<TreeData> TreeGrid; 
 
    public List<TreeData> TreeGridData { get; set; } 
 
    public List<string> ToolbarItems { get; set; } 
    public Syncfusion.Blazor.TreeGrid.RowPosition position { get; set; } = RowPosition.Below; 
 
    public double PreviousSelectedIndex = -1; 
    public int value = 20; 
    public static int? Pkey { get; set; } 
 
 
    protected override void OnInitialized() 
    { 
        this.ToolbarItems = new List<string>() { "Add", "Delete", "Update", "Cancel" }; 
 
        this.TreeGridData = TreeData.GetSelfDataSource().ToList(); 
    } 
    public void RowSelectHandler(RowSelectEventArgs<TreeData> args) 
    { 
        PreviousSelectedIndex = args.RowIndex; // saving the row index 
    } 
    public async void ActionComplete(ActionEventArgs<TreeData> args) 
    { 
        if (args.RequestType.ToString() == "Save") 
        { 
            await Task.Delay(100); 
            Pkey = args.Data.TaskId;           //get primary key value of newly added record  
            var index = this.TreeGrid.GetRowIndexByPrimaryKey(Pkey); 
            this.TreeGrid.SelectRow(index.Result); 
        } 
    } 
 
 
 
    private void ItemSelected(MenuEventArgs args) 
    { 
        ++value; 
        var data = new TreeData() 
        { 
            TaskId = value, 
            TaskName = "Parent Task 1", 
            Duration = 10, 
            Progress = 70, 
            Priority = "Critical", 
            Resources = 1 
        }; 
        if (args.Item.Text == "Add Above") 
        { 
            this.TreeGrid.AddRecord(data, PreviousSelectedIndex, RowPosition.Above); 
 
        } 
        if (args.Item.Text == "Add Below") 
        { 
           this.TreeGrid.AddRecord(data, PreviousSelectedIndex, RowPosition.Below); 
        } 
        if (args.Item.Text == "Add Child") 
        { 
            this.TreeGrid.AddRecord(data, PreviousSelectedIndex, RowPosition.Child); 
 
        } 
    } 
 
} 

 
 
Please check the below API help documentations, 
 
Query 2:  Is there any built-in way to manage adding rows before, after or as a child on request or is this way the best one? 
 
You can use the AddRecord method for adding new record with different position as you are using now. 
 
Kindly get back to us for further assistance. 
Regards, 
Padmavathy Kamalanathan 



MA Martin Ares February 25, 2021 04:43 AM UTC

Hi,

Thank you very much for your help.  While this solution seems good, it does not work all the time.  For instance, using the solution you provided I've created a video showing that nodes get created at "random" places.

For instance, I create a sibling below parent task 2, and then a sibling below child task 2 and it gets created before child task 2.  This is just an example, I did a LOT of testing and could not arrive with a simple explanation.  Please help.

Thanks,

ma

Attachment: Blazorhowtosample__Google_Chrome_20210224_233607_d7fafa8e.zip


PK Padmavathy Kamalanathan Syncfusion Team February 26, 2021 12:26 PM UTC

Hi Martin, 
 
Thanks  for contacting Syncfusion Support. 
 
We are able to reproduce the reported issue while adding record with “Below” Position. We have considered it as a bug and logged report for the same. Thank you for taking time to report this issue " Record does not get added at proper position while using AddRecord method with Below Position" and helping us improve our product. Fix for this issue will be included in our patch release after our Volume 1 2021 Release which is expected to be rolled out at the end of March 2021. 
 
You can track the current status of your request, review the proposed resolution timeline and contact us for any further queries through this link, 
 
 
Note: To view the above feedback, kindly login into your account. 
 
Disclaimer: The feedback link is in review state and you can check it once it is validated 
 
Kindly get back to us for further assistance. 
 
Regards, 
Padmavathy Kamalanathan 


Marked as answer
Loader.
Up arrow icon