为什么不重写/修改它的 JavaScript 函数(在 autocomplete.js 中)?
感兴趣的代码和功能:
bindStaticEvents
bindStaticEvents: function() {
var $this = this;
this.bindKeyEvents();
this.dropdown.mouseover(function() {
$(this).addClass('ui-state-hover');
}).mouseout(function() {
$(this).removeClass('ui-state-hover');
}).mousedown(function() {
if($this.active) {
$(this).addClass('ui-state-active');
}
}).mouseup(function() {
if($this.active) {
$(this).removeClass('ui-state-active');
$this.search('');
$this.input.focus();
}
}).focus(function() {
$(this).addClass('ui-state-focus');
}).blur(function() {
$(this).removeClass('ui-state-focus');
}).keydown(function(e) {
var keyCode = $.ui.keyCode,
key = e.which;
if(key === keyCode.SPACE || key === keyCode.ENTER || key === keyCode.NUMPAD_ENTER) {
$(this).addClass('ui-state-active');
}
}).keyup(function(e) {
var keyCode = $.ui.keyCode,
key = e.which;
if(key === keyCode.SPACE || key === keyCode.ENTER || key === keyCode.NUMPAD_ENTER) {
$(this).removeClass('ui-state-active');
$this.search('');
$this.input.focus();
e.preventDefault();
e.stopPropagation();
}
});
bindKeyEvents
bindKeyEvents: function() {
var $this = this;
this.currentText = this.input.val();
this.previousText = this.input.val();
//bind keyup handler
this.input.keyup(function(e) {
var keyCode = $.ui.keyCode,
key = e.which,
shouldSearch = true;
$this.previousText = $this.currentText;
$this.currentText = this.value;
// Cancel a possible long running search when selecting an entry via enter
if (key === keyCode.ENTER || key === keyCode.NUMPAD_ENTER) {
if ($this.timeout) {
clearTimeout($this.timeout);
}
shouldSearch = false;
}
else if (key === keyCode.ESCAPE) {
$this.hide();
shouldSearch = false;
}
else if ((e.ctrlKey && key === 65) // ctrl+a
|| (e.ctrlKey && key === 67) // ctrl+c
|| key === keyCode.LEFT
|| key === keyCode.RIGHT
|| key === keyCode.TAB
|| key === 16 // keyCode.SHIFT
|| key === keyCode.HOME
|| key === keyCode.END
|| key === 18 // keyCode.ALT
|| key === 17 // keyCode.CONTROL
|| (key >= 112 && key <= 123)) { // F1-F12
shouldSearch = false;
}
else if(key === keyCode.UP || key === keyCode.DOWN) {
if($this.panel.is(':visible')) {
var highlightedItem = $this.items.filter('.ui-state-highlight');
if(highlightedItem.length) {
$this.displayAriaStatus(highlightedItem.data('item-label'));
}
}
shouldSearch = false;
}
else if($this.cfg.pojo && !$this.cfg.multiple && ($this.previousText !== $this.currentText)) {
$this.hinput.val($(this).val());
}
if(shouldSearch) {
var value = $this.input.val();
if(!value.length) {
$this.hide();
}
if(value.length >= $this.cfg.minLength) {
//Cancel the search request if user types within the timeout
if($this.timeout) {
clearTimeout($this.timeout);
}
var delay = $this.cfg.delay;
if (value != '' && (key == keyCode.BACKSPACE || key == keyCode.DELETE)) {
delay = $this.cfg.deletionDelay;
}
$this.timeout = setTimeout(function() {
$this.search(value);
}, delay);
}
}
}).keydown(function(e) {
var keyCode = $.ui.keyCode;
if($this.panel.is(':visible')) {
var highlightedItem = $this.items.filter('.ui-state-highlight');
switch(e.which) {
case keyCode.UP:
var prev = highlightedItem.length == 0 ? $this.items.eq(0) : highlightedItem.prevAll('.ui-autocomplete-item:first');
if(prev.length == 1) {
highlightedItem.removeClass('ui-state-highlight');
prev.addClass('ui-state-highlight');
if($this.cfg.scrollHeight) {
PrimeFaces.scrollInView($this.panel, prev);
}
if($this.cfg.itemtip) {
$this.showItemtip(prev);
}
}
e.preventDefault();
break;
case keyCode.DOWN:
var next = highlightedItem.length == 0 ? $this.items.eq(0) : highlightedItem.nextAll('.ui-autocomplete-item:first');
if(next.length == 1) {
highlightedItem.removeClass('ui-state-highlight');
next.addClass('ui-state-highlight');
if($this.cfg.scrollHeight) {
PrimeFaces.scrollInView($this.panel, next);
}
if($this.cfg.itemtip) {
$this.showItemtip(next);
}
}
e.preventDefault();
break;
case keyCode.ENTER:
case keyCode.NUMPAD_ENTER:
highlightedItem.click();
e.preventDefault();
e.stopPropagation();
break;
case 18: //keyCode.ALT:
case 224:
break;
case keyCode.TAB:
highlightedItem.trigger('click');
$this.hide();
break;
}
}
else if (e.which == keyCode.TAB) {
// clear pending search before leaving the field
if ($this.timeout) {
clearTimeout($this.timeout);
}
}
});
},
bindDynamicEvents
bindDynamicEvents: function() {
var $this = this;
//visuals and click handler for items
this.items.bind('mouseover', function() {
var item = $(this);
if(!item.hasClass('ui-state-highlight')) {
$this.items.filter('.ui-state-highlight').removeClass('ui-state-highlight');
item.addClass('ui-state-highlight');
if($this.cfg.itemtip) {
$this.showItemtip(item);
}
}
})
.bind('click', function(event) {
var item = $(this),
itemValue = item.attr('data-item-value');
if($this.cfg.multiple) {
var itemDisplayMarkup = '<li data-token-value="' + item.attr('data-item-value') + '"class="ui-autocomplete-token ui-state-active ui-corner-all ui-helper-hidden">';
itemDisplayMarkup += '<span class="ui-autocomplete-token-icon ui-icon ui-icon-close" />';
itemDisplayMarkup += '<span class="ui-autocomplete-token-label">' + item.attr('data-item-label') + '</span></li>';
$this.inputContainer.before(itemDisplayMarkup);
$this.multiItemContainer.children('.ui-helper-hidden').fadeIn();
$this.input.val('').focus();
$this.hinput.append('<option value="' + itemValue + '" selected="selected"></option>');
}
else {
$this.input.val(item.attr('data-item-label')).focus();
this.currentText = $this.input.val();
this.previousText = $this.input.val();
if($this.cfg.pojo) {
$this.hinput.val(itemValue);
}
}
$this.invokeItemSelectBehavior(event, itemValue);
$this.hide();
});
},
建议:将key === keyCode.SPACE 替换为key === 188(这是; 的代码)
注意:它可能会产生一些副作用,尤其是当您在其他地方使用自动完成功能时。