# 视图中同步非实体数据
# 1.场景介绍
以物料表和物料市场价格表为例介绍如何在物料表中视图中获取到物料市场价格表中的市场指导价。
# 2.效果展示
效果展示如下:

# 3.实现思路
1.通过queryEntitiesByProperty获取物料市场指导价格
2.添加视图查询后事件,从物料表中获取物料编码,查询物料编码对应的市场指导价格,再将查询到的市场指导价格更新回相应的物料表中,以便后续展示。
# 4.操作步骤
# 4.1 创建数据实体
执行数据库脚本,以MySQL为例,示例脚本如下:
drop table if exists material;
drop table if exists materialprice;
create table material (
id varchar(32) not null comment '主键',
name varchar(255) comment '物料名称',
code varchar(255) comment '物料代码',
price numeric(10,2) comment '物料价格',
primary key (id)
) comment='物料表';
create table materialprice (
id varchar(255) not null,
name varchar(255) comment '物料名称',
code varchar(255) comment '物料编码',
mPrice numeric(10,2) comment '市场指导价',
primary key (id)
) comment='物料市场价格表';
在构件包内,新建供应商数据实体

# 4.2 生成并调整表单和视图
右键点击实体,生成表单和视图

# 4.3 查询物料市场指导价格
在服务-通用业务中新建服务,通过queryEntitiesByProperty获取物料市场指导价格:



queryEntitiesByProperty具体用法请参考:数据库操作:如何使用queryEntitiesByProperty
# 4.2 添加视图加载后事件
在物料表默认视图中先添加物料市场市场价格的显示列:

在默认视图-高级设置-事件:添加视图加载后事件,从物料表中获取物料编码,查询物料编码对应的市场指导价格,再将查询到的市场指导价格更新回相应的物料表中:

let codes = []
//取出当前视图中物料编码放入数组中
for (let i = 0; i < this.data.length; i++) {
codes.push(this.data[i].code);
}
//调用逻辑流传入参数查询市场物料价格
let finalParams = ["code", codes.join(',')]
const databack = await this.Ajax.post("material.selearch.biz.ext", {
materialCodes: finalParams
}, false)
//获取市场价格信息后,对比物料编码给列表市场指导价参数赋值
if (databack) {
const marketPriceList = databack.out;
for (let i = 0; i < this.data.length; i++) {
let code = this.data[i].code
marketPriceList.forEach(marketPrice => {
if (code === marketPrice.code) {
this.$set(this.data[i], 'MGPrice', marketPrice.mPrice)
}
})
}
}