2014-09-23 67 views
0

我嘗試使用外部JSON作爲源的自動完成jQuery UI的插件:http://jqueryui.com/autocomplete/外部的Json與自動完成的jQuery

我不understrand的好方法,並且json的好形式。在我的例子中,我有我的json: [「tag1」,「tag2」,「tag3」]

自動完成似乎工作,但如果開始鍵入「a」,自動完成建議所有標記在json裏面。似乎自動完成不會過濾json的內容,並且始終在json中顯示整個標籤。 所以,我不明白如何有正常的行爲:例如,當我輸入「t」時,自動完成只會提示我「tag1」。

我的頁面在這裏: http://tcdemo.fr/temp/test.html

JSON的是在這裏: http://tcdemo.fr/temp/search.json

非常感謝

回答

0

看你的代碼,有三件事情你可以做一個解決方案:

  1. 處理服務器端的過濾並在endpoi上調用ajax函數nt它給出了過濾結果
  2. 如果它是一個靜態的對象集合,加載在.tagit() bind之外的json並將json結果傳遞給availableTags屬性。該過濾默認處理。

    $("#mytags").tagit({ 
        availableTags: jsonResult, // search.json is loaded into jsonResult 
        show_tag_url: "/tags/", 
        singleField: true, 
        singleFieldNode: $('#submit_tag_names') 
    }); 
    
  3. 否則,如果由於某種原因,喜歡你的方法 - 在JavaScript的最終過濾結果成功。在你的腳本更改成功函數:

    success: function(choices) { 
         var filter = search.term.toLowerCase(); 
         var filteredChoices = $.grep(choices, function(element) { 
          // Only match autocomplete options that begin with the search term. 
          return (element.toLowerCase().indexOf(filter) === 0); 
         }); 
         showChoices(this._subtractArray(filteredChoices, this.assignedTags())); 
        } 
    

希望這有助於