2015-09-15 15 views
0

我被困在jQuery 1.7和jQueryUI 1.8.6上,目前無法升級(我們有相當多的已棄用和刪除的代碼,並且無法在此時升級) 。舊版本的jQueryUI中的類別AutoComplete控件

我可以使用jQueryUI's AutoComplete control,但我無法弄清楚如何使用類別 - 使用樣本found here

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

我得到一個錯誤:this._super is not a function,如果我刪除了這一行,然後調用widget()時,與Cannot read property 'element' of undefined以下行失敗。

我知道jQuery插件的語法在版本之間有所變化,但我似乎無法修改它們的示例,以便它可以與舊版本一起使用。 This question在1.7中暗示了一個稍微不同的語法,但是當我弄亂它時,我只是不斷收到不同的錯誤。

任何想法,我需要改變,使這項工作?

回答

1

_super方法是在新版本的jQuery UI中創建的,它在以前的版本中不存在。作爲一種解決方法,您可以撥打$.ui.autocomplete.prototype._create.call(this);,這基本上和_super一樣。


只要_renderItemData沒有退出要麼,你必須實現它,通過它更改爲_renderItem,然後調用.data("ui-autocomplete-item", item);

你完整的代碼改變是:

$.widget("custom.catcomplete", $.ui.autocomplete, { 
    _create: function() { 
    $.ui.autocomplete.prototype._create.call(this); 
    this.widget().menu("option", "items", "> :not(.ui-autocomplete-category)"); 
    }, 
    _renderMenu: function (ul, items) { 
    var that = this, 
     currentCategory = ""; 
    $.each(items, function (index, item) { 
     var li; 
     if (item.category != currentCategory) { 
     ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>"); 
     currentCategory = item.category; 
     } 
     li = that._renderItem(ul, item).data('ui-autocomplete-item', item); 
     if (item.category) { 
     li.attr("aria-label", item.category + " : " + item.label); 
     } 
    }); 
    } 
}); 

小提琴工作:http://jsfiddle.net/qjw165sz/1/

+1

完美,非常感謝。你保存了一天。 –