Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lookupFilter is not working when use serviceUrl #206

Closed
germanmartin opened this issue Jul 23, 2014 · 2 comments
Closed

lookupFilter is not working when use serviceUrl #206

germanmartin opened this issue Jul 23, 2014 · 2 comments

Comments

@germanmartin
Copy link

Hi!

   Can someone help me? I need to make a filtering of suggestion but i'm using serviceUrl to get the data with json. In the example of this page the filtering works great just with local data.

I have added this:
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
var re = new RegExp('\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi');
return re.test(suggestion.value);
},

But still not working (is not filtering). This is the entire code:

    // Initialize ajax autocomplete:
    $('#autocomplete-ajax').autocomplete({
        serviceUrl: '/Handler.ashx',
        lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
            var re = new RegExp('\\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi');
            return re.test(suggestion.value);
        },
        onSelect: function (suggestion) {
            $('#selction-ajax').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
        },
        onHint: function (hint) {
            $('#autocomplete-ajax-x').val(hint);
        },
        onInvalidateSelection: function () {
            $('#selction-ajax').html('You selected: none');
        }
    });
@tkirda
Copy link
Member

tkirda commented Jul 23, 2014

When using serviceUrl option, it is server responsibility to filter results.

@tkirda tkirda closed this as completed Jul 23, 2014
@germanmartin
Copy link
Author

You are right and I understand.
Thank you very much.
Well I will use "context" variable to get the suggestion word.

The solution for me is something like this:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    Dim prefix As String = context.Request.QueryString("query")
    Dim DevelopersList As String = ""
    Dim MyConn As SQLiteConnection = New SQLiteConnection("Data Source=database.db;Version=3;")
    Dim MyComm As SQLiteCommand = New SQLiteCommand("SELECT * FROM people_list WHERE Name LIKE '%" & prefix & "%'", MyConn)
    MyConn.Open()
    Dim dr As SQLiteDataReader = MyComm.ExecuteReader

    While dr.Read
        If dr.Item("ID") IsNot DBNull.Value Then
            DevelopersList += "{ ""value"": """ & dr.Item("Name") & """, ""data"": """ & dr.Item("ID") & """ },"
        End If
    End While
    MyConn.Close()

    DevelopersList = DevelopersList.TrimEnd(",", "")

    'Cargamos las Javascripts tradicionales
    Dim DevelopersListJS As String = String.Empty
    DevelopersListJS += "{ ""query"": ""Unit"", ""suggestions"": ["
    DevelopersListJS += DevelopersList
    DevelopersListJS += "] }"

    context.Response.ContentType = "text/plain"
    context.Response.Write(DevelopersListJS)
End Sub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants