【发布时间】:2016-05-12 12:10:37
【问题描述】:
这是场景:
当用户输入邮政编码时,必须显示自动填充,当用户选择邮政编码时,应自动填写城市和县等其他字段。
有关邮政编码、城市和邮政编码的信息在 JSON 对象中。
用户点击自动完成列表时是否有事件发生?
有谁知道如何做到这一点?
感谢您的帮助
【问题讨论】:
标签: javascript dynamics-crm microsoft-dynamics
这是场景:
当用户输入邮政编码时,必须显示自动填充,当用户选择邮政编码时,应自动填写城市和县等其他字段。
有关邮政编码、城市和邮政编码的信息在 JSON 对象中。
用户点击自动完成列表时是否有事件发生?
有谁知道如何做到这一点?
感谢您的帮助
【问题讨论】:
标签: javascript dynamics-crm microsoft-dynamics
我刚刚使用 Polshgiant 共享的代码示例测试了您的场景,并按照 Jorge Cunha 的建议在属性上添加了 onchange 事件。
不幸的是,我的测试表明,只有击键会触发 onchange 事件: 当我选择自动完成值时,不会触发 onchange。
因此,我认为此问题没有受支持的解决方案。
但我需要相同的功能,所以我希望其他人会证明我错了。
5 月 17 日编辑: 今天的一个新测试显示 onchange 事件被调用了,但是
var newValue = Xrm.Page.getControl(control).getValue();
获取键入的值,而不是自动完成的值。
然而
var newValue = Xrm.Page.getControl(control).getAttribute().getValue();
给我自动完成选择的值。
因此我现在可以使用此功能:-)
【讨论】:
CRM 2016 最近添加了一个按键事件,您可以将其用于字符串字段,让您可以完全按照自己的意愿去做(但不适用于移动设备)。这是SDK's example:
/* Sample JavaScript code to demonstrate the auto-completion feature.
This sample configures the auto-complete feature for the "Account Name"
field in the account form. */
function suggestAccounts() {
// List of sample account names to suggest
accounts = [
{ name: 'A. Datum Corporation', code: 'A01' },
{ name: 'Adventure Works Cycles', code: 'A02' },
{ name: 'Alpine Ski House', code: 'A03' },
{ name: 'Bellows College', code: 'A04' },
{ name: 'Best For You Organics Company', code: 'A05' },
{ name: 'Blue Yonder Airlines', code: 'A06' },
{ name: 'City Power & Light', code: 'A07' },
{ name: 'Coho Vineyard', code: 'A08' },
{ name: 'Coho Winery', code: 'A09' },
{ name: 'Coho Vineyard & Winery', code: 'A10' },
{ name: 'Contoso, Ltd.', code: 'A11' },
{ name: 'Proseware, Inc.', code: 'A30' },
{ name: 'Relecloud', code: 'A31' },
{ name: 'School of Fine Art', code: 'A32' },
{ name: 'Southridge Video', code: 'A33' },
{ name: 'Tailspin Toys', code: 'A34' },
{ name: 'Trey Research', code: 'A35' },
{ name: 'The Phone Company', code: 'A36' },
{ name: 'VanArsdel, Ltd.', code: 'A37' },
{ name: 'Wide World Importers', code: 'A38' },
{ name: 'Wingtip Toys', code: 'A39' },
{ name: 'Woodgrove Bank', code: 'A40' }
];
var keyPressFcn = function (ext) {
try {
var userInput = Xrm.Page.getControl("name").getValue();
resultSet = {
results: new Array(),
commands: {
id: "sp_commands",
label: "Learn More",
action: function () {
// Specify what you want to do when the user
// clicks the "Learn More" link at the bottom
// of the auto-completion list.
// For this sample, we are just opening a page
// that provides information on working with
// accounts in CRM.
window.open("http://www.microsoft.com/en-us/dynamics/crm-customer-center/create-or-edit-an-account.aspx");
}
}
};
var userInputLowerCase = userInput.toLowerCase();
for (i = 0; i < accounts.length; i++) {
if (userInputLowerCase === accounts[i].name.substring(0, userInputLowerCase.length).toLowerCase()) {
resultSet.results.push({
id: i,
fields: [accounts[i].name]
});
}
if (resultSet.results.length >= 10) break;
}
if (resultSet.results.length > 0) {
ext.getEventSource().showAutoComplete(resultSet);
} else {
ext.getEventSource().hideAutoComplete();
}
} catch (e) {
// Handle any exceptions. In the sample code,
// we are just displaying the exception, if any.
console.log(e);
}
};
Xrm.Page.getControl("name").addOnKeyPress(keyPressFcn);
}
然后你可以使用addOnChange event来处理用户的选择。
【讨论】:
在盒子的 CRM 中,您没有做任何事情,但您有 3 种可能性去做。
1 为您的邮政编码创建一个实体并将您的信息放入其中。在您的表单上创建查找后,我记得以受支持的方式进行的唯一方式。
2 在字段的事件更改时,调用您的方法 js 自动填充,但是这个字段需要完全旋转,以便您的脚本获得匹配。
3 这不受支持,但您可以操作 DOM 并使用 js 脚本自动填充您的字段,并在字段上发生事件。
干杯
【讨论】: