Not sure how to use the JQuery UI Autocomplete … :(

前端 未结 2 856
闹比i
闹比i 2020-12-18 17:28

this is a continuation from a previous JQueryUI Autocomplete question, I asked.

This time, I have my data returning ... but I have no idea how i define what

相关标签:
2条回答
  • 2020-12-18 17:39

    This code worked for me:

    $( "#Textbox" ).autocomplete({
        minLength: 2,
        source: function( request, response ) {
                var term = request.term;
                if ( term in cache ) {
                    response( cache[ term ] );
                    return;
                }
    
                var currentProject=$("#project option:selected").text();
                $.ajax({
                        url:  "url",
                        data: {term : request.term, IssueType :'Test', Project : currentProject},
                        dataType: "json",     
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function(data) { return data; },
                        success: function (data) {
                            var results = $.map(data, function(item){
                                return { value: item.value, id: item.id }}); 
                                cache[request.term] = results; response(results); }
                });
        }
    });
    
    0 讨论(0)
  • 2020-12-18 17:59

    Here's my working example of jQuery UI's autocomplete. Hope it helps:

        var cache = {};
        $("#textbox").autocomplete({
          source: function(request, response) {
           if (request.term in cache) {
            response($.map(cache[request.term].d, function(item) {
             return { value: item.value, id: item.id }
            }))
            return;
           }
           $.ajax({
            url: "/Services/AutoCompleteService.asmx/GetEmployees",  /* I use a web service */
            data: "{ 'term': '" + request.term + "' }",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataFilter: function(data) { return data; },
            success: function(data) {
             cache[request.term] = data;
             response($.map(data.d, function(item) {
              return {
               value: item.value,
               id: item.id
              }
             }))
            },
            error: HandleAjaxError  // custom method
           });
          },
          minLength: 3,
          select: function(event, ui) {
           if (ui.item) {
            formatAutoComplete(ui.item);   // custom method
           }
          }
         });
    

    If you're not doing so by now, get Firebug. It's an invaluable tool for web development. You can set a breakpoint on this JavaScript and see what happens.

    0 讨论(0)
提交回复
热议问题