- 打字稿
- ABP .NET Core
我正在使用网格插入行(我正在使用的网格是Devextreme框架的组成部分(。无论如何,类似于其他网格,它会在插入记录时提高onRowInserting
事件,从而提供插入的行作为参数。在此事件中,我需要将该"匿名"对象(插入的数据(转换为我的客户端DTO。
onRowInserting(e) {
let mynewrow: ItemDto = e.data; // e.data contains the inserted row
}
要更好地了解我需要实现的目标,请阅读这篇文章:
将行添加到Devextreme网格(Angular( - 型号/架构
编辑
ItemDto
:
export class ItemDto implements IItemDto {
description: string;
note: string;
quantita: number;
postId: number;
id: number;
constructor(data?: IItemDto) {
if (data) {
for (var property in data) {
if (data.hasOwnProperty(property))
(<any>this)[property] = (<any>data)[property];
}
}
}
init(data?: any) {
if (data) {
this.description = data["description"];
this.note = data["note"];
this.quantita = data["quantita"];
this.postId = data["postId"];
this.id = data["id"];
}
}
static fromJS(data: any): ItemDto {
let result = new ItemDto();
result.init(data);
return result;
}
toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
data["description"] = this.description;
data["note"] = this.note;
data["quantita"] = this.quantita;
data["postId"] = this.postId;
data["id"] = this.id;
return data;
}
clone() {
const json = this.toJSON();
let result = new ItemDto();
result.init(json);
return result;
}
}
export interface IItemDto {
description: string;
note: string;
quantita: number;
postId: number;
id: number;
}
及以下,e.data
的含量(此刻,我在网格中仅添加了一些列,因此并非所有字段都存在(。
Object {
__KEY__: "7c2ab8-1ff6-6b53-b9d7-ba25c27"
description: "mydescription"
id: 32
note: "mynote"
postId: 4
quantita: 2
> __proto__: Object { constructor; , _defineG....
}
此图像更好地表示对象:https://i.stack.imgur.com/xkeb1.jpg
我不确定我在let mynewrow: ItemDto
行中所做的事情。我不知道它是否正确,或者以后使用该变量是否足以将其传递给保存新行的服务。
您可以使用装饰器和串行化器。请参阅此处的LIB:https://www.npmjs.com/package/serialize-ts
如何使用 object.assign()
将属性的值从JSON响应对象推入您想要的类?
class thingy {
a = null;
print = function() {
console.log(this.a);
};
}
const sourceJson = {
a: "hello world"
};
const target = Object.assign(new thingy(), sourceJson);
target.print()