在应用制作工具中,如何制作动态 table 单元格文本?

In App Maker, how do you make dynamic table cell text?

在 App Maker 中,我正在显示一个 table 并希望使用来自另一个 table 的数据查找将 table 单元格数据替换为不同的文本。假设两个 tables,DepartmentsEmployees.

Employees 的 table 列表中,我想用 DeptDescription 替换 DeptID。 (页面数据源是Employees,我不想建立数据模型之间的关系。)

我想我想在 onDataLoad 事件中为 DeptID 的 table 单元格标签编写一些脚本。到目前为止我有这么多:

 app.datasources.Departments.query.filters.DeptID._equals = widget.datasource.item.DeptID;
 app.datasources.Departments.newQuery().run();
 widget.text = app.datasources.Departments.item.DeptDescription;

我知道这不正确,但我接近了吗?

1 种方法)如果您需要绕过关系功能,请创建一个聚合 table 加入您的 table。这样你就可以使用 sql 加入数据源定义中的两个 table

2) 如果你不想制作新的table。将文本从值绑定更改为 "more options"

=getDescription(@datasource.item.DeptId)

然后是您在客户端脚本

中编写的代码
function getDescription(id){
  google.script.run
  .withSuccessHandler(function successHandler(result){    return result;})
  .withFailureHandler( function failureHandler(e){ console.log(" Failed" +e);})
  .queryValue(id); 

}

服务器端脚本:

function queryValue(id){ 
  var query = app.models.Departments.newQuery();
  query.filters.DeptID._equals = id;
  var results = query.run();
  return results[0]["DeptDescription"];
}

最后一行可能是结果[0].DeptDescription

这个答案未经测试,但我想提出一个可能的解决方案,它不需要大量的数据库调用,尤其是那些重复调用服务器脚本的解决方案,当您执行 line 时可能会消耗大量处理时间项目调用。

  1. 在部门模型下设置一个单独的数据源。将默认的 'Query Builder' 更改为 'Query Script' 并添加类型为 'list(number)' 或 'list(string)' 的参数,这应该与您的主键字段类型匹配。取消选中 'auto load' 选项。
  2. 在您的 'Query Script' 部分输入以下代码:

    query.filters.Id._in = query.parameters.YourParameter;

    return query.run();

  3. 转到应该生成您的 table 的员工数据源并找到您的 'On Load' 客户端脚本部分。在此部分中输入以下代码:

    var departmentsDs = app.datasources.YourDepartmentsDs;

    departmentsDs.properties.YourParameter = datasource.items.map(函数(deptIds) {return deptIds.DeptID;});

    departmentDs.load();

  4. 现在转到包含您的 table 的页面。如果您尚未创建标签小部件,请立即创建。在此文本绑定的标签小部件中输入以下内容:

    @datasources.YourDepartmentsDs.loaded && (@datasources.YourDepartmentsDs.items).map(函数(Id){return Id.Id}).indexOf(@widget.datasource.item.DeptID) !== -1 ? @datasources.YourDepartmentDs.items[(@datasources.YourDepartmentsDs.items).map(函数(Id){return Id.Id}).indexOf(@widget.datasource.item.DeptID)].DeptDescription : 'Unable to retrieve Dept Description'

如前所述,这是未经测试的,我在没有 App Maker 的情况下凭记忆编写了代码,因此可能需要进行一些额外的调整。使用 J.G 提出的第一个选项。不过,这也是一个非常可行的解决方案。我很抱歉,但代码格式化程序似乎不适合我。