How to setup checkbox event in grid when edit mode

Hi

I am working on grid for ASP.NET web forms.  I want to implement checkbox in grid.  

1.When it comes to edit mode, checkbox converts to textbox.  I want to show checkbox even if it's edit mode.  How can we implement this?

2. When edit mode, if I checked checkbox in one of the rows, I want to disable another column in the same row.  Can we do that?


I attach sample solution.

If you click edit button in grid, it converts textbox...
and also when edit mode, If I clicked checkbox(checkColumnA) on, I want to set  start date disabled, and also( checkColumnB) is clicked, I want to set end date disabled.


I would appreciate it if you help this.

Thanks,
Yukiko

  


Attachment: test_822e98e6.7z

13 Replies

PK Prasanna Kumar Viswanathan Syncfusion Team August 24, 2018 09:03 AM UTC

Hi Yukiko, 

Thanks for contacting Syncfusion support. 

Query : When it comes to edit mode, checkbox converts to textbox.  I want to show checkbox even if it's edit mode 
 
To achieve your requirement we suggest you to use EditTemplate property of ejGrid. On editing the column values, the custom editor can be created by using EditTemplate property of columns API. So, we have render the ejCheckBox on editing the column values.   

Find the code example: 


<ej:Grid ID="Grid1" runat="server" AllowPaging="True"  DataSourceID="test34" ClientIDMode="AutoID"  OnServerAddRow="Grid1_ServerAddRow" AllowCellMerging="true" AllowGrouping="true"  OnServerEditRow="Grid1_ServerEditRow"  AllowSorting="true">  
                 <ClientSideEvents ActionBegin="onbegin" MergeHeaderCellInfo="mergeHead"  RowSelected="rowselect" TemplateRefresh="template" />          
         
                           …... 
       
        <Columns> 
            <ej:Column Field="memberId" Width="100px" IsPrimaryKey="true"/> 
 
                           …... 
 
           <ej:Column Field="checkColumnA"   TemplateID="#testA" Template="true"  Width="50px"> 
                <EditTemplate Create="createcheckA" Read="readcheckA" Write="writecheckA"/></ej:Column> 
            <ej:Column Field="checkColumnB"  TemplateID="#testB" Template="true"   Width="50px"> 
                <EditTemplate Create="createcheckB" Read="readcheckB" Write="writecheckB"/></ej:Column> 
          
        </Columns> 
</ej:Grid> 


<script> 
    function createcheckA(args){ 
        return $("<input>"); 
    } 
    function writecheckA(args) { 
        if(args.rowdata.checkColumnA == "Y"){ 
            args.element.ejCheckBox({checked :true, 
                --------------- 
                }            
            }); 
        } 
        else if(args.rowdata.checkColumnA == "N"){ 
            args.element.ejCheckBox({ 
                checked: false, 
                   -------------- 
                } 
            }); 
        } 
    } 
 
    function readcheckA(args) { 
        if (args.ejCheckBox("isChecked")) 
            return "Y"; 
        else 
            return "N"; 
    } 
 
    function createcheckB(args) { 
        return $("<input>"); 
    } 
 
    function writecheckB(args) { 
        if (args.rowdata.checkColumnB == "Y") { 
            args.element.ejCheckBox({ 
                checked: true,  
                -------------------- 
            }); 
        } 
        else if (args.rowdata.checkColumnB == "N") { 
            args.element.ejCheckBox({ 
                checked: false, 
                ------------------------------ 
            }); 
        } 
    } 
    function readcheckB(args) { 
        if (args.ejCheckBox("isChecked")) 
            return "Y"; 
        else 
            return "N"; 
    } 
</script> 

For more information refer the below help documentation,  


Query 2 : When edit mode, if I checked checkbox in one of the rows, I want to disable another column in the same row.  Can we do that? 
 
To achieved your requirement we suggest you to bind the change event in ejCheckBox.  

In below code example we bound change event in write method of editTemplate and in change event we disabled the startDate if the checkColumnA is checked. If we unchecked then the checkColumn A will be enabled. Similarly we have enabled and disabled the endDate column based on checkColumnB. 

Find the code example:  

<script> 
    function createcheckA(args){ 
        return $("<input>"); 
    } 
    function writecheckA(args) { 
        if(args.rowdata.checkColumnA == "Y"){ 
            args.element.ejCheckBox({checked :true, 
                change: function (e) { 
                    if (e.isChecked) { 
                        $("#ctl00_MainContent_ucPerson1_Grid1startDate").prop("disabled", true); 
                        $("#ctl00_MainContent_ucPerson1_Grid1startDate").addClass("e-disable"); 
                    } 
                    else { 
                        $("#ctl00_MainContent_ucPerson1_Grid1startDate").prop("disabled", false); 
                        $("#ctl00_MainContent_ucPerson1_Grid1startDate").removeClass("e-disable"); 
                    } 
                }            
            }); 
        } 
        else if(args.rowdata.checkColumnA == "N"){ 
            args.element.ejCheckBox({ 
                checked: false, 
                change: function (e) { 
                    if(e.isChecked){ 
                    $("#ctl00_MainContent_ucPerson1_Grid1startDate").prop("disabled", true); 
                    $("#ctl00_MainContent_ucPerson1_Grid1startDate").addClass("e-disable"); 
                    } 
                    else { 
                        $("#ctl00_MainContent_ucPerson1_Grid1startDate").prop("disabled",false); 
                        $("#ctl00_MainContent_ucPerson1_Grid1startDate").removeClass("e-disable"); 
                    } 
                } 
            }); 
        } 
    } 
    function createcheckB(args) { 
        return $("<input>"); 
    } 
    function writecheckB(args) { 
        if (args.rowdata.checkColumnB == "Y") { 
            args.element.ejCheckBox({ 
                checked: true,  
                change: function (e) { 
                    if (e.isChecked) { 
                        $("#ctl00_MainContent_ucPerson1_Grid1endDate").prop("disabled", true); 
                        $("#ctl00_MainContent_ucPerson1_Grid1endDate").addClass("e-disable"); 
                    } 
                    else { 
                        $("#ctl00_MainContent_ucPerson1_Grid1endDate").prop("disabled", false); 
                        $("#ctl00_MainContent_ucPerson1_Grid1endDate").removeClass("e-disable"); 
                    } 
                } 
            }); 
        } 
        else if (args.rowdata.checkColumnB == "N") { 
            args.element.ejCheckBox({ 
                checked: false, 
                change: function (e) { 
                    if (e.isChecked) { 
                        $("#ctl00_MainContent_ucPerson1_Grid1endDate").prop("disabled", true); 
                        $("#ctl00_MainContent_ucPerson1_Grid1endDate").addClass("e-disable"); 
                    } 
                    else { 
                        $("#ctl00_MainContent_ucPerson1_Grid1endDate").prop("disabled", false); 
                        $("#ctl00_MainContent_ucPerson1_Grid1endDate").removeClass("e-disable"); 
                    } 
                } 
            }); 
        } 
    } 
</script> 

Please refer the below documentation for details of change event of checkbox. 


We have modified the provided sample with the above mentioned changes. Please refer the below link for sample. 


If you need any further assistance please get back to us. 

Regards, 
Prasanna Kumar N.S.V 
 



YI Yukiko Imazu August 24, 2018 10:50 PM UTC

Hi Prasanna,

Thank you for your detail information and sample solution.
I could resolve edit mode issue.

One more thing I need to do is I want to implement same function with this edit more when added a new row.
Is it possible?  When I run the sample solution and clicked add button, it doesn't show check box correctly.

I appreciate your help in advance.

Thanks,
Yukiko



PK Prasanna Kumar Viswanathan Syncfusion Team August 27, 2018 08:42 AM UTC

Hi Yukiko, 

Based on your previous query you have requested to render checkbox based on another column value. So we have suggested you to achieve your requirement using EditTemplate feature of Grid. While rendering checkbox through editTemplate we have checked the condition based on another column value which is the cause of not rendering ejCheckBox while adding a new record in Grid. 

To resolve the issue kindly render the checkbox in editTemplate write function when requestType is add. 

Refer the below code example 
    
 <script> 
    function createcheckA(args){ 
        return $("<input>"); 
    } 
 
    function writecheckA(args) { 
        if (args.requestType == "add") { 
            args.element.ejCheckBox() 
        } 
        if(args.rowdata.checkColumnA == "Y"){ 
            args.element.ejCheckBox({checked :true, 
                change: function (e) { 
                     .                .              .                 .            .  
                }            
            }); 
        } 
        else if(args.rowdata.checkColumnA == "N"){ 
            args.element.ejCheckBox({ 
                checked: false, 
.                .               .               .            .              .  
            }); 
        } 
    } 
 
    function readcheckA(args) { 
        if (args.ejCheckBox("isChecked")) 
            return "Y"; 
        else 
            return "N"; 
    } 
 
    function createcheckB(args) { 
        return $("<input>"); 
    } 
 
    function writecheckB(args) { 
        if (args.requestType == "add") { 
            args.element.ejCheckBox() 
        } 
        if (args.rowdata.checkColumnB == "Y") { 
            args.element.ejCheckBox({ 
                checked: true,  
                .                .              .             .              .         .  
                } 
            }); 
        } 
        else if (args.rowdata.checkColumnB == "N") { 
            args.element.ejCheckBox({ 
                checked: false, 
.                .                .           .             .             .  
            }); 
        } 
    } 
 
    function readcheckB(args) { 
        if (args.ejCheckBox("isChecked")) 
            return "Y"; 
        else 
            return "N"; 
    } 
 
 
</script> 

Refer the below screenshot for the output 

 

Please get back to us if you have further queries. 

Regards, 
Prasanna Kumar N.S.V. 



YI Yukiko Imazu August 27, 2018 02:25 PM UTC

Hi Prasanna Kumar,

Thank you so much for your support.
I could implement our requirement with your help.

Regards,
Yukiko


PK Prasanna Kumar Viswanathan Syncfusion Team August 28, 2018 04:14 AM UTC

Hi Yukiko, 
 
We will wait to hear from you. 
 
Regards, 
Prasanna Kumar N.S.V 



YI Yukiko Imazu September 17, 2018 08:10 PM UTC

Hi,

I have one more question regarding this checkbox behavior.

I tried to create custom dialog with checkbox.  I want to implement same click event which we've discussed above.
When I use other edit mode, it works fine.  When it comes to custom dialog, it doesn't work well.
I would like to know how to implement this function in custom dialog.

I attach my sample program.

Thanks,
Yukiko


Attachment: test_fa763758.7z


PK Prasanna Kumar Viswanathan Syncfusion Team September 18, 2018 06:15 AM UTC

Hi Yukiko, 

Based on your requirement you need to disable or enable the startDate field in a custom dialog based on the value of the checkbox. In your code example we found that you have used dialogEditorTemplate in Grid.  

For other editModes we need to use editTemplate property to achieve the requirement. The purpose of using editTemplate property is used to create a custom editor i.e., we can used appropriate JS controls based on the column type.  

As we have used dialogEditorTemplate in Grid,  so while using template form we can change the HTML elements to appropriate JS controls based on the column type. This can be achieved by using actionComplete event of ejGrid. So, we cannot use editTemplate property for the checkColumn A and checkColumn B in dialogEditorTemplate.  

To achieve this requirement we suggest you to render the ejCheckBox inside the dialog using actionComplete event and bind the change event to ejCheckBox. Using the change event of ejCheckBox we can disable or enable the startDate field in the Dialog. 

Please refer the below code example. 


<ej:Grid ID="Grid1" runat="server" AllowPaging="True"  DataSourceID="test34" ClientIDMode="AutoID"  OnServerAddRow="Grid1_ServerAddRow"  AllowCellMerging="true" AllowGrouping="true"  OnServerEditRow="Grid1_ServerEditRow"  AllowSorting="true">  
        <EditSettings AllowEditing="true"  AllowAdding="true"  AllowDeleting="true" EditMode="DialogTemplate" DialogEditorTemplateID="#temp" /> 
 
      <PageSettings ShowDefaults="true" PageSize="2" Template="#pageSizeLOATmp" EnableTemplates="true" /> 
         <ClientSideEvents ActionBegin="onbegin" MergeHeaderCellInfo="mergeHead"  RowSelected="rowselect" ActionComplete="onGridActionCompleteEvent" BeforePrint="printLOA"/>          
        <ToolbarSettings ToolbarItems="add,edit,delete,update,cancel" ShowToolbar="true"></ToolbarSettings> 
       
        <Columns> 
            <ej:Column Field="memberId" Width="100px" IsPrimaryKey="true"/> 
            <ej:Column Field="first_name" Type="string"   Width="80px" >          
            </ej:Column>             
 
                          …... 





function onGridActionCompleteEvent(args) { 
        $("#" + args.model.clientId).prepend($(".PrintHeaderLOA")); 
 
        pageSetting(args, "LOA"); 
 
        if (args.requestType == "beginedit" || args.requestType == "add") { 
       
                                  …... 
 
            if (args.model.editSettings.editMode == "dialogtemplate") { 
 
                $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#checkColumnA").ejCheckBox({ change: "checkchange" }); 
 
                $.ajax({ 
                    type: "POST", 
                    url: "Default.aspx/DropDown", 
                    contentType: "application/json; charset=utf-8", 
                    dataType: "json", 
                    success: function (value) { 
                      
                        $("#priceId").ejDropDownList({ 
                            dataSource: value.d, 
                            fields: { value: "priceId", text: "price1" }, 
                            width: "100%" 
                        }); 
                     
                    }, 
                    error: function (e) { 
                        alert(e); 
                    } 
                }); 
            } 
        } 
    } 
 
    function checkchange(args) { 
        if (args.isChecked == false) { 
            $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#startDate").removeClass("e-disable"); 
        } 
        else if (args.isChecked == true) { 
            $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#startDate").addClass("e-disable"); 
        } 
    } 

Refer the below screenshots. 

When checkbox is checked startDate is disabled. 

 

When checkbox is unchecked startDate is enabled. 

 

We have prepared a sample with the above mentioned changes. Please refer the below link for sample. 


Regards, 
Prasanna Kumar N.S.V 
 



YI Yukiko Imazu September 18, 2018 03:36 PM UTC

Hi Prasanna,

Thank you for your support.  I could achieve my requirement.
One more thing that I found was after implemented this function, if it was unchecked in Grid by default, it gets disabled in custom dialog.

 

I would like to use checkbox like normal dialog even if grid checkbox was unchecked.  Is it possible to do that in custom dialog?

I attach my program.

Thanks,
Yukiko

Attachment: test_ea378626.7z


PK Prasanna Kumar Viswanathan Syncfusion Team September 19, 2018 07:15 AM UTC

Hi Yukiko, 

Query : One more thing that I found was after implemented this function, if it was unchecked in Grid by default, it gets disabled in custom dialog. 
 
Based on your query you need to disable or enable the date field based on the checkstate of the checkbox at initial rendering of ejDialog. We have achieved your requirement using actionComplete event of ejGrid. In this event we disabled or enabled the startDate field based on the checkstate. 


function onGridActionCompleteEvent(args) { 
 
        if (args.requestType == "beginedit" || args.requestType == "add") { 
 
                               …...       
 
             
            if (args.model.editSettings.editMode == "dialogtemplate") { 
 
                $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#checkColumnA").ejCheckBox({ change: "checkchange" }); 
 
                var checkstate = $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#checkColumnA").ejCheckBox("isChecked"); 
 
                if (checkstate) { 
                    $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#startDate").addClass("e-disable") 
                    $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#startDate").attr("disabled", true); 
                    } 
                else { 
                    $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#startDate").removeClass("e-disable"); 
                    $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#startDate").attr("disabled",false); 
                    } 
                $.ajax({ 
                    type: "POST", 
                    url: "Default.aspx/DropDown", 
                    contentType: "application/json; charset=utf-8", 
                    dataType: "json", 
                    success: function 

                                                                          …... 


We have prepared a sample for your reference. Please refer the below link for sample. 

 
Query 2: “I would like to use checkbox like normal dialog even if grid checkbox was unchecked.  Is it possible to do that in custom dialog?”  

We are unclear in mentioned requirement, so please provide more details for the above requirement. 

Regards, 
Prasanna Kumar N.S.V 
 



YI Yukiko Imazu September 19, 2018 02:22 PM UTC

Hi Prasanna,

Thank you for your replying.
I run your solution.  I found that default setting of database is different from mine.
I attach my sample again.  I changed default value of checkbox column to "N" in the member Table.


If data was like above, it shows like this.


When it comes to this situation, even if in your modified sample, checkbox doesn't work.

Thank you in advance,
Yukiko




Attachment: Checkbox_In_Dialogtemplate_a5f64831.7z


PK Prasanna Kumar Viswanathan Syncfusion Team September 20, 2018 05:38 AM UTC

Hi Yukiko, 

In your sample we found that in dialog template elements you have given id attribute for the checkbox when checkColumnA value is equal to Y and for other checkbox column you have not given the id attribute. In actionComplete event we will found the input element based on id attribute, so when the checkColumnA value is equal to N the checkbox will be rendered without the id attribute and we will not able to find the checkbox element in actionComplete event. So, the mentioned issue has been occurred. 

To avoid the mentioned issue we suggest you to give the id attribute to the checkbox in the else condition also like in the below code example. Please refer the below code example. 

<script type="text/template" id="temp"> 
    <table> 
 
        <tr> 
            <td>startDate</td> 
            <td> <input id="startDate" name="startDate" value="{{: startDate}}" disabled="disabled"  
                                    class="e-field e-ejinputtext valid e-disable" style="text-align: right; width: 116px; height: 28px" /> 
                
            </td> 
            <td>  
                 {{if checkColumnA == 'Y'}}  
                   <input type="checkbox" class="check" checked id="checkColumnA"   /> 
                  {{else}} 
                   <input type="checkbox" class="check" id="checkColumnA" disabled/> 
                  {{/if}} 
            </td> 
        </tr> 
    </table> 
</script> 
 
function onGridActionCompleteEvent(args) { 
 
        if (args.requestType == "beginedit" || args.requestType == "add") { 
       
                                           …... 
 
            if (args.model.editSettings.editMode == "dialogtemplate") { 
 
                $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#checkColumnA").ejCheckBox({ change: "checkchange" }); 
 
                var checkstate = $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#checkColumnA").ejCheckBox("isChecked"); 
 
                if (checkstate) { 
                    $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#startDate").addClass("e-disable") 
                    $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#startDate").attr("disabled", true); 
                    } 
                else { 
                    $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#startDate").removeClass("e-disable"); 
                    $("#ctl00_MainContent_ucPerson1_Grid1EditForm").find("#startDate").attr("disabled",false); 
                    } 
                $.ajax({ 
                    type: "POST", 
                    url: "Default.aspx/DropDown", 
                    contentType: "application/json; charset=utf-8", 
                    dataType: "json", 
                    success: function (value) { 
                      
                        $("#priceId").ejDropDownList({ 
                            dataSource: value.d, 
                            fields: { value: "priceId", text: "price1" }, 
                            width: "100%" 
                        }); 
                     
                    }, 
                    error: function (e) { 
                        alert(e); 
                    } 
                }); 
            } 
        } 
    } 

If you need any further assistance please get back to us. 

Regards, 
Prasanna Kumar N.S.V 
 



YI Yukiko Imazu September 20, 2018 06:02 PM UTC

Hi Prasanna,

Thank you so much for your support, Prasanna!
We could achieve our requirement.

Regards,
Yukiko


PK Prasanna Kumar Viswanathan Syncfusion Team September 21, 2018 04:04 AM UTC

Hi Yukiko, 

We are happy to hear that your requirement has been achieved. 

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

Regards, 
Prasanna Kumar N.S.V 


Loader.
Up arrow icon