2012-11-21 79 views
0

類別返回的任何想法undefined不顯示jQuery UI自動完成類別

我得到的項目,但不是類別?

$.widget("custom.catcomplete", $.ui.autocomplete, { 
     _renderMenu: function(ul, items) { 
      var that = this, 
       currentCategory = ""; 
      $.each(items, function(index, item) { 
       if (item.category != currentCategory) { 
        ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>"); 
        currentCategory = item.category; 
       } 
       that._renderItemData(ul, item); 
      }); 
     } 
    }); 

    function split(val) { 
      return val.split(/,\s*/); 
     } 
     function extractLast(term) { 
      return split(term).pop(); 
     } 

    $("#people_q:not(.ui-autocomplete-input)").live("keyup", function (event) { 
     $(this).catcomplete({ 
      source: function (request, response) { 
       $.ajax({ 
        url: "/json/people_search.asp", 
        dataType: "json", 
        data: { 
         q:extractLast(request.term) 
        }, 
        success: function (data) { 
         response($.map(data.results.result, function (item) { 
          return { 
           label: item.sresult, 
           value: item.sresult 
          }; 
         })); 
        } 
       }); 
      }, 
      minLength: 2, 
      open: function() { 
       $(this).removeClass("ui-corner-all").addClass("ui-corner-top"); 
      }, 
      close: function() { 
       $(this).removeClass("ui-corner-top").addClass("ui-corner-all"); 
      }, 
       focus: function() { 
        // prevent value inserted on focus 
        return false; 
       }, 
       select: function(event, ui) { 
        var terms = split(this.value); 
        // remove the current input 
        terms.pop(); 
        // add the selected item 
        terms.push(ui.item.value); 
        // add placeholder to get the comma-and-space at the end 
        terms.push(""); 
        this.value = terms.join(", "); 
        return false; 
       } 
     }); 
    }); 

這裏是我用來提供自動完成檢查的有效Json。

{ 
    "results": { 
     "count": "8", 
     "result": [ 
      { 
       "id": 0, 
       "sresult": "web development", 
       "category": "tag" 
      }, 
      { 
       "id": 1, 
       "sresult": "entrepreneur", 
       "category": "tag" 
      }, 
      { 
       "id": 2, 
       "sresult": "Talks/Presentations", 
       "category": "tag" 
      }, 
      { 
       "id": 3, 
       "sresult": "music management", 
       "category": "tag" 
      }, 
      { 
       "id": 4, 
       "sresult": "User Experience Design", 
       "category": "tag" 
      }, 
      { 
       "id": 5, 
       "sresult": "English", 
       "category": "tag" 
      }, 
      { 
       "id": 6, 
       "sresult": "French", 
       "category": "tag" 
      }, 
      { 
       "id": 7, 
       "sresult": "entier Associates Ltd", 
       "category": "company" 
      } 
     ] 
    } 
} 

回答

1

你供應response回調不包括對每個項目category的數據。您應該可以修改success回撥,如下所示:

success: function (data) { 
    response($.map(data.results.result, function (item) { 
     return { 
      label: item.sresult, 
      value: item.sresult, 
      category: item.category // <----- 
     }; 
    })); 
} 
+0

傳奇般的狀態。 – chris