Loading...
Loading...
Compare original and translation side by side
| Concept | When to Use | Key Methods |
|---|---|---|
| Local Data Binding | Working with in-memory JSON arrays | JsonAdaptor, executeLocal |
| Remote Data Binding | Calling API endpoints for data | UrlAdaptor, WebApiAdaptor, executeQuery |
| Query Construction | Filtering, sorting, grouping data | from(), where(), select(), sortBy(), take() |
| CRUD Operations | Managing records (add, edit, delete) | insert(), update(), remove(), saveChanges() |
| Adaptor Selection | Choosing the right data source type | 10 adaptors with decision tree |
| Error Handling | Managing failure scenarios | .catch(), try/catch, error status checking |
| Offline Mode | Working without server connection | offline property, localStorage |
| Caching | Improving performance | enableCache, cache clearing strategies |
| Middleware | Customizing requests/responses | applyPreRequestMiddlewares, applyPostMiddlewares |
| Async Patterns | Sequential and parallel operations | async/await, Promise.all(), then/catch |
| 概念 | 使用场景 | 核心方法 |
|---|---|---|
| 本地数据绑定 | 处理内存中的 JSON 数组 | JsonAdaptor, executeLocal |
| 远程数据绑定 | 调用 API 端点获取数据 | UrlAdaptor, WebApiAdaptor, executeQuery |
| 查询构造 | 过滤、排序、分组数据 | from(), where(), select(), sortBy(), take() |
| CRUD 操作 | 管理记录(新增、编辑、删除) | insert(), update(), remove(), saveChanges() |
| Adaptor 选型 | 选择适配的数据源类型 | 10 个 Adaptor 及决策树 |
| 错误处理 | 处理故障场景 | .catch(), try/catch, 错误状态校验 |
| 离线模式 | 无服务器连接时工作 | offline 属性, localStorage |
| 缓存 | 提升性能 | enableCache, 缓存清理策略 |
| 中间件 | 自定义请求/响应逻辑 | applyPreRequestMiddlewares, applyPostMiddlewares |
| 异步模式 | 串行和并行操作 | async/await, Promise.all(), then/catch |
npm install @syncfusion/ej2-dataimport { Component, OnInit } from '@angular/core';
import { DataManager, Query, JsonAdaptor, ReturnOption } from '@syncfusion/ej2-data';
@Component({
selector: 'app-data-demo',
templateUrl: './data-demo.component.html',
styleUrls: ['./data-demo.component.css']
})
export class DataDemoComponent implements OnInit {
public orders: object[];
ngOnInit(): void {
// Local data
const data = [
{ OrderID: 10248, CustomerID: 'VINET', EmployeeID: 5, ShipCity: 'Reims' },
{ OrderID: 10249, CustomerID: 'TOMSP', EmployeeID: 6, ShipCity: 'Münster' },
{ OrderID: 10250, CustomerID: 'HANAR', EmployeeID: 4, ShipCity: 'Rio de Janeiro' }
];
// Create DataManager with local data
const dataManager = new DataManager({
json: data,
adaptor: new JsonAdaptor()
});
// Execute query
this.orders = dataManager.executeLocal(
new Query().where('EmployeeID', 'equal', 5)
);
}
}<table>
<thead>
<tr>
<th>Order ID</th>
<th>Customer ID</th>
<th>Employee ID</th>
<th>Ship City</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let order of orders">
<td>{{ order.OrderID }}</td>
<td>{{ order.CustomerID }}</td>
<td>{{ order.EmployeeID }}</td>
<td>{{ order.ShipCity }}</td>
</tr>
</tbody>
</table>npm install @syncfusion/ej2-dataimport { Component, OnInit } from '@angular/core';
import { DataManager, Query, JsonAdaptor, ReturnOption } from '@syncfusion/ej2-data';
@Component({
selector: 'app-data-demo',
templateUrl: './data-demo.component.html',
styleUrls: ['./data-demo.component.css']
})
export class DataDemoComponent implements OnInit {
public orders: object[];
ngOnInit(): void {
// 本地数据
const data = [
{ OrderID: 10248, CustomerID: 'VINET', EmployeeID: 5, ShipCity: 'Reims' },
{ OrderID: 10249, CustomerID: 'TOMSP', EmployeeID: 6, ShipCity: 'Münster' },
{ OrderID: 10250, CustomerID: 'HANAR', EmployeeID: 4, ShipCity: 'Rio de Janeiro' }
];
// 基于本地数据创建 DataManager
const dataManager = new DataManager({
json: data,
adaptor: new JsonAdaptor()
});
// 执行查询
this.orders = dataManager.executeLocal(
new Query().where('EmployeeID', 'equal', 5)
);
}
}<table>
<thead>
<tr>
<th>Order ID</th>
<th>Customer ID</th>
<th>Employee ID</th>
<th>Ship City</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let order of orders">
<td>{{ order.OrderID }}</td>
<td>{{ order.CustomerID }}</td>
<td>{{ order.EmployeeID }}</td>
<td>{{ order.ShipCity }}</td>
</tr>
</tbody>
</table>const dataManager = new DataManager({
url: 'url',
adaptor: new WebApiAdaptor()
});
// Using Promise
dataManager.executeQuery(new Query().take(10))
.then((result: ReturnOption) => {
this.orders = result.result;
})
.catch((error) => {
console.error('Failed to fetch orders:', error);
});const dataManager = new DataManager({
url: 'url',
adaptor: new WebApiAdaptor()
});
// 使用 Promise
dataManager.executeQuery(new Query().take(10))
.then((result: ReturnOption) => {
this.orders = result.result;
})
.catch((error) => {
console.error('Failed to fetch orders:', error);
});async fetchOrders(): Promise<void> {
try {
const result = await this.dataManager.executeQuery(
new Query().take(10)
);
this.orders = result.result;
} catch (error) {
console.error('Error:', error);
}
}async fetchOrders(): Promise<void> {
try {
const result = await this.dataManager.executeQuery(
new Query().take(10)
);
this.orders = result.result;
} catch (error) {
console.error('Error:', error);
}
}const query = new Query()
.where('EmployeeID', 'equal', 5)
.where('ShipCity', 'startswith', 'Rio');
const result = dataManager.executeLocal(query);const query = new Query()
.where('EmployeeID', 'equal', 5)
.where('ShipCity', 'startswith', 'Rio');
const result = dataManager.executeLocal(query);const query = new Query()
.select(['OrderID', 'CustomerID', 'EmployeeID'])
.where('EmployeeID', 'greaterThan', 3)
.sortBy('OrderID')
.take(10)
.skip(0);
dataManager.executeQuery(query).then((result: ReturnOption) => {
this.orders = result.result;
this.totalRecords = result.count;
});const query = new Query()
.select(['OrderID', 'CustomerID', 'EmployeeID'])
.where('EmployeeID', 'greaterThan', 3)
.sortBy('OrderID')
.take(10)
.skip(0);
dataManager.executeQuery(query).then((result: ReturnOption) => {
this.orders = result.result;
this.totalRecords = result.count;
});| Property | Type | Purpose |
|---|---|---|
| | Local data array |
| | Remote service endpoint |
| | Data source type handler |
| | Custom HTTP headers |
| | Enable offline mode |
| | Enable response caching |
| | Enable cross-domain requests |
| | Paging record count |
| | Cache TTL in milliseconds (default: infinite) |
| | Properties to exclude from persistence |
| | Enable timezone offset handling |
| 属性 | 类型 | 用途 |
|---|---|---|
| | 本地数据数组 |
| | 远程服务端点 |
| | 数据源类型处理器 |
| | 自定义 HTTP 请求头 |
| | 开启离线模式 |
| | 开启响应缓存 |
| | 开启跨域请求 |
| | 分页记录数 |
| | 缓存 TTL(单位毫秒,默认永不过期) |
| | 持久化时要排除的属性 |
| | 开启时区偏移处理 |
dataManager.executeQuery(query)
.then((result: ReturnOption) => {
if (result.result) {
this.items = result.result;
}
})
.catch((error: any) => {
if (error.status === 401) {
// Redirect to login
console.error('Unauthorized');
} else if (error.status === 403) {
// Access denied
console.error('Forbidden');
} else if (error.status === 500) {
// Server error
console.error('Server error');
} else if (error.status === 0) {
// Network error
console.error('Network error - check connectivity');
}
});dataManager.executeQuery(query)
.then((result: ReturnOption) => {
if (result.result) {
this.items = result.result;
}
})
.catch((error: any) => {
if (error.status === 401) {
// 跳转登录页
console.error('Unauthorized');
} else if (error.status === 403) {
// 访问被拒绝
console.error('Forbidden');
} else if (error.status === 500) {
// 服务端错误
console.error('Server error');
} else if (error.status === 0) {
// 网络错误
console.error('Network error - check connectivity');
}
});// Define your data model
interface Order {
OrderID: number;
CustomerID: string;
EmployeeID: number;
ShipCity: string;
}
// Use generics for type safety
const dataManager: DataManager<Order> = new DataManager({
json: orders,
adaptor: new JsonAdaptor()
});
// Typed results
const result = await dataManager.executeQuery(new Query());
const typedOrders: Order[] = result.result as Order[];// 定义数据模型
interface Order {
OrderID: number;
CustomerID: string;
EmployeeID: number;
ShipCity: string;
}
// 使用泛型保证类型安全
const dataManager: DataManager<Order> = new DataManager({
json: orders,
adaptor: new JsonAdaptor()
});
// 类型化返回结果
const result = await dataManager.executeQuery(new Query());
const typedOrders: Order[] = result.result as Order[];