共用方式為


範例:Dynamics 365 控制項中的自動完成

 

發行︰ 2017年1月

適用於: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

使用範例 JavaScript 程式碼設定 Dynamics 365 中的自動完成體驗,藉由使用 Microsoft Dynamics 365 (線上和內部部署) 中推出的下列用戶端 API:getValueKeypress 方法自動完成方法

示範

當您將本主題中的範例 JavaScript 程式碼新增為客戶表單中的表單程式庫,以及在客戶表單中的 OnLoad 事件 上新增 suggestAccounts 函數時,自動完成功能會在客戶表單的 [客戶名稱] 欄位中設定。 在 [客戶名稱] 欄位中輸入名稱時,將顯示自動完成下拉式清單,而下拉式清單的值會篩選出符合您在此欄位中輸入的字元。 按一下下拉式清單的值,將它新增至 [客戶名稱] 欄位。

使用表單指令碼的自動完成體驗

範例

/* 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: 'Contoso Pharmaceuticals', code: 'A12' },
  { name: 'Contoso Suites', code: 'A13' },
  { name: 'Consolidated Messenger', code: 'A14' },
  { name: '​Fabrikam, Inc.', code: 'A15' },
  { name: 'Fabrikam Residences', code: 'A16' },
  { name: '​First Up Consultants', code: 'A17' },
  { name: 'Fourth Coffee', code: 'A18' },
  { name: 'Graphic Design Institute', code: 'A19' },
  { name: 'Humongous Insurance', code: 'A20' },
  { name: 'Lamna Healthcare Company', code: 'A21' },
  { name: 'Litware, Inc.', code: 'A22' },
  { name: 'Liberty Delightful Sinful Bakery & Cafe', code: 'A23' },
  { name: 'Lucerne Publishing', code: 'A24' },
  { name: 'Margie Travel', code: 'A25' },
  { name: '​Munson Pickles and Preserves Farm', code: 'A26' },
  { name: 'Nod Publishers', code: 'A27' },
  { name: 'Northwind Electric Cars', code: 'A28' },
  { name: 'Northwind Traders', code: 'A29' },
  { 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("https://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);    
}

另請參閱

Xrm.Page.ui 控制項 (用戶端參考)

Microsoft Dynamics 365

© 2017 Microsoft. 著作權所有,並保留一切權利。 著作權