我为 Select2 Selection 创建了一个自定义适配器,我想在其中直接在选择而不是下拉列表中显示输入字段。
$.fn.select2.amd.define('SearchableSingleSelection', [
'select2/utils',
'select2/selection/single',
'select2/selection/eventRelay',
'select2/dropdown/search'
], function(Utils, SingleSelection, EventRelay, DropdownSearch) {
var adapter = Utils.Decorate(SingleSelection, DropdownSearch);
adapter = Utils.Decorate(adapter, EventRelay);
/**
* Renders the custom Select2 element with a search field inside.
* @returns {jQuery} The rendered Select2 element.
*/
adapter.prototype.render = function() {
var $rendered = DropdownSearch.prototype.render.call(this, SingleSelection.prototype.render);
this.$searchContainer.hide();
if (this.options.options.minimumResultsForSearch === Infinity) {
this.$searchContainer.find('input').prop("disabled", "disabled");
}
if (this.options.options.maximumInputLength) {
this.$searchContainer.find('input').attr("maxlength", this.options.options.maximumInputLength);
}
this.$element.siblings('.select2').find('.selection').prepend(this.$searchContainer);
return $rendered;
};
var bindOrigin = adapter.prototype.bind;
adapter.prototype.bind = function(container) {
var self = this;
bindOrigin.apply(this, arguments);
container.on('open', function() {
self.$selection.hide();
self.$searchContainer.show();
self.$searchContainer.find('input')[0].focus();
});
container.on('close', function() {
self.$searchContainer.hide();
self.$selection.show();
self.$searchContainer.find('input')[0].blur();
});
};
return adapter;
});
现在我也想自定义 AllowClear 功能,因为它不起作用。如何修改 AllowClear.prototype.update 和 AllowClear.prototype._handleClear 函数?