Server Actions in React In-place Editor

03 Sep 202615 minutes to read

When submitting React In-place Editor values to the server, the primaryKey property is required for remote data operations; otherwise, the action will not be performed.

If the URL property is empty, data submission is handled locally and the actionSuccess event triggers with null as the argument value.

The following arguments are passed to the server when submit actions are performed.

Arguments Description
value The edited value to be processed, such as database updates.
primaryKey The identifier for mapping the value on the server, such as the record ID.
name The field name for server-side mapping, such as database column names.

Find the following sample server codes for defining models and controller functions to configure processing data.

  public class SubmitModel {
      public string Name { get; set; }
      public string PrimaryKey { get; set; }
      public string Value { get; set; }
  }
public IEnumerable<SubmitModel> UpdateData([FromBody]SubmitModel value) {
  // User can process data
  return value;
}
  • When server submission is successful, the actionSuccess event is triggered with the returned server data.

  • If the server does not respond or an error occurs, the actionFailure event is triggered and the value is not updated in the Editor.

In the following sample, both actionSuccess and actionFailure events are configured. The actionSuccess event triggers when the value is successfully submitted to the server, and the returned value is converted to chips.

[Class-component]

import { Inject, InPlaceEditorComponent, MultiSelect } from '@syncfusion/ej2-react-inplace-editor';
import * as React from 'react';
class App extends React.Component {
    inplaceEditorObj;
    serviceUrl = 'https://ej2services.syncfusion.com/development/web-services/api/Editor/UpdateData';
    frameWorkList = ['Android', 'JavaScript', 'jQuery', 'TypeScript', 'Angular', 'React', 'Vue', 'Ionic'];
    value = ['JavaScript', 'jQuery'];
    model = { mode: 'Box', dataSource: this.frameWorkList, placeholder: 'Select skill' };
    chipOnCreate() {
        this.inplaceEditorObj.element.querySelector('.e-editable-value').innerHTML = this.chipCreation(this.inplaceEditorObj.value, true);
    }
    onActionSuccess(e) {
        e.value = this.chipCreation(e.value.split(','), true);
    }
    onActionFailure(e) {
        e.value = this.chipCreation(e.value.split(','), false);
    }
    chipCreation(data, isSuccess) {
        let value = '<div class="e-chip-list">';
        [].slice.call(data).forEach((val) => {
            value += '<div class="e-chip"> <span class="e-chip-text' + (!isSuccess ? 'e-highlight' : '') + '"> ' + val + '</span></div>';
        });
        value += '</div>';
        return value;
    }
    create() {
        this.chipOnCreate();
    }
    render() {
        return (<div id='container'>
          <span className="content-title"> Select frameworks: </span>
          <InPlaceEditorComponent ref={(text) => { this.inplaceEditorObj = text; }} id='multiselect' data-underline='false' mode='Inline' type='MultiSelect' value={this.value} name='skill' url={this.serviceUrl} primaryKey='FrameWork' adaptor='UrlAdaptor' model={this.model} actionSuccess={this.onActionSuccess = this.onActionSuccess.bind(this)} actionFailure={this.onActionFailure = this.onActionFailure.bind(this)} created={this.create = this.create.bind(this)}>
          <Inject services={[MultiSelect]}/>
          </InPlaceEditorComponent>
      </div>);
    }
}
export default App;
import { ActionEventArgs, Inject, InPlaceEditorComponent, MultiSelect } from '@syncfusion/ej2-react-inplace-editor';
import * as React from 'react';

class App extends React.Component<{},{}> {
  public inplaceEditorObj: InPlaceEditorComponent;
  
  public serviceUrl = 'https://ej2services.syncfusion.com/development/web-services/api/Editor/UpdateData';
  public frameWorkList = ['Android', 'JavaScript', 'jQuery', 'TypeScript', 'Angular', 'React', 'Vue', 'Ionic'];
  public value = ['JavaScript', 'jQuery'];
  public model = { mode: 'Box', dataSource: this.frameWorkList, placeholder: 'Select skill' };

  public chipOnCreate(): void {
    (this.inplaceEditorObj as any).element.querySelector('.e-editable-value').innerHTML = this.chipCreation((this.inplaceEditorObj.value as string[]), true);
  }

  public onActionSuccess(e: ActionEventArgs): void {
    e.value = this.chipCreation(e.value.split(','), true);
  }

  public onActionFailure(e: ActionEventArgs): void {
    e.value = this.chipCreation(e.value.split(','), false);
  }

  public chipCreation(data: string[], isSuccess: boolean): string {
    let value: string = '<div class="e-chip-list">';
    [].slice.call(data).forEach((val: string) => {
        value += '<div class="e-chip"> <span class="e-chip-text' + (!isSuccess ? 'e-highlight' : '') + '"> ' + val + '</span></div>';
    });
    value += '</div>';
    return value;
  }

  public create(): void {
    this.chipOnCreate();
  }

  public render() {
    return (
      <div id='container'>
          <span className="content-title"> Select frameworks: </span>
          <InPlaceEditorComponent ref={(text) => { this.inplaceEditorObj = text! }} id='multiselect' data-underline='false' mode='Inline' type='MultiSelect' value={this.value} name='skill' url={this.serviceUrl} primaryKey='FrameWork' adaptor='UrlAdaptor' model={this.model} actionSuccess={ this.onActionSuccess=this.onActionSuccess.bind(this) } actionFailure={ this.onActionFailure=this.onActionFailure.bind(this) } created={this.create=this.create.bind(this)} >
          <Inject services={[MultiSelect]} />
          </InPlaceEditorComponent>
      </div>
    );
  }
}

export default App;

[Functional-component]

import { Inject, InPlaceEditorComponent, MultiSelect } from '@syncfusion/ej2-react-inplace-editor';
import * as React from 'react';
function App() {
    let inplaceEditorObj;
    const serviceUrl = 'https://ej2services.syncfusion.com/development/web-services/api/Editor/UpdateData';
    const frameWorkList = ['Android', 'JavaScript', 'jQuery', 'TypeScript', 'Angular', 'React', 'Vue', 'Ionic'];
    const value = ['JavaScript', 'jQuery'];
    const model = { mode: 'Box', dataSource: frameWorkList, placeholder: 'Select skill' };
    function chipOnCreate() {
        inplaceEditorObj.element.querySelector('.e-editable-value').innerHTML = chipCreation(inplaceEditorObj.value, true);
    }
    function onActionSuccess(e) {
        e.value = chipCreation(e.value.split(','), true);
    }
    function onActionFailure(e) {
        e.value = chipCreation(e.value.split(','), false);
    }
    function chipCreation(data, isSuccess) {
        let value = '<div class="e-chip-list">';
        [].slice.call(data).forEach((val) => {
            value += '<div class="e-chip"> <span class="e-chip-text' + (!isSuccess ? 'e-highlight' : '') + '"> ' + val + '</span></div>';
        });
        value += '</div>';
        return value;
    }
    function create() {
        chipOnCreate();
    }
    return (<div id='container'>
          <span className="content-title"> Select frameworks: </span>
          <InPlaceEditorComponent ref={(text) => { inplaceEditorObj = text; }} id='multiselect' data-underline='false' mode='Inline' type='MultiSelect' value={value} name='skill' url={serviceUrl} primaryKey='FrameWork' adaptor='UrlAdaptor' model={model} actionSuccess={onActionSuccess = onActionSuccess.bind(this)} actionFailure={onActionFailure = onActionFailure.bind(this)} created={create = create.bind(this)}>
          <Inject services={[MultiSelect]}/>
          </InPlaceEditorComponent>
      </div>);
}
export default App;
import { ActionEventArgs, Inject, InPlaceEditorComponent, MultiSelect } from '@syncfusion/ej2-react-inplace-editor';
import * as React from 'react';

function App () {
  let inplaceEditorObj: InPlaceEditorComponent;
  
  const serviceUrl : string = 'https://ej2services.syncfusion.com/development/web-services/api/Editor/UpdateData';
  const frameWorkList : string[] = ['Android', 'JavaScript', 'jQuery', 'TypeScript', 'Angular', 'React', 'Vue', 'Ionic'];
  const value : string[]= ['JavaScript', 'jQuery'];
  const model : object = { mode: 'Box', dataSource: frameWorkList, placeholder: 'Select skill' };

  function chipOnCreate(): void {
    (inplaceEditorObj as any).element.querySelector('.e-editable-value').innerHTML = chipCreation((inplaceEditorObj.value as string[]), true);
  }

  function onActionSuccess(e: ActionEventArgs): void {
    e.value = chipCreation(e.value.split(','), true);
  }

  function onActionFailure(e: ActionEventArgs): void {
    e.value = chipCreation(e.value.split(','), false);
  }

  function chipCreation(data: string[], isSuccess: boolean): string {
    let value: string = '<div class="e-chip-list">';
    [].slice.call(data).forEach((val: string) => {
        value += '<div class="e-chip"> <span class="e-chip-text' + (!isSuccess ? 'e-highlight' : '') + '"> ' + val + '</span></div>';
    });
    value += '</div>';
    return value;
  }

  function create(): void {
    chipOnCreate();
  }

    return (
      <div id='container'>
          <span className="content-title"> Select frameworks: </span>
          <InPlaceEditorComponent ref={(text) => { inplaceEditorObj = text! }} id='multiselect' data-underline='false' mode='Inline' type='MultiSelect' value={value} name='skill' url={serviceUrl} primaryKey='FrameWork' adaptor='UrlAdaptor' model={model} actionSuccess={ onActionSuccess=onActionSuccess.bind(this) } actionFailure={ onActionFailure=onActionFailure.bind(this) } created={create=create.bind(this)} >
          <Inject services={[MultiSelect]} />
          </InPlaceEditorComponent>
      </div>
    );
}

export default App;

See Also