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
app.vue
<template>
  <div id="app">
    <div class="e-form">
        <input type="number" v-model="orderID" id='OrderID' placeholder="Order ID"/>
        <input type="text" v-model="customerID" id="CustomerID" placeholder="Customer ID"/>
        <input type="number" v-model="employeeID" id="EmployeeID" placeholder="Employee ID"/>
        <input type="button" value="Insert" v-on:click='onclick' id="manipulate"/>
    </div>
    <table class='e-table'>
        <tr><th>Order ID</th><th>Customer ID</th><th>Employee ID</th></tr>
        <tr v-for="item in items">
            <td>{{item.OrderID}}</td><td>{{item.CustomerID}}</td><td>{{item.EmployeeID}}</td>
        </tr>
    </table>
  </div>
</template>

<script>
import Vue from 'vue';
import { DataManager, Query } from '@syncfusion/ej2-data';
import {data} from './datasource.js';

export default {
  data () {
    return {
        items: [],
        dataManager: new DataManager(data.slice(0, 4)),
        orderID: null,
        customerID: null,
        employeeID: null
    }
  },
  created: function() {
      this.dataManager.executeQuery(new Query()).then((e) =>{
            this.items = e.result;
      });
  },
  methods: {
    onclick: function() {
        if (!this.orderID) { return; }
        this.dataManager.insert({OrderID: this.orderID, CustomerID: this.customerID, EmployeeID: this.employeeID});
        this.dataManager.executeQuery(new Query()).then((e) => {
            this.items = e.result;
        });
    }
  }
}
</script>
<style>
    .e-form {
        display: block;
        padding-bottom: 10px;
    }
    .e-form input {
        width: 15%;
    }
  .e-table {
      border: solid 1px #e0e0e0;
      border-collapse: collapse;
      font-family: Roboto;
  }

  .e-table td, .e-table th {
      border-style: solid;
      border-width: 1px 0 0;
      border-color: #e0e0e0;
      display: table-cell;
      font-size: 14px;
      line-height: 20px;
      overflow: hidden;
      padding: 8px 21px;
      vertical-align: middle;
      white-space: nowrap;
      width: auto;
  }
</style>


{% endraw %}

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
app.vue
<template>
  <div id="app">
    <div class="e-form">
        <input type="number" v-model="orderID" id='OrderID' placeholder="Order ID"/>
        <input type="text" v-model="customerID" id="CustomerID" placeholder="Customer ID"/>
        <input type="number" v-model="employeeID" id="EmployeeID" placeholder="Employee ID"/>
        <input type="button" value="Update" v-on:click='onclick' id="manipulate"/>
    </div>
    <table class='e-table'>
        <tr><th>Order ID</th><th>Customer ID</th><th>Employee ID</th></tr>
        <tr v-for="item in items">
            <td>{{item.OrderID}}</td><td>{{item.CustomerID}}</td><td>{{item.EmployeeID}}</td>
        </tr>
    </table>
  </div>
</template>

<script>
import Vue from 'vue';
import { DataManager, Query } from '@syncfusion/ej2-data';
import {data} from './datasource.js';

export default {
  data () {
    return {
        items: [],
        dataManager: new DataManager(data.slice(0, 4)),
        orderID: null,
        customerID: null,
        employeeID: null
    }
  },
  created: function() {
      this.dataManager.executeQuery(new Query()).then((e) =>{
            this.items = e.result;
      });
  },
  methods: {
    onclick: function() {
        if (!this.orderID) { return; }
        this.dataManager.update('OrderID', {OrderID: +this.orderID, CustomerID: this.customerID, EmployeeID: +this.employeeID});
        this.dataManager.executeQuery(new Query()).then((e) => {
            this.items = e.result;
        });
    }
  }
}
</script>
<style>
    .e-form {
        display: block;
        padding-bottom: 10px;
    }
    .e-form input {
        width: 15%;
    }
  .e-table {
      border: solid 1px #e0e0e0;
      border-collapse: collapse;
      font-family: Roboto;
  }

  .e-table td, .e-table th {
      border-style: solid;
      border-width: 1px 0 0;
      border-color: #e0e0e0;
      display: table-cell;
      font-size: 14px;
      line-height: 20px;
      overflow: hidden;
      padding: 8px 21px;
      vertical-align: middle;
      white-space: nowrap;
      width: auto;
  }
</style>


{% endraw %}

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
app.vue
<template>
  <div id="app">
    <div class="e-form">
        <input type="number" v-model="orderID" id='orderID' placeholder="Order ID"/>
        <input type="button" value="Remove" v-on:click='onclick' id="manipulate"/>
    </div>
    <table class='e-table'>
        <tr><th>Order ID</th><th>Customer ID</th><th>Employee ID</th></tr>
        <tr v-for="item in items">
            <td>{{item.OrderID}}</td><td>{{item.CustomerID}}</td><td>{{item.EmployeeID}}</td>
        </tr>
    </table>
  </div>
</template>

<script>
import Vue from 'vue';
import { DataManager, Query } from '@syncfusion/ej2-data';
import {data} from './datasource.js';

export default {
  data () {
    return {
        items: [],
        dataManager: new DataManager(data.slice(0, 4)),
        orderID: null
    }
  },
  created: function() {
      this.dataManager.executeQuery(new Query()).then((e) =>{
            this.items = e.result;
      });
  },
  methods: {
    onclick: function() {
        if (!this.orderID) { return; }
        this.dataManager.remove('OrderID', {OrderID: +this.orderID});
        this.dataManager.executeQuery(new Query()).then((e) => {
            this.items = e.result;
        });
    }
  }
}
</script>
<style>
    .e-form {
        display: block;
        padding-bottom: 10px;
    }
    .e-form input {
        width: 15%;
    }
  .e-table {
      border: solid 1px #e0e0e0;
      border-collapse: collapse;
      font-family: Roboto;
  }

  .e-table td, .e-table th {
      border-style: solid;
      border-width: 1px 0 0;
      border-color: #e0e0e0;
      display: table-cell;
      font-size: 14px;
      line-height: 20px;
      overflow: hidden;
      padding: 8px 21px;
      vertical-align: middle;
      white-space: nowrap;
      width: auto;
  }
</style>


{% endraw %}

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
app.vue
<template>
  <div id="app">
    <div class="e-form">
        <input type="number" v-model="orderID" id='OrderID' placeholder="Order ID"/>
        <input type="text" v-model="customerID" id="CustomerID" placeholder="Customer ID"/>
        <input type="number" v-model="employeeID" id="EmployeeID" placeholder="Employee ID"/>
        <input type="button" value="Update" v-on:click='update' id="manipulate"/>
        <input type="button" value="Add" v-on:click='add' id="manipulate"/>
        <input type="button" value="Remove" v-on:click='remove' id="manipulate"/>
    </div>
    <div class="e-form">
        <label>Click to Save changes:</label>
        <input type="button" value="Save Changes" id="save" v-on:click='save' style="width: 20%"/>
    </div>
    <table class='e-table'>
        <tr><th>Order ID</th><th>Customer ID</th><th>Employee ID</th></tr>
        <tr v-for="item in items">
            <td>{{item.OrderID}}</td><td>{{item.CustomerID}}</td><td>{{item.EmployeeID}}</td>
        </tr>
    </table>
  </div>
</template>

<script>
import Vue from 'vue';
import { DataManager, Query } from '@syncfusion/ej2-data';
import {data} from './datasource.js';

export default {
  data () {
    return {
        items: [],
        dataManager: new DataManager(data.slice(0, 4)),
        orderID: null,
        customerID: null,
        employeeID: null,
        changes: { changedRecords: [], addedRecords: [], deletedRecords: [] }
    }
  },
  created: function() {
      this.dataManager.executeQuery(new Query()).then((e) =>{
            this.items = e.result;
      });
  },
  methods: {
    save: function() {
        this.dataManager.saveChanges(this.changes, 'OrderID');
        this.changes = { changedRecords: [], addedRecords: [], deletedRecords: [] };
        this.dataManager.executeQuery(new Query()).then((e) => {
            this.items = e.result;
        });
    },
    add: function() {
        this.changes.addedRecords.push({
            OrderID: +this.orderID,
            CustomerID: this.customerID,
            EmployeeID: +this.employeeID
        });
        this.orderID = this.customerID = this.employeeID = null;
    },
    remove: function() {
        this.changes.deletedRecords.push({
            OrderID: +this.orderID,
            CustomerID: this.customerID,
            EmployeeID: +this.employeeID
        });
        this.orderID = this.customerID = this.employeeID = null;
    },
    update: function() {
        changes.changedRecords.push({
            OrderID: +this.orderID,
            CustomerID: this.customerID,
            EmployeeID: +this.employeeID
        });
        this.orderID = this.customerID = this.employeeID = null;
    }
  }
}
</script>
<style>
    .e-form {
        display: block;
        padding-bottom: 10px;
    }
    .e-form input {
        width: 15%;
    }
  .e-table {
      border: solid 1px #e0e0e0;
      border-collapse: collapse;
      font-family: Roboto;
  }

  .e-table td, .e-table th {
      border-style: solid;
      border-width: 1px 0 0;
      border-color: #e0e0e0;
      display: table-cell;
      font-size: 14px;
      line-height: 20px;
      overflow: hidden;
      padding: 8px 21px;
      vertical-align: middle;
      white-space: nowrap;
      width: auto;
  }
</style>


{% endraw %}