The autoSuggestBehaviour is a very cool demonstration of AJAXy features. It’s a useful visual hint for the user and allows the user to quickly select a valid value for an input component. However, there is often a requirement to control the triggering of the autoSuggestBehaviour. For e.g. based on a minimum no. of characters typed in by the user. This can be achieved through a hacky Javascript solution at least until Oracle decides to add a word length control property to the ootb component. Hat tip to Eugene for most of the code here. I have merely tweaked his code with a few bug fixes to get this working.
So without further ado, here’s the javascript that I put together to achieve this. Note that the code may still have bugs. One issue I have noticed is that if you quickly type in the keyword (longer than trigger length) and without pause backspace to a length smaller than your trigger, the popup still gets triggered.
Trigger the initCustomAutoSuggest function on page load – say using a load clientListener.
var initialized = false; //Initialization flag function customAutoSuggest(e) { var src = e.getSource(); var len = AdfDhtmlEditableValuePeer.GetContentNode(e.getSource()).value.length; var peer = src.getPeer(); console.log('Text length::' + len); //Check for the word length if(len > 3) { console.log('Trigger auto suggest'); AdfAutoSuggestBehavior.prototype.originalAutoSuggest(e); } else { /* These were required to fix some behaviour issues like * popup not closing etc. */ this._cancelSmartListTimer(); if(this._timerId) { AdfPage.PAGE.rescheduleTimer(this._timerId, 500); } /* If content length is less than required count * then hide the popup */ if(peer.isPopupVisible(src, AdfAutoSuggestBehavior._POPUP_PANEL_ID)) { peer.hidePopup(src, AdfAutoSuggestBehavior._POPUP_PANEL_ID); } } } initCustomAutoSuggest = function () { AdfAutoSuggestBehavior.prototype._maxSuggestedItems = 10; if (!initialized) { /*Make a copy of the original autoSuggest behaviour for key up*/ AdfAutoSuggestBehavior.prototype.originalAutoSuggest = AdfAutoSuggestBehavior.prototype._fireKeyUp; /*user custom behavuour for key up action */ AdfAutoSuggestBehavior.prototype._fireKeyUp = customAutoSuggest; /* Find the input component */ var inp = AdfPage.PAGE.findComponentByAbsoluteId("it1"); /* Undo any clientListeners that may already have been added * to the input text. This code needs cleanup as it potentially * removes any clientListener that the developer may have added * in */ inp.setProperty("clientListeners", null); AdfAutoSuggestBehavior.prototype.initialize(inp); initialized = true; } }