jquery smartAutocomplete with infinite scroll

不问归期 提交于 2019-12-12 02:58:42

问题


how we can have autocomplete combo with infinit scroll? i found an autocomplete jquery ui with infinit scroll, but this autocomplete gets data by pagemethods. but i want to use it in mvc application and want to use an action of a controller to retrieving data. to use this autocomplete by pagemethods should do this:

$(document).ready(function () {
        //Input for testing purposes
        $("#inp").smartautocomplete({
            getDataFunc: getData,
            pageSize: 15,
            autoFocus: true
        });
    });

    //Function the SA plugin called when data is needed. 
    var getData = function (input, pageIndex, pageSize, callback) {

        PageMethods.GetData(input, pageIndex, pageSize, function (response) {
            if (response) {
                response = $.map(response, function (item) {
                    return {
                        label: item,
                        value: item
                    }
                });
                callback(response);
            }
            else callback();
        });
    };

but i change the way of getting data by using $.ajax:

var getData = function (input, pageIndex, pageSize, callback) {
        $.getJSON(
            { url: '@Url.Action("GetData", "Home")' },
            { input: input, pageIndex: pageIndex, pageSize: pageSize },
            function (response) {
            if (response) {
                response = $.map(response, function (item) {
                    return {
                        label: item,
                        value: item
                    };
                });
                callback(response);
            }
            else callback();
        });

    };

but it does not work, and the action does not called. this autocomplete is accessible here: http://www.codeproject.com/Articles/325719/JQueryUI-smartAutocomplete?fid=1683905

i want to know if there is any other solution to have autocomplete with infinit scroll


回答1:


Replace PageMethod call with AJAX call

        $.ajax({
            url: '@Url.Action("GetData", "Default")',                
            type: 'GET',
            dataType: 'json',
            data: {
                input: input,
                pageIndex: pageIndex,
                pageSize: pageSize

            },

            success: function (response) {
                //alert(response);
                if (response) {
                    response = $.map(response, function (item) {
                        return { label: item, value: item };
                    });
                    callback(response);
                } else {
                    callback();
                }
            },
            error: function (e) {
                alert('error' + e);
            },
            contentType: 'application/json; charset=utf-8'


        });

Make sure your controller action is returning JSONResult

return new JsonResult {JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data =    data };//new {result = data}

Hope this helps.



来源:https://stackoverflow.com/questions/16812758/jquery-smartautocomplete-with-infinite-scroll

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