I have an Employees model with first_name
and last_name
string fields (I'll use John Doe for the example). I'd like to have each line of a drop-down widget read "Doe, John" using the Employees datasource. I can bind the names array of a drop-down widget to one field or the other pretty easily by editing the "names" field of the widget:
=@datasources.Employees.item.last_name
yields Doe, and
=@datasources.Employees.item.first_name
yields John in the dropdown's first list item, respectively.
Concatenating a static string to the field also works:
=@datasources.Employees.item.last_name + "-test"
yields Doe-test in the drop-down
However, when I try to modify this binding to combine the two fields, it doesn't work:
=@datasources.Employees.item.last_name + ", " + @datasource.item.Employees.first_name
yields just Doe
Even dumping both fields into an array and joining them:
=[@datasources.Employees.item.last_name, @datasources.Employees.item.first_name].join(", ")
yields just Doe
The only way I've found to get the values of two different fields from a record together on the same line in a drop-down widget is to use client scripting to populate the names array of the widget during the onDataLoad event:
app.datasources.Employees.load(function() {
app.datasources.Employees.items.forEach(
item => widget.names.push([item.last_name, item.first_name].join(", "))
);
});
Is this the best/only way to do this, or am I just missing something on the names binding dialog? Thanks!
In your dropdown names binding do the following:
(@datasources.Employees.items).map(function(i) { return i.last_name + ', ' + i.first_name; })
That will do what you want.