Kendo AutoComplete Not Displaying

元气小坏坏 提交于 2019-12-14 03:51:34

问题


I have a custom editor that I use with autocomplete. The web service is getting called and returning the data. However, nothing is diaplying in the editor. I put a breakpoint in schema.parse() but it's never hit. What am I missing?

function myAutoCompleteEditor(container, options) {
    $('<input data-text-field="Name" data-value-field="Name" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoAutoComplete({
            autoBind: false,
            suggest: true,
            delay: 500,
            dataSource: new kendo.data.DataSource({
                serverFiltering: true,
                transport: {
                    read: function (opt) {
                        $.getJSON("/myWebService/GetData");
                    },
                },
                schema: {
                    errors: function (e) {
                        return e;
                    },
                    parse: function (data) {
                        return data.Name;
                    }
                }
            })
        });
}

UPDATE:

The data, when shown via JSON.stringfy(data) is like this:

[{"Address":"123 1st St.","ID":"1","Name":"David"},{"Address":"234 2nd St.","ID":"2","Name":"Smith"}]

UPDATE 2:

The code looks like this now:

function myAutoCompleteEditor(container, options) {
    $('<input data-text-field="Name" data-value-field="Name" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoAutoComplete({
            dataValueField: "Name",
            autoBind: false,
            suggest: true,
            delay: 500,
            dataSource: new kendo.data.DataSource({
                serverFiltering: true,
                transport: {
                    read: {
                        url: function (opt) {
                            return "/myWebServices/GetData/" + opt.filter.filters[0].value;
                        },
                        dataType: "json"
                    }                        
                },
                schema: {
                    errors: function (e) {
                        return e;
                    }
                },
                data: function (response) {
                    return $.parseJSON(response);
                }
            })
        });
}

UPDATE 3:

Finally got it working by removing the schema and data section. Accepting OnaBai's answer since he definitely pointed me to the right direction. The final code looks like this:

function myAutoCompleteEditor(container, options) {
    $('<input data-text-field="Name" data-value-field="Name" data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoAutoComplete({
            dataValueField: "Name",
            autoBind: false,
            suggest: true,
            delay: 500,
            dataSource: new kendo.data.DataSource({
                serverFiltering: true,
                transport: {
                    read: {
                        url: function (opt) {
                            return "/myWebServices/GetData/" + opt.filter.filters[0].value;
                        },
                        dataType: "json"
                    }                        
                }
            })
        });
}

回答1:


The problem is the implementation of read function. This function should invoke opt.success with data that you want to return. What you are doing is retrieving data from a URL but you are not returning such data.

But in your case it seem that you don't do anything special (no need for defining a function). So you can define it as an Object and just define transport.read.url

You can use:

dataSource: new kendo.data.DataSource({
    serverFiltering: true,
    transport: {
        read: {
            url : "/myWebService/GetData"
        }
    },
    schema: {
        parse: function (data) {
            return data.Name;
        }
    }
})

EDIT: For using the data as the server returns it and do not have to parse, you can use:

function myAutoCompleteEditor(container, options) {
    $('<input data-text-field="Name" data-value-field="Name" data-bind="value:' + options.field + '"/>').appendTo(container).kendoAutoComplete({
        autoBind      : false,
        suggest       : true,
        delay         : 500,
        dataValueField: "Name",
        dataSource    : new kendo.data.DataSource({
            transport: {
                read: {
                    url : "/myWebService/GetData"
                }
            }
        })
    });
}

The trick is defining dataValueField that defines which value of the returned array is the value of the autocomplete.



来源:https://stackoverflow.com/questions/16500659/kendo-autocomplete-not-displaying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!