GridComponent with SwitchComponent

Hi,

I want to use SwitchComponent into my GridComponent

This is my portion of code:



Thisi is the template function

This is save code (I commented api's call because i have an error)


This is my page:


This is my dialog with console.log on the right


Before save:

After Save (local only):

The anaUserId field is saved correctly, the enabled no
Have you got any idea?
Thanks Alex




3 Replies 1 reply marked as answer

BS Balaji Sekar Syncfusion Team March 5, 2021 01:02 PM UTC

Hi Alex, 
 
Greetings from the Syncfusion support. 
 
We checked your query with provided the information and you bound Switch component in the Grid editing using editTemplate. By default we can bind the custom component in the Grid column using editTemplate and we should the save Switch Component’s changes using actionBegin event of Grid. 
 
In below code example we have bound the SwitchComponent in the Enabled column using column.editTemplate and we have store the Switch’s changes to “this.rowData” and it will apply to Enabled value in save requestType of actionBegin event. 
 
Please refer the below code example and sample for more information. 
 
[index.js] 
 
editTemplateCB(args) { 
    let isChecked = getValue("Enabled", args); 
    return ( 
      <div> 
        <label htmlFor="checked" style={{ padding: "10px 72px 10px 0" }}> 
          Enabled 
        </label> 
        <SwitchComponent 
          id="checked" 
          checked={isChecked} 
          change={this.changeFn.bind(this)} 
        /> 
      </div> 
    ); 
  } 
  changeFn = e => { 
    if (this.rowData) { 
      this.rowData["Enabled"] = e.checked; 
    } 
  }; 
  actionBegin(args) { 
    if (args.requestType === "beginEdit" || args.requestType === "add") { 
      this.rowData = Object.assign({}, args.rowData); 
    } 
    if (args.requestType === "save") { 
      const Enabled = "Enabled"; 
      args.data[Enabled] = this.rowData[Enabled] ? 1 : 0; 
    } 
  } 
 return ( 
      <div className="control-pane"> 
        <div className="control-section"> 
          <GridComponent 
            dataSource={myData} 
            toolbar={this.toolbarOptions} 
            allowPaging={true} 
            height={400} 
            editSettings={this.editSettings} 
            pageSettings={this.pageSettings} 
            actionBegin={this.actionBegin.bind(this)} 
          > 
            <ColumnsDirective> 
              <ColumnDirective 
                field="ID" 
                headerText="ID" 
                width="120" 
                textAlign="Right" 
                isPrimaryKey={true} 
              /> 
              <ColumnDirective 
                field="Manager" 
                headerText="Manager" 
                width="150" 
              /> 
              <ColumnDirective 
                field="Enabled" 
                headerText="Enabled" 
                width="120" 
                textAlign="Right" 
                editTemplate={this.editTemplateCB.bind(this)} 
                editType="numericedit" 
              /> 
              <ColumnDirective 
                field="CreatedAt" 
                headerText="Created At" 
                editType="datepickeredit" 
                format="yMd" 
                width="170" 
              /> 
            </ColumnsDirective> 
            <Inject services={[Page, Toolbar, Edit]} /> 
          </GridComponent> 
        </div> 
      </div> 
    ); 
 
 
Please get back to us, if you need further assistance. 
 
Regards, 
Balaji Sekar

Marked as answer

AL Alex March 5, 2021 04:17 PM UTC

Hi Balaji Sekar,

Thanks for your explanation and the esample. I have added the missing portions of code an I solved my problem.
I appreciate your help.

Have a nice day
Alex


BS Balaji Sekar Syncfusion Team March 8, 2021 09:32 AM UTC

Hi Alex, 
  
We glad that your issue has been fixed.  
  
Please get back to us if you require further other assistance from us. 
  
Regards, 
Balaji Sekar. 


Loader.
Up arrow icon