Click here to Skip to main content
15,891,704 members
Articles / Web Development / ASP.NET

JQueryUI smartAutocomplete

Rate me:
Please Sign up or sign in to vote.
4.75/5 (9 votes)
22 Feb 2012CPOL3 min read 82.3K   3.2K   27  
This widget extends the functionalities of jQueryUI Autocomplete widget by adding infinite scrolling when loading data from a remote source.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script src="Scripts/smartautocomplete.js" type="text/javascript"></script>
    <style>
        .ui-autocomplete
        {
            max-height: 100px;
            overflow-y: auto; /* prevent horizontal scrollbar */
            overflow-x: hidden; /* add padding to account for vertical scrollbar */
            padding-right: 20px;
        }
        /* IE 6 doesn't support max-height
	 * we use height instead, but this forces the menu to always be this tall
	 */
        * html .ui-autocomplete
        {
            height: 100px;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function () {
            //Input for testing purposes
            $("#inp").smartautocomplete({
                getDataFunc: getData,
                pageSize: 15,
                autoFocus: true
            });

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

        //Function the SA plugin called when data is needed. 
        var getData = function (input, pageIndex, pageSize, callback) {
            //In this example I use a WebMethod, but you can call anything from a local source to any web service.
            PageMethods.GetData(input, pageIndex, pageSize, function (response) {
                if (response) {
                    //Data is assumed to be received in a {label: , value: , ...} form, as needed by jqueryUI autocomplete. Of course, if you change the _renderItem function, you are free to modify this as you want
                    response = $.map(response, function (item) {
                        return {
                            label: item,
                            value: item
                        }
                    });
                    callback(response);
                }
                else callback();
            });
        };

    </script>
    <asp:ScriptManager ID="scriptManager" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <h2>
        <input id="inp" type="text" style="width: 350px;" />
        <input id="inp2" type="text" style="width: 350px;" />
    </h2>
</asp:Content>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Zurply
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions