Select children rows based on a Boolean property when parent row is checked

When using AutoCheckHierarchy and ShowCheckbox, when I select a parent row I want the children to be selected depending on a boolean property that the items in the data source have. 
For instance: if I check parent 1 in the following data source,  the condition is to check the children that have a duration less than 4, in that case only child 2 and 3 should be checked

    public static List<BusinessObjectGetSelfDataSource()
    {
        List<BusinessObjectBusinessObjectCollection = new List<BusinessObject>();
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 1TaskName = "Parent Task 1"StartDate = new DateTime(20171023), Duration = 10Progress = 70ParentId = null });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 2TaskName = "Child task 1"StartDate = new DateTime(20171023), Duration = 4Progress = 80ParentId = 1 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 3TaskName = "Child Task 2"StartDate = new DateTime(20171024), Duration = 5Progress = 65ParentId = 2 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 4TaskName = "Child task 3"StartDate = new DateTime(20171025), Duration = 6Progress = 77ParentId = 3 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 5TaskName = "Parent Task 2"StartDate = new DateTime(20171023), Duration = 10Progress = 70ParentId = null });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 6TaskName = "Child task 1"StartDate = new DateTime(20171023), Duration = 4Progress = 80ParentId = 5 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 7TaskName = "Child Task 2"StartDate = new DateTime(20171024), Duration = 5Progress = 65ParentId = 5 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 8TaskName = "Child task 3"StartDate = new DateTime(20171025), Duration = 6Progress = 77ParentId = 5 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 9TaskName = "Child task 4"StartDate = new DateTime(20171025), Duration = 6Progress = 77ParentId = 5 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 10TaskName = "Parent Task 3"StartDate = new DateTime(20171023), Duration = 10Progress = 70ParentId = null });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 11TaskName = "Child task 1"StartDate = new DateTime(20171023), Duration = 4Progress = 80ParentId = 10 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 12TaskName = "Child Task 2"StartDate = new DateTime(20171024), Duration = 5Progress = 65ParentId = 10 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 13TaskName = "Parent Task 4"StartDate = new DateTime(20171025), Duration = 6Progress = 77ParentId = null });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 14TaskName = "Child task 1"StartDate = new DateTime(20171025), Duration = 6Progress = 77ParentId = 13 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 15TaskName = "Parent Task 5"StartDate = new DateTime(20171023), Duration = 10Progress = 70ParentId = null });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 16TaskName = "Child task 1"StartDate = new DateTime(20171023), Duration = 4Progress = 80ParentId = 15 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 17TaskName = "Child Task 2"StartDate = new DateTime(20171024), Duration = 5Progress = 65ParentId = 15 });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 18TaskName = "Parent Task 6"StartDate = new DateTime(20171025), Duration = 6Progress = 77ParentId = null });
        BusinessObjectCollection.Add(new BusinessObject() { TaskId = 19TaskName = "Child task 1"StartDate = new DateTime(20171025), Duration = 6Progress = 77ParentId = 18 });
        return BusinessObjectCollection;
    }

1 Reply 1 reply marked as answer

FS Farveen Sulthana Thameeztheen Basha Syncfusion Team March 2, 2021 03:22 PM UTC

Hi Adriana, 

Thanks for contacting Syncfusion Support. 

Query#:- if I check parent 1 in the following data source,  the condition is to check the children that have a duration less than 4, in that case only child 2 and 3 should be checked. 
 
We have achieved this requirement using CheckboxChange event of the TreeGrid. Also we have disabled AutoCheckHierarchy property so that we can select the checkboxes based on condition despite(hierarchy selection) i.e prevented autoSelection of child records while clicking the parent records. We have get the selectedIndex based on condition and select the checkboxes using SelectCheckboxes method of the TreeGrid.  

Refer to the code below:- 
<SfTreeGrid IdMapping="TaskId"  @ref="TreeGrid"  ParentIdMapping="ParentId" DataSource="@TreeGridData" TreeColumnIndex="1" AutoCheckHierarchy="false"> 
    <TreeGridEvents TValue="TreeData.BusinessObject" CheckboxChange="Change"></TreeGridEvents> 
    <TreeGridColumns> 
        <TreeGridColumn Field="TaskId" HeaderText="Task ID" Width="80" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></TreeGridColumn> 
          .   .   . 
   </TreeGridColumns> 
</SfTreeGrid> 
@code{ 
    SfTreeGrid<TreeData.BusinessObject> TreeGrid; 
    private List<double> SelectIndex { get; set; } 
    private bool flag { get; set; } = true; 
    public List<TreeData.BusinessObject> TreeGridData { get; set; } 
    protected override void OnInitialized() 
    { 
        this.TreeGridData = TreeData.GetSelfDataSource().ToList(); 
    } 
    public async Task Change(CheckBoxChangeEventArgs<TreeData.BusinessObject> args) 
    { 
        var Source = TreeGrid.GetCurrentViewRecords(); 
        var keys = Source.GroupBy(rec => rec.ParentId).Where(g => g.Key != null).ToDictionary(g => g.Key?.ToString(), g => g.ToList()).Keys.ToList(); 
 
        if(this.flag) {           // prevented checkbox selection while using selectcheckbox method  
 
            var Row = Source.ElementAt(Convert.ToInt32(args.SelectedRowIndex)); 
            if (Row.ParentId == null || keys.Contains(Row.TaskId.ToString())) 
            { 
 
                this.SelectedRowIndex = args.SelectedRowIndex;         
                var IndexNum = 0; 
                SelectIndex = new List<double>(); 
                var Children = new List<TreeData.BusinessObject>(); 
                var parents = new List<int?> { Row.TaskId }; 
                foreach(var record in Source) 
                { 
                    if (parents.Contains(record.ParentId))      // get the records for multi level childrecords 
                    { 
                        Children.Add(record); 
                        parents.Add(record.TaskId);              
                    } 
                } 
                foreach (var child in Children) 
                { 
                    IndexNum = 0; 
                    foreach (var record in Source) 
                    { 
                        if (record.TaskId == child.TaskId && child.Duration < 4) 
                        { 
                            SelectIndex.Add(IndexNum);         // Add the SelectedIndex based on condition(Duration < 4) 
                        } 
                        IndexNum++; 
                    } 
                } 
                this.flag = false; 
                await TreeGrid.SelectCheckboxes(SelectIndex.ToArray());    //select the checkboxes manually with this method 
            } 
        } 
        else 
        { 
            this.flag = true; 
        } 
 
 
    } 


Screenshot:- 
 

Refer to the Documentation Link:- 

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

Regards, 
Farveen sulthana T 


Marked as answer
Loader.
Up arrow icon