Search results

CRUD Data operations

In this section, you will see in detail about how to manipulate data using DataManager. The DataManager can create, update and delete records either in local data source or remote data source.

Each data sources uses different way in handling the CRUD operations and hence DataManager uses data adaptors to manipulate data that can be understood by a particular data source.

Insert

The insert method of DataManager is used to add new record to the data source. For remote data source, the new record will be send along with the request to the server.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { DataManager, Query, ReturnOption } from '@syncfusion/ej2-data';
import { data } from './datasource';
interface Orders { OrderID: number; EmployeeID: number; CustomerID: string; Order_Details: Object[]; }
function Row(props: { item: Orders }) {
    let item: Object = props.item;
    return (<tr>
        <td>{item.OrderID}</td>
        <td>{item.CustomerID}</td>
        <td>{item.EmployeeID}</td>
    </tr>)
}

class DataTable extends React.Component<{}, {}> {
    public dm: DataManager;
    public style: { [x: string]: string };
    public insert: Function;
    constructor(props: Object) {
        super(props);
        this.state = { items: [] };
        this.style = { class: 'e-form' };
        this.dm = new DataManager(data.slice(0, 5));
        this.dm.executeQuery(new Query())
            .then((e: ReturnOption) => {
                this.setState({
                    items: e.result.map((data: Object) => <Row item={data}></Row>)
                });
            });
        this.insert = this.insertUpdate.bind(this)
    }

    public insertUpdate() {
        let orderid: HTMLInputElement = document.getElementById('OrderID') as HTMLInputElement;
        let cusid: HTMLInputElement = document.getElementById('CustomerID') as HTMLInputElement;
        let empid: HTMLInputElement = document.getElementById('EmployeeID') as HTMLInputElement;
        let data: { OrderID: number, CustomerID: string, EmployeeID: number } = {
            OrderID: +orderid.value,
            CustomerID: cusid.value,
            EmployeeID: +empid.value
        };
        if (!data.OrderID) { return; }
        this.dm.insert(data);
        this.dm.executeQuery(new Query())
            .then((e: ReturnOption) => {
                this.setState({
                    items: (e.result as Object[]).map((data: Object) => <Row item={data}></Row>)
                });
            });
    }

    public render() {
        return (<div><div style={this.style}>
            <input type="number" id='OrderID' placeholder="Order ID" />
            <input type="text" id="CustomerID" placeholder="Customer ID" />
            <input type="number" id="EmployeeID" placeholder="Employee ID" />
            <input type="button" value="Insert" id="manipulate" onClick={this.insert} /></div>
            <div><table id='datatable' class='e-table'>
                <thead>
                    <tr><th>Order ID</th><th>Customer ID</th><th>Employee ID</th></tr>
                </thead>
                <tbody>{this.state.items}</tbody>
            </table>
            </div></div>)
    }
}

ReactDOM.render(<DataTable />, document.getElementById('container'));

In remote data sources, when the primary key field is an identity field, then it is advised to return the created data in the response.

Update

The update method of DataManager is used to modify/update a record in the data source. For remote data source, the modified record will be send along with the request to the server.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { DataManager, Query, ReturnOption } from '@syncfusion/ej2-data';
import { data } from './datasource';
interface Orders { OrderID: number; EmployeeID: number; CustomerID: string; Order_Details: Object[]; }
function Row(props: { item: Orders }) {
    let item: Object = props.item;
    return (<tr>
             <td>{item.OrderID}</td>
             <td>{item.CustomerID}</td>
             <td>{item.EmployeeID}</td>
            </tr>)
}

class DataTable extends React.Component<{}, {}> {
    public dm: DataManager;
    public style: { [x: string]: string };
    public insert: Function;
     constructor(props: Object) {
        super(props);
        this.state = { items: [] };
        this.style = { class: 'e-form' };
        this.dm = new DataManager(data.slice(0, 5))
        this.dm.executeQuery(new Query())
        .then((e: ReturnOption) => {
            this.setState({
                items: (e.result as Object[]).map((data: Object) => <Row item={data}></Row>)
            });
        });
        this.insert = this.insertUpdate.bind(this);
     }

     public insertUpdate() {
        let orderid: HTMLInputElement = document.getElementById('OrderID') as HTMLInputElement;
        let cusid: HTMLInputElement = document.getElementById('CustomerID') as HTMLInputElement;
        let empid: HTMLInputElement = document.getElementById('EmployeeID') as HTMLInputElement;
        let data: { OrderID: number, CustomerID: string, EmployeeID: number } = {
            OrderID: +orderid.value,
            CustomerID: cusid.value,
            EmployeeID: +empid.value
        };
        if(!data.OrderID) { return; }
        this.dm.update('OrderID', data);
        this.dm.executeQuery(new Query())
            .then((e: ReturnOption) => {
                this.setState({
                items: (e.result as Object[]).map((data: Object) => <Row item={data}></Row>)
            });
        });
     }

    public render() {
        return (<div><div style={this.style}>
            <input type="number" id='OrderID' placeholder="Order ID"/>
            <input type="text" id="CustomerID" placeholder="Customer ID"/>
            <input type="number" id="EmployeeID" placeholder="Employee ID"/>
            <input type="button" value="Update" id="manipulate" onClick={this.insert} /></div>
            <div><table id='datatable' class='e-table'>
                <thead>
                    <tr><th>Order ID</th><th>Customer ID</th><th>Employee ID</th></tr>
                </thead>
                <tbody>{this.state.items}</tbody>
            </table></div></div>)
    }
}

ReactDOM.render(<DataTable />, document.getElementById('container'));

Primary key name is required by the update method to find the record to be updated.

Remove

The remove method of DataManager is used to remove a record from the data source. For remote data source, the record details such as primary key and data will be send along with the request to the server.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { DataManager, Query, ReturnOption } from '@syncfusion/ej2-data';
import { data } from './datasource';
interface Orders { OrderID: number; EmployeeID: number; CustomerID: string; Order_Details: Object[]; }
function Row(props: { item: Orders }) {
    let item: Object = props.item;
    return (<tr>
             <td>{item.OrderID}</td>
             <td>{item.CustomerID}</td>
             <td>{item.EmployeeID}</td>
            </tr>)
}

class DataTable extends React.Component<{}, {}> {
    public dm: DataManager;
    public style: { [x: string]: string };
    public insert: Function;
     constructor(props: Object) {
        super(props);
        this.state = { items: [] };
        this.style = { class: 'e-form' };
        this.dm = new DataManager(data.slice(0, 5));
        this.dm.executeQuery(new Query())
        .then((e: ReturnOption) => {
            this.setState({
                items: (e.result as Object[]).map((data: Object) => <Row item={data}></Row>)
            });
        });
        this.insert = this.insertUpdate.bind(this);
     }

     public insertUpdate(): void {
        let orderid: HTMLInputElement = document.getElementById('OrderID') as HTMLInputElement;
        let cusid: HTMLInputElement = document.getElementById('CustomerID') as HTMLInputElement;
        let empid: HTMLInputElement = document.getElementById('EmployeeID') as HTMLInputElement;
        let data: { OrderID: number } = {
            OrderID: +orderid.value,
        };
        if(!data.OrderID) { return; }
        this.dm.remove('OrderID', data);
        this.dm.executeQuery(new Query())
            .then((e: ReturnOption) => {
                this.setState({
                items: (e.result as Object[]).map((data: Object) => <Row item={data}></Row>)
            });
        });
     }

    public render() {
        return (<div><div style={this.style}>
            <input type="number" id='OrderID' placeholder="Order ID"/>
            <input type="button" value="Remove" id="manipulate" onClick={this.insert} /></div>
            <div><table id='datatable' class='e-table'>
                <thead>
                    <tr><th>Order ID</th><th>Customer ID</th><th>Employee ID</th></tr>
                </thead>
                <tbody>{this.state.items}</tbody>
            </table></div></div>)
    }
}

ReactDOM.render(<DataTable />, document.getElementById('container'));

Primary key name and its value are required to find the record to be removed.

Batch Edit Operation

DataManager supports batch processing for the CRUD operations. You can use the saveChanges method to batch the edit operation. For remote data source, requests to add, remove and change are handled altogether at a time rather than passing the request separately for each operation.

Source
Preview
index.tsx
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { DataManager, Query, ReturnOption } from '@syncfusion/ej2-data';
import { data } from './datasource';
interface Orders { OrderID: number; EmployeeID: number; CustomerID: string; Order_Details: Object[]; }
function Row(props: { item: Orders }) {
    let item: Object = props.item;
    return (<tr>
        <td>{item.OrderID}</td>
        <td>{item.CustomerID}</td>
        <td>{item.EmployeeID}</td>
    </tr>)
}

class DataTable extends React.Component<{}, {}> {
    public dm: DataManager;
    public style: { [x: string]: string };
    public insert: Function;
    public update: Function;
    public remove: Function;
    public save: Function;
    public changes: { changedRecords: Object[], addedRecords: Object[], deletedRecords: Object[] };
    constructor(props: Object) {
        super(props);
        this.state = { items: [] };
        this.style = { class: 'e-form' };
        this.changes = { changedRecords: [], addedRecords: [], deletedRecords: [] };
        this.dm = new DataManager(data.slice(0, 5));
        this.dm.executeQuery(new Query())
            .then((e: ReturnOption) => {
                this.setState({
                    items: (e.result as Object[]).map((data: Object) => <Row item={data}></Row>)
                });
            });
        this.insert = this.action('addedRecords').bind(this);
        this.update = this.action('changedRecords').bind(this);
        this.remove = this.action('deletedRecords').bind(this);
        this.save = this.saveChanges.bind(this);
    }

    public action(action?: string): Function {
        return function (): void {
            let orderid: HTMLInputElement = document.getElementById('OrderID') as HTMLInputElement;
            let cusid: HTMLInputElement = document.getElementById('CustomerID') as HTMLInputElement;
            let empid: HTMLInputElement = document.getElementById('EmployeeID') as HTMLInputElement;
            let data: { OrderID: number, CustomerID: string, EmployeeID: number } = {
                OrderID: +orderid.value,
                CustomerID: cusid.value,
                EmployeeID: +empid.value
            };
            if (!data.OrderID) { return; }
            this.changes[action].push(data);
            orderid.value = cusid.value = empid.value = null;
        };
    }

    public saveChanges(): void {
        this.dm.saveChanges(this.changes);
        this.dm.executeQuery(new Query())
            .then((e: ReturnOption) => {
                this.setState({
                    items: (e.result as Object[]).map((data: Object) => <Row item={data}></Row>)
                });
            });
        this.changes = { changedRecords: [], addedRecords: [], deletedRecords: [] };
    }

    public render() {
        return (<div><div style={this.style}>
            <input type="number" id='OrderID' placeholder="Order ID" />
            <input type="text" id="CustomerID" placeholder="Customer ID" />
            <input type="number" id="EmployeeID" placeholder="Employee ID" />
            <input type="button" value="Insert" onClick={this.insert} />
            <input type="button" value="Update" onClick={this.update} />
            <input type="button" value="Remove" onClick={this.remove} /></div>
            <div style={this.style}>
                <label>Click to Save changes:</label>
                <input type="button" value="Save Changes" onClick={this.save} /></div>
            <div><table id='datatable' class='e-table'>
                <thead>
                    <tr><th>Order ID</th><th>Customer ID</th><th>Employee ID</th></tr>
                </thead>
                <tbody>{this.state.items}</tbody>
            </table></div></div>)
    }
}

ReactDOM.render(<DataTable />, document.getElementById('container'));