java - 链接两个类之间的变量以输入计算值
问题描述
在我的 Spring + Angular 项目中,我必须插入一个摘要页面。在上一页 ( codeChoice.ts ) 上,用户可以选择一个code
. 此代码的值在摘要页面上用于执行计算。
- 代码选择.ts
code: CodeA[] = []
//...
goToSummary() {
this.router.navigate(['/summary'], {
state: {
code: this.code
}
})
}
在摘要页面中,我必须将我拥有的 CodeA 的价格添加到相关的 CodeB 中,然后对这些价格应用百分比和附件的字母参数。这就是为什么我想在后端创建一个关联类来管理两个类之间的联合:
- AssociationSummary.java
@Entity
@Table(name = "associationSummary")
public class AssociationSummary {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(cascade = CascadeType.ALL)
private CodeA codeA;
@ManyToOne(cascade = CascadeType.ALL)
private CodeB codeB;
@Column
private int percentage;
private string accessories;
// GET AND SET
}
相应地在前端:
- 关联摘要.ts
export class AssociationSummary {
id: number
codeA: CodeA
codeB: CodeB
percentage: number
accessories: string
}
其中 CodeA 和 CodeB 有这个变量:
export class CodeA {
id: number
codeA: string
priceA: number
}
export class CodeB {
id: number
codeB: string
priceB: number
}
因此,有了这些数据,我如何将用户在 prevoius 页面中选择的代码链接CodeA
到相关的类AssociationSummary
?在summary.ts
页面中,我现在已经这样做了:
export class Summary implements OnInit {
constructor(
private service: AssociationSummaryService
) { }
code: CodeA[]
association: AssociationSummary[]
ngOnInit() {
//I load the service data from the db
this.service.getAssociationSummary().subscribe(data => {
this.association = data;
})
// the code taken from the previous page
this.code = window.history.state.code
}
}
这是摘要页面的示例:
CODE A: ...
CODE B: ... (select of codeB if there are more codeB associated at CodeA)
------------
codeA price: ...
codeB price: ...
------------
Sum: ...
Sum * percentage: ...
解决方案
推荐阅读
- mysql - “?” 参数不过滤 SSRS (MySQL ODBC) 中的数据
- javascript - axios - 如何在单个 Promise 中链接 axios 调用
- google-apps-script - 无效参数:createTemplateFromFile 的用户界面错误
- tfs - 如何使用 TFS 流程模板修改项目管理员组?
- kubernetes - Kubernetes Secret 通过删除持续存在
- python - 在python中更改索引名称
- c# - asp.net core 2.2 登录成功后重定向到登录
- java - 从数据库加载详细信息时光标出错
- java - 如何实现自己的高效 OS 监听器?
- c# - 如何向绑定源添加另一行?'添加到 BindingSource 列表的对象必须都是同一类型。'