我们已经实现了以下帮助函数:-
function defaultLookup (entity, destinationPropertyName, sourceCollectionName, options) {
/// <summary>
/// Defaults an entity's lookup property
/// </summary>
/// <param name="entity" type="Object">The entity featuring the lookup property to default</param>
/// <param name="destinationPropertyName" type="String">The lookup property against the entity to default</param>
/// <param name="sourceCollectionName" type="String">The collection from which to source the lookup value</param>
/// <param name="options" type="PlainObject" optional="true">
/// A set of key/value pairs used to select additional configuration options. All options are optional.
/// <br/>- String filter: If supplied, defines the match condition for the required default, otherwise the lookup defaults to the first entry in the source collection
/// </param>
options = options || {}; // Force options to be an object
var source = myapp.activeDataWorkspace.ApplicationData[sourceCollectionName]; // DataServiceQuery
var query = {}; //DataServiceQuery
if (options.filter) {
query = source.filter(options.filter);
} else {
query = source.top(1);
}
query.execute().then(function (result) {
entity[destinationPropertyName] = result.results[0];
});
};
在您的情况下,您需要更改 ApplicationData 以读取 ProjectData。
这可以在屏幕的 created 事件中调用如下:-
myapp.AddEditCustomer.created = function (screen) {
var defaultValue = "Chris Cook";
var filter = "(Name eq " + msls._toODataString(defaultValue, ":String") + ")";
defaultLookup(screen.Customer, "Contact", "Contacts", { filter: filter });
};
在您的情况下,screen.Customer 应更改为 screen.OrderRequest,“Contact”应更改为“CustomerName”,“Contacts”应更改为“ShippingContacts”。此外,根据您的查找表有一个名为 ContactName 的字段,过滤器字符串需要引用 ContactName 而不仅仅是名称。
或者,可以从您的实体创建的事件(在您的 UserCode 脚本部分中)调用此帮助程序,如下所示:-
myapp.Customer.created = function (entity) {
var defaultValue = "Chris Cook";
var filter = "(Name eq " + msls._toODataString(defaultValue, ":String") + ")";
defaultLookup(entity, "Contact", "Contacts", { filter: filter });
};
在我的代码示例中,主表称为“客户”,查找表称为“联系人”。主表中的“联系人”字段引用联系人表中的条目。 Contacts 表有一个名为“Name”的字段和一个名称设置为“Chris Cook”值的记录(defaultValue 和 filter 变量指的是这种情况)。
下图显示了正在调试的 screen.Customer 属性:-