0

I need to supply my autocomplete from a webservice who passes an array into the autocomplete source.

Here is my webservice:

    public Filter[] getAutoComplete(string column)
    {
        List<Filter> list = new List<Filter>();

        DbAccess dbacc = new DbAccess();

          DataTable dt = dbacc.getColumnHeader(column);

          Filter _Filter = new Filter();


          if (column == "member_id")
          {
              foreach (DataRow row in dt.Rows)
              {
                  _Filter.memid = row["member_id"].ToString();
              }
          }
          else if (column == "lname")
          {
              foreach (DataRow row in dt.Rows)
              {
                  _Filter.memid = row["lname"].ToString();
              }
          }
          else if (column == "mname")
          {
              foreach (DataRow row in dt.Rows)
              {
                  _Filter.memid = row["mname"].ToString();
              }
          }
          else if (column == "fname")
          {
              foreach (DataRow row in dt.Rows)
              {
                  _Filter.memid = row["fname"].ToString();
              }
          }

        list.Add(_Filter);

        return list.ToArray();
    }

And here is my jquery ajax:

$.ajax({

    url: "webservices/wbFilters.asmx/getAutoComplete",
    data: json,
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (mydata) {

        $("#tags").autocomplete({
            source: mydata
        });

    }
});

I can verify that my list array has a value. I just couldn't attach it to my autocomplete. I read that I can add a array as a source but I really can't get it to work.

Can someone please explain what needs to be done to get this working?

3 Answers 3

1

For custom data sources that hold more than a list of strings, you need to specify the property or properties you want to display. Therefore you need to override the _renderItem, like so:

$("#tags").autocomplete({
    source: myData
}).data("autocomplete")._renderItem = function(ul, item) {
    return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.memid + "</a>" )
            .appendTo( ul );
};

For detailed info, see: http://jqueryui.com/demos/autocomplete/#custom-data

Sign up to request clarification or add additional context in comments.

9 Comments

Now there's something. But it is undefined.
Maybe you need to combine my answer with Dappergoat's and use myData.d.
I actually did that. Still now working. I'm getting undefined results.
Then you should try to debug what is in myData or item.
I finally got it to work. Thank you guys. One more question. After entering a second character, the autocomplete hides. Is there a length or something that I should set to change this?
|
0

Have you tried changing your Success function to this:

 success: function (mydata) {

        $("#tags").autocomplete({
            source: mydata.d
        });

    }

it is my experience that .net wraps the json object in a parameter called "d"

1 Comment

Have you tried this in compbination with Jeroens answer? I've recently made a autocomplete feature like that
0

This worked great for me and it follows the api at jquery.

$("#Dialogs_ProjectID").autocomplete({
    source: function (request, response) {
        $.ajax({
            type: "POST",
            url: "Log.aspx/jQuery_Get_Autocomplete",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({
                columnName: "Project",
                startsWith: request.term
            }),
            success: function (data) {
                response($.map(data.d, function (item) {
                    return {
                        value: item
                    }
                }));
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    }

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.