AppMaker - 将数据源中每个下拉列表名称数组元素的两个字段组合起来的更好方法?

AppMaker - Better way to combine two fields from datasource per element of dropdown's names array?

我有一个带有 first_namelast_name 字符串字段的 Employees 模型(我将使用 John Doe 作为示例)。我想使用 Employees 数据源读取下拉小部件的每一行 "Doe, John"。通过编辑小部件的 "names" 字段,我可以很容易地将下拉小部件的名称数组绑定到一个字段或另一个字段:

=@datasources.Employees.item.last_name 产生 Doe,并且 =@datasources.Employees.item.first_name 分别在下拉列表的第一个列表项中生成 John。

将静态字符串连接到字段也有效:

=@datasources.Employees.item.last_name + "-test" 在下拉列表中产生 Doe-test

但是,当我尝试修改此绑定以合并两个字段时,它不起作用:

=@datasources.Employees.item.last_name + ", " + @datasource.item.Employees.first_name 只产生 Doe

甚至将两个字段转储到一个数组中并加入它们:

=[@datasources.Employees.item.last_name, @datasources.Employees.item.first_name].join(", ") 仅产生 Doe

我发现在下拉小部件的同一行上从一条记录中获取两个不同字段的值的唯一方法是使用客户端脚本在 onDataLoad 期间填充小部件的名称数组事件:

app.datasources.Employees.load(function() {
  app.datasources.Employees.items.forEach(
    item => widget.names.push([item.last_name, item.first_name].join(", "))
  );
});

这是 best/only 的方法吗,还是我只是在名称绑定对话框中遗漏了什么?谢谢!

在您的下拉名称绑定中执行以下操作:

(@datasources.Employees.items).map(function(i) { return i.last_name + ', ' + i.first_name; })

这将如你所愿。

发生这种情况的原因是因为 names 的期望值是:

An optional array of option labels that correspond 1-1 with the option values. This is useful if the value set in options and value are not strings, and instead set some underlying data in the backend. For example, if value and options are bound to a record, this could be used to display a certain field of the record to the user.

那么首先要考虑的是,下拉选项的绑定是什么?我假设它会是

@datasource.Employees.items.

如果是这样,我们需要考虑的下一件事是您需要提供一个数组,该数组与选项所具有的项目数完全相同。不幸的是,当您按照自己的方式执行绑定时,情况并非如此,

@datasources.Employees.item.last_name + ", " + @datasource.item.Employees.first_name

您只提供了一个项目而不是一个数组,这将产生意想不到的结果,具体取决于您的数据源的配置方式。但是,当您通过客户端脚本执行此操作时,您将项目推入该数组并匹配选项数据源的项目长度,因此它有效。

您使用的方法很好,只是每次发生更改时您都必须重新加载数据源。更好的方法是遵循 Markus Malessa 提供的答案。这样,您就提供了一个 与选项值 对应 1-1 的项目数组,并且无需在每次发生更改时都重新加载数据源。