【问题标题】:How do I use Twitters typeahead.js with Microsoft OData API's?如何将 Twitter typeahead.js 与 Microsoft OData API 一起使用?
【发布时间】:2024-01-16 10:17:01
【问题描述】:

我在编写网络应用程序以从我们的 CRM 应用程序获取 JSON 数据时遇到问题。

我想做的是使用Twitter's typeahead.js 插件从我们的 CRM 应用程序中远程获取客户信息。 Microsoft 确实提供了一种使用 JSON 数据进行通信的方法。他们称之为 OData。但是,这看起来不像典型的 JSON 响应。这就是为什么我无法使用它设置 typeahead 插件。

当我向 API URL 发送 GET 请求时,我收到以下响应:

{
    "d":{
        "results":[
            {
                "__metadata":{
                    "uri":"http://*****/*****/XRMServices/2011/OrganizationData.svc/AccountSet(guid'de227fde-fb40-dd11-b5d3-001cc46325e5')",
                    "type":"Microsoft.Crm.Sdk.Data.Services.Account"
                },
                "Name":"Some company as",
                "AccountId":"de227fde-fb40-dd11-b5d3-001cc46325e5"
            },
            {
                "__metadata":{
                    "uri":"http://*****/*****/XRMServices/2011/OrganizationData.svc/AccountSet(guid'5dc70a19-e91e-e311-9ad7-005056ac083a')",
                    "type":"Microsoft.Crm.Sdk.Data.Services.Account"
                },
                "Name":"Compnay AS",
                "AccountId":"5dc70a19-e91e-e311-9ad7-005056ac083a"
            }
        ]
    }
}

那么问题来了: 如何设置 Twitter 的 typeahead 插件以使用此数据结构?

在显示建议时,我想要来自 JSON 响应的 Name 值。我想在选择建议时获取关联的AccountId 值。

到目前为止,这是我在代码中得到的:

HTML:

<!DOCTYPE html>
<html lang="no">
    <head>
        <title>Company :: Time Registrering</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" type="text/css" href="css/global.css" />
    </head>
    <body>

        <form action="" method="GET" autocomplete="off">
            <input type="text" name="account" id="account" placeholder="Kunde...">
        </form>

        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/typeahead.js"></script>
        <script type="text/javascript" src="js/global.js"></script>
    </body>
</html>

JavaScript: (js/global.js)

$(document).ready(function () {
    var accounts = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Name'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: "http://*****/*****/xrmservices/2011/OrganizationData.svc/AccountSet?$select=Name,AccountId&$filter=substringof('%QUERY',Name) and StateCode/Value eq 0"
    });

    accounts.initialize();

    $("#account").typeahead({
        hint: true,
        highlight: true,
        minLength: 2
    }, {
        name: 'account',
        displayKey: 'd.results[0].Name',
        source: accounts.ttAdapter()
    });

});

但是:我的代码不起作用。我得到的只是输入字段下的“未定义”文本。我怀疑我的datumTokenizerdisplayKey 引用不正确。我不完全理解datumTokinizer。所以如果有人能启发我,我会很感激:)

【问题讨论】:

    标签: javascript json typeahead.js bloodhound


    【解决方案1】:

    你应该使用过滤器并使用jQuery.map

    html

     <input type="text" name="account" id="account" placeholder="Kunde..." />
    

    js

    $(document).ready(function () {
    
    
    
        var accounts = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Name'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            remote: {
            url : "https://gist.githubusercontent.com/anonymous/80a75d841a055ea0e480/raw/4eb8d4f1833d8a15cae1830097c090f5d581bd12/gistfile1.txt",
            filter: function(jsonValue) {
    
                    return $.map(jsonValue.d.results, function (result) {
                    return {
                        value: result.Name
                    };
                });
            }
        }     
    
    
        });
    
        accounts.initialize();
    
        $("#account").typeahead({
            hint: false,
            highlight: true,
            minLength: 2
        }, {
            source: accounts.ttAdapter()
        });
    
    });
    

    Fiddle here

    【讨论】:

    • 这很有趣。就像我想通了并发布了我的答案一样:您也这样做了。不管怎样,谢谢你! :)
    • 很高兴你自己想出来了:D
    【解决方案2】:

    我找到了解决问题的方法。我注意到我可以在 Bloodhound 对象中使用过滤器函数并使用 $.map 生成一个数组,以便 Bloodhound 引擎可以按照预期进行查找。

    这就是我的 JavaScript 代码现在的样子(问题中的 HTML 没有改变):

    $(document).ready(function () {
        var accounts = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Name'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            remote: {
                url: "http://****/****/xrmservices/2011/OrganizationData.svc/AccountSet?$select=Name,AccountId&$filter=substringof('%QUERY',Name) and StateCode/Value eq 0",
                filter: function (accounts) {
                    return $.map(accounts.d.results, function (account) {
                        return {
                            Name: account.Name,
                            AccountId: account.AccountId
                        };
                    });
                }
            }
        });
    
        accounts.initialize();
    
        $("#account").typeahead({
            hint: true,
            highlight: true,
            minLength: 2
        }, {
            name: 'account',
            displayKey: 'Name',
            source: accounts.ttAdapter()
        });
    
    
        $("#account").on('typeahead:selected', function(dom, selectedItem) {
            console.log(selectedItem.AccountId);
        });
    });
    

    希望这会帮助其他人和我做同样事情的人:)

    【讨论】: