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

Disable checkbox from ShowCheckbox property

Hi,

 I have a TreeGrid and use the option of ShowCheckbox="true" in the TreeGridColumn.

Now I have some rows I want to disable, which work quit good. 
For this I use the RowDataBound event to set the e-disabled css class. But the checkbox is still clickable.

public void RowDataBoundHandler(RowDataBoundEventArgs<TTargetEntity> args)
{
     if((args.Data as TaskReportingDto)?.IsBilled ?? false)
          args.Row.AddClass(new []{ "e-disabled"});
}



What can I do to disable the checkbox as well?




Thank you and best regards


7 Replies

PS Pon Selva Jeganathan Syncfusion Team February 6, 2023 04:02 AM UTC

Hi Sven Boris Bornemann,


Query: I have some rows I want to disable. What can I do to disable the checkbox as well?


We achieved your requirement by using the CheckboxChange event and the SelectCheckboxes method of the TreeGrid. In the row data bound event, collect the TaskId value of the selected data which is the unique value. In the CheckboxChange event, based on the Taskid value, prevent the selection using the SelectCheckboxes method.


Please refer to the below code snippet,


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

…..

    <TreeGridEvents TValue="TreeData.BusinessObject" CheckboxChange="checkboxchange" RowDataBound="RowDataBoundHandler" RowSelecting="Rowselecting"></TreeGridEvents>

</SfTreeGrid>

 

 

public void RowDataBoundHandler(RowDataBoundEventArgs<TreeData.BusinessObject> args)

    {

        // Here you can customize your code

        if (args.Data?.TaskId == 4 || args.Data?.TaskId == 6 || args.Data?.TaskId == 5)

        {

 

            args.Row.AddClass(new[] { "e-disabled" }); //here we disable the row

            rowIndexes.Add(args.Data.TaskId); // collect the disabled row taskid(this is unique value)

        }

    }

    public void checkboxchange(CheckBoxChangeEventArgs<TreeData.BusinessObject> args)

    {

        if (args.SelectedRowIndex != -1)

        {

            for (var i = 0; i < rowIndexes.Count; i++)

            {

                if (this.TreeGridData[(int)args.SelectedRowIndex].TaskId == rowIndexes[i])

                {

                    //args.Checked = false;

                    double[] selected = new double[1];

                    selected[0] = args.SelectedRowIndex;

 

 

                    TreeGrid.SelectCheckboxes(selected); // Here disable the checkbox selection

                }

            }

        }

    }

  


Please refer to the below API documentation,


CheckboxChange event

RowDataBound event

SelectCheckboxes method


Please refer to the below sample,

https://www.syncfusion.com/downloads/support/directtrac/general/ze/Selection-1299525089


Kindly get back to us for further assistance.


Regards,

Pon selva



SB Sven Boris Bornemann February 6, 2023 07:42 PM UTC

Hi,


thank you for the answer. 

In your example the TaskId is an integer. In my code it need to be a string. I updated your code with my dataset. 

Is this case the SelectedRowIndex is not the same as the TaskId anymore!

What do I need to do to get it working?


Other question related to that.

The disabled checkbox get selected when I click the checkbox on a parent element which isn't disabled.

Is it possible to prevent this as well?


Best regards

Sven


Attachment: hierarchy_freezeline_4c24f859.zip


PS Pon Selva Jeganathan Syncfusion Team February 8, 2023 03:06 PM UTC

Sven Boris Bornemann,


Query: In my code it need to be a string. I updated your code with my dataset. Is this case the SelectedRowIndex is not the same as the TaskId anymore! What do I need to do to get it working?


We are able to replicate the issue at our end. In your code, the issue is that the SelectedRowIndex is not updated correctly, which is causing the issue. To resolve this, use the GetCurrentViewRecords method of the SfTreeGrid component to get the current view records and get the row index value.


Please refer to the below code snippet,


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

…..

    <TreeGridEvents TValue="TreeData.BusinessObject" CheckboxChange="checkboxchange" RowDataBound="RowDataBoundHandler" RowSelecting="Rowselecting"></TreeGridEvents>

</SfTreeGrid>

 

 

public void RowDataBoundHandler(RowDataBoundEventArgs<TreeData.BusinessObject> args)

    {

        // Here you can customize your code

        if (args.Data?.TaskId == 4 || args.Data?.TaskId == 6 || args.Data?.TaskId == 5)

        {

 

            args.Row.AddClass(new[] { "e-disabled" }); //here we disable the row

            rowIndexes.Add(args.Data.TaskId); // collect the disabled row taskid(this is unique value)

        }

    }

    public void checkboxchange(CheckBoxChangeEventArgs<TreeData.BusinessObject> args)

    {

        if (args.SelectedRowIndex != -1)

        {

            for (var i = 0; i < rowIndexes.Count; i++)

            {

if (TreeGrid.GetCurrentViewRecords()[(int)args.SelectedRowIndex].TaskId == rowIndexes[i])

                {

                {

                    //args.Checked = false;

                    double[] selected = new double[1];

                    selected[0] = args.SelectedRowIndex;

 

 

                    TreeGrid.SelectCheckboxes(selected); // Here disable the checkbox selection

                }

            }

        }

    }

  


Please refer to the below API documentation,

https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.TreeGrid.SfTreeGrid-1.html#Syncfusion_Blazor_TreeGrid_SfTreeGrid_1_GetCurrentViewRecords


Query: The disabled checkbox get selected when I click the checkbox on a parent element which isn't disabled. Is it possible to prevent this as well?


We have already discussed same in our Knowledge Base documentation.


Please refer to the below KB documentation,

https://www.syncfusion.com/kb/12537/how-to-select-records-using-checkbox-column-based-on-condition-in-blazor-tree-grid


Kindly get back to us for further assistance.



SB Sven Boris Bornemann February 10, 2023 12:43 PM UTC

Hi,

The first question is solved. Works perfect. Thank you very much


But the second one isn' working because I want to use AutoCheckHierarchie=true. And is this case I want to prevent that the check box in a disabled row is checked.


Best 




PS Pon Selva Jeganathan Syncfusion Team February 13, 2023 06:48 PM UTC

Sven Boris Bornemann,


Query: second one isn' working because I want to use AutoCheckHierarchie=true. And is this case I want to prevent that the check box in a disabled row is checked


We are able to replicate the issue at our end.  To overcome this, we suggest you to follow the below code example.


Please refer to the below code snippet,


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

…..

    <TreeGridEvents TValue="TreeData.BusinessObject" CheckboxChange="checkboxchange" RowDataBound="RowDataBoundHandler" RowSelecting="Rowselecting"></TreeGridEvents>

</SfTreeGrid>

 

 

public void RowDataBoundHandler(RowDataBoundEventArgs<TreeData.BusinessObject> args)

    {

        // Here you can customize your code

        if (args.Data?.TaskId == 4 || args.Data?.TaskId == 6 || args.Data?.TaskId == 5)

        {

 

            args.Row.AddClass(new[] { "e-disabled" }); //here we disable the row

            rowIndexes.Add(args.Data.TaskId); // collect the disabled row taskid(this is unique value)

        }

    }

    public void checkboxchange(CheckBoxChangeEventArgs<TreeData.BusinessObject> args)

    {

         //here we collect the selct child's parent records details

            var parent = TreeGridData.Where(p => p.isparent == true).ToList();

            for (var j = 0; j < parent.Count; j++)

            {

                if (TreeGrid.GetCurrentViewRecords()[(int)args.SelectedRowIndex].TaskId == parent[j].TaskId)

                {

                    parentcheked = true//enable the parent condition when parent is checked

                    selected_parent = parent[j];

                }

            }

 

           

            for (var i = 0; i < rowIndexes.Count; i++)

            {

                //here disable the checkbox selection on individual disable row

                if (TreeGrid.GetCurrentViewRecords()[(int)args.SelectedRowIndex].TaskId == rowIndexes[i])

                {

                   

                    double[] selected = new double[1];

                    selected[0] = args.SelectedRowIndex;

 

 

                    TreeGrid.SelectCheckboxes(selected);

                }

                //here disable the disable row's checkbox selecttion when parent is checked

                else if (parentcheked == true && args.Checked==true)

                {

                    //here collect the all child records of selected parent

                    var Child = TreeGrid.GetCurrentViewRecords().Where(p => p.ParentId == selected_parent.TaskId).ToList();

                  

int val = 0;

                    for (var ch = 0; ch < Child.Count; ch++)

                    {

                        for (i = 0; i < rowIndexes.Count; i++)

                        {


                            if (Child[ch].TaskId == rowIndexes[i])


                            {

                                 val = i;


                    }

                    }

                        if (Child[ch].TaskId != rowIndexes[val])

                        {

                            Tree_data.Add(Child[ch]);

                        }


                    }

                    double[] selected = new double[Tree_data.Count];

 

                    for (var ch = 0; ch < Tree_data.Count; ch++)

                    {

                        //here we find the child index of selected parent

                        selected[ch] = TreeGrid.GetCurrentViewRecords().FindIndex(a => a.TaskId.Contains(Tree_data[ch].TaskId));

                    }

                    await Task.Delay(100);

                    //here prevent the selection

                    await TreeGrid.SelectCheckboxes(selected);

 

                    parentcheked = false;// disable

                }

 

    }

  


In the above code snippet, the selected parent record details are collected using its index. Using the taskid value of the parent, all child records of the selected parent are collected, excluding the disabled rows. Then, the checkboxes are selected using the selectCheckboxes method based on the child records index value.


Please refer to the below screenshot,



Note: Based on that you need to handle the multiple levels.


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






SB Sven Boris Bornemann February 15, 2023 10:06 AM UTC

Wow.. ok this is so complex.

I also don't know if my task is a parent or not. 

I guess I will look for another solution. 

But thank you for investigating.



PS Pon Selva Jeganathan Syncfusion Team February 20, 2023 03:22 AM UTC

Sven Boris Bornemann,


Query:
I also don't know if my task is a parent or not. 


We suggest defining a Boolean variable in your data source that enables a value if the record is a parent, otherwise, it is disabled. By checking this Boolean variable's value, you can identify whether the record is a parent or not.


Please refer to the below code snippet,


public class BusinessObject

        {

            public string TaskId { get; set; }

            public string TaskName { get; set; }

            public DateTime? StartDate { get; set; }

            public int? Duration { get; set; }

            public int? Progress { get; set; }

            public string? ParentId { get; set; }

            public bool isparent { get; set; }

        }

 

        public static List<BusinessObject> GetSelfDataSource()

        {

            List<BusinessObject> BusinessObjectCollection = new List<BusinessObject>();

            BusinessObjectCollection.Add(new BusinessObject() { TaskId = "1", TaskName = "Parent Task 1", StartDate = new DateTime(2017, 10, 23), Duration = 10, Progress = 70, ParentId = null, isparent=true });

            BusinessObjectCollection.Add(new BusinessObject() { TaskId = "2", TaskName = "Child task 1", StartDate = new DateTime(2017, 10, 23), Duration = 4, Progress = 80, ParentId = "1",isparent=false });

….Add(new BusinessObject() { TaskId = "5", TaskName = "Parent Task 2", StartDate = new DateTime(2017, 10, 23), Duration = 10, Progress = 70, ParentId = null,isparent=true });

,,,

        }


Please refer to the below sample,

https://www.syncfusion.com/downloads/support/directtrac/general/ze/Selection2007524650


Kindly get back to us for further assistance.


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




Loader.
Live Chat Icon For mobile
Up arrow icon