如何将我的数据源的返回映射到Kendo AutoComplete UI?

huangapple go评论65阅读模式
英文:

How do I map the return of my dataSource to a Kendo AutoComplete UI

问题

我正在尝试在用户从自动完成组件中进行选择时获取"unitKey"。

英文:

I have a Kendo jquery AutoComplete UI component with a remote REST web API dataSource. I want to map the responce of the API to the autoComplete. I have it displaying the choices but when I examine the select function I just see what I've selected. What I want is the complete json object on the select event. Or at least what I really need is the unitKey property.

my code:

//create AutoComplete UI component
    $("#autoAddress").kendoAutoComplete({
        minLength: 3,
        dataTextField: "address",                
        filter: "startswith",
        placeholder: "Address lookup ...",            
        select : function(e) {
            var item = e.item;
            console.log(item.text());
        },
        dataSource: new kendo.data.DataSource ({   
            serverFiltering: true,
            transport: {
                read: {
                    url: "https://testapi.mydomain.com/homeinfo/api/address/",
                    // dataType: "jsonp",
                    data: {
                        q : function() {
                            return $("#autoAddress").data("kendoAutoComplete").value();
                        },                                
                        maxRows: 30
                    }
                }
            },
            schema : {
                data: function(response){
                    return response;
                }
            }                              
        })
    });

sample date from api call:

[
    {
        "unitKey": "S37.75      ",
        "address": "1234 ADDISON AVE"
    },
    {
        "unitKey": "S22.215     ",
        "address": "1234 AUGUSTINE DR"
    },
    {
        "unitKey": "L100.9      ",
        "address": "1234 AVENIDA DE LAS CASAS"
    }
]

I'm trying to get the "unitKey" when the user makes a selection from the autocomplete component.

答案1

得分: 1

你应该查看 e.dataItem 而不是 e.item:

console.log(e.dataItem.unitKey);

在这种情况下,通常我会将 'e' 本身记录到控制台,或者使用调试器中的断点来检查它的内容,以查看它包含了什么,因为文档通常并不总是如此详尽。

英文:

You want to look at e.dataItem rather than e.item:

console.log(e.dataItem.unitKey);

In cases like this I usually log 'e' itself to the console or breakpoint with the debugger to inspect it to see what it contains since the documentation is not always as comprehensive as it could be.

huangapple
  • 本文由 发表于 2023年2月16日 03:39:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定