- Insert
- Update
- Remove
- Batch Edit Operation
Contact Support
Manipulation in Angular Data component
30 Apr 202424 minutes to read
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.
import { NgModule } from '@angular/core'
import { FormsModule } from '@angular/forms'
import { BrowserModule } from '@angular/platform-browser'
import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { DataManager, Query, ReturnOption } from '@syncfusion/ej2-data';
import { data } from './datasource';
@Component({
imports: [
FormsModule , CommonModule
],
standalone: true,
selector: 'app-root',
templateUrl: './app.template.html',
styles: [`
.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;
}
.e-form {
display: block;
padding-bottom: 10px;
}
.e-form input {
width: 15%;
}
`]
})
export class AppComponent implements OnInit {
public items?: object[] | any;
public edit?: { OrderID: string, CustomerID: string, EmployeeID: string } | any;
public dm?: DataManager;
public text?: string;
public show = true;
public ngOnInit(): void {
this.text = 'Insert';
this.edit = { OrderID: null, CustomerID: null, EmployeeID: null } as any;
this.dm = new DataManager(data.slice(0, 5));
this.dm.executeQuery(new Query())
.then((e: ReturnOption) => this.items = e.result as object[]).catch((e) => true);
}
public insert(): void {
(this.dm as DataManager).insert({
OrderID: this.edit?.OrderID,
CustomerID: this.edit?.CustomerID,
EmployeeID: this.edit?.EmployeeID
});
(this.dm as DataManager).executeQuery(new Query())
.then((e: ReturnOption) => this.items = e.result as object[]).catch((e) => true);
}
}
<div class="e-form">
<input type="number" id='OrderID' placeholder="Order ID" [(ngModel)]="edit.OrderID" />
<input type="text" id="CustomerID" placeholder="Customer ID" [(ngModel)]="edit.CustomerID" *ngIf="show"/>
<input type="number" id="EmployeeID" placeholder="Employee ID" [(ngModel)]="edit.EmployeeID" *ngIf="show"/>
<input type="button" [value]="text" id="manipulate" (click)="insert()" />
</div>
<table class='e-table'>
<tr>
<th>Order ID</th>
<th>Customer ID</th>
<th>Employee ID</th>
</tr>
<tr *ngFor="let item of items">
<td></td>
<td></td>
<td></td>
</tr>
</table>
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
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
](https://ej2.syncfusion.com/documentation/api/data/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.
import { NgModule } from '@angular/core'
import { FormsModule } from '@angular/forms'
import { BrowserModule } from '@angular/platform-browser'
import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { DataManager, Query, ReturnOption } from '@syncfusion/ej2-data';
import { data } from './datasource';
@Component({
imports: [
FormsModule, CommonModule
],
standalone: true,
selector: 'app-root',
templateUrl: './app.template.html',
styles: [`
.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;
}
.e-form {
display: block;
padding-bottom: 10px;
}
.e-form input {
width: 15%;
}
`]
})
export class AppComponent implements OnInit {
public items?: object[] | any;
public edit?: { OrderID: string, CustomerID: string, EmployeeID: string } | any;
public dm?: DataManager;
public text?: string;
public show = true;
public ngOnInit(): void {
this.text = 'Update';
this.edit = { OrderID: null, CustomerID: null, EmployeeID: null } as any;
this.dm = new DataManager(data.slice(0, 5));
this.dm.executeQuery(new Query())
.then((e: ReturnOption) => this.items = e.result as object[]).catch((e) => true);
}
public insert(): void {
(this.dm as DataManager).update('OrderID', {
OrderID: this.edit?.OrderID,
CustomerID: this.edit?.CustomerID,
EmployeeID: this.edit?.EmployeeID
});
(this.dm as DataManager).executeQuery(new Query())
.then((e: ReturnOption) => this.items = e.result as object[]).catch((e) => true);
}
}
<div class="e-form">
<input type="number" id='OrderID' placeholder="Order ID" [(ngModel)]="edit.OrderID" />
<input type="text" id="CustomerID" placeholder="Customer ID" [(ngModel)]="edit.CustomerID" *ngIf="show"/>
<input type="number" id="EmployeeID" placeholder="Employee ID" [(ngModel)]="edit.EmployeeID" *ngIf="show"/>
<input type="button" [value]="text" id="manipulate" (click)="insert()" />
</div>
<table class='e-table'>
<tr>
<th>Order ID</th>
<th>Customer ID</th>
<th>Employee ID</th>
</tr>
<tr *ngFor="let item of items">
<td></td>
<td></td>
<td></td>
</tr>
</table>
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
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.
import { NgModule } from '@angular/core'
import { FormsModule } from '@angular/forms'
import { BrowserModule } from '@angular/platform-browser'
import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { DataManager, Query, ReturnOption } from '@syncfusion/ej2-data';
import { data } from './datasource';
@Component({
imports: [
FormsModule , CommonModule
],
standalone: true,
selector: 'app-root',
templateUrl: './app.template.html',
styles: [`
.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;
}
.e-form {
display: block;
padding-bottom: 10px;
}
.e-form input {
width: 15%;
}
`]
})
export class AppComponent implements OnInit {
public items?: object[] | any;
public edit?: { OrderID: string, CustomerID: string, EmployeeID: string } | any;
public dm?: DataManager;
public text?: string;
public show = false;
public ngOnInit(): void {
this.text = 'Remove';
this.edit = { OrderID: null, CustomerID: null, EmployeeID: null } as any;
this.dm = new DataManager(data.slice(0, 5));
this.dm.executeQuery(new Query())
.then((e: ReturnOption) => this.items = e.result as object[]).catch((e) => true);
}
public insert(): void {
(this.dm as DataManager).remove( 'OrderID', {
OrderID: this.edit.OrderID
});
(this.dm as DataManager).executeQuery(new Query())
.then((e: ReturnOption) => this.items = e.result as object[]).catch((e) => true);
}
}
<div class="e-form">
<input type="number" id='OrderID' placeholder="Order ID" [(ngModel)]="edit.OrderID" />
<input type="text" id="CustomerID" placeholder="Customer ID" [(ngModel)]="edit.CustomerID" *ngIf="show"/>
<input type="number" id="EmployeeID" placeholder="Employee ID" [(ngModel)]="edit.EmployeeID" *ngIf="show"/>
<input type="button" [value]="text" id="manipulate" (click)="insert()" />
</div>
<table class='e-table'>
<tr>
<th>Order ID</th>
<th>Customer ID</th>
<th>Employee ID</th>
</tr>
<tr *ngFor="let item of items">
<td></td>
<td></td>
<td></td>
</tr>
</table>
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
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.
import { NgModule } from '@angular/core'
import { FormsModule } from '@angular/forms'
import { BrowserModule } from '@angular/platform-browser'
import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { DataManager, Query, ReturnOption } from '@syncfusion/ej2-data';
import { data } from './datasource';
@Component({
imports: [
FormsModule, CommonModule
],
standalone: true,
selector: 'app-root',
templateUrl: './app.template.html',
styles: [`
.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;
}
.e-form {
display: block;
padding-bottom: 10px;
}
.e-form input {
width: 15%;
}
`]
})
export class AppComponent implements OnInit {
public items?: object[] | any;
public edit?: { OrderID: string, CustomerID: string, EmployeeID: string } | any;
public dm?: DataManager;
public changes: { changedRecords: object[], addedRecords: object[], deletedRecords: object[] } =
{ changedRecords: [], addedRecords: [], deletedRecords: [] };
public text?: string;
public ngOnInit(): void {
this.text = 'Update';
this.edit = { OrderID: null, CustomerID: null, EmployeeID: null } as any;
this.dm = new DataManager(data.slice(0, 5));
this.dm.executeQuery(new Query())
.then((e: ReturnOption) => this.items = e.result as object[]).catch((e) => true);
}
public save(): void {
(this.dm as DataManager).saveChanges(this.changes);
(this.dm as DataManager).executeQuery(new Query())
.then((e: ReturnOption) => this.items = e.result as object[]).catch((e) => true);
this.changes = { changedRecords: [], addedRecords: [], deletedRecords: [] };
}
public insert(action: string): void {
(this.changes as any)[action].push({
OrderID: this.edit?.OrderID,
CustomerID: this.edit?.CustomerID,
EmployeeID: this.edit?.EmployeeID
});
this.edit = { OrderID: null, CustomerID: null, EmployeeID: null } as any;
}
}
<div class="e-form">
<input type="number" id='OrderID' placeholder="Order ID" [(ngModel)]="edit.OrderID" />
<input type="text" id="CustomerID" placeholder="Customer ID" [(ngModel)]="edit.CustomerID"/>
<input type="number" id="EmployeeID" placeholder="Employee ID" [(ngModel)]="edit.EmployeeID"/>
<input type="button" value="Insert" (click)="insert('addedRecords')" />
<input type="button" value="Update" (click)="insert('changedRecords')" />
<input type="button" value="Remove" (click)="insert('deletedRecords')" />
</div>
<div class="e-form">
<label>Click to Save changes: </label>
<input type="button" value="Save Changes" (click)="save()" />
</div>
<table class='e-table'>
<tr>
<th>Order ID</th>
<th>Customer ID</th>
<th>Employee ID</th>
</tr>
<tr *ngFor="let item of items">
<td></td>
<td></td>
<td></td>
</tr>
</table>
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));