Customizing autocomplete

jQuery UI Widgets
Post Reply
edilmar
Posts: 9
Joined: 29 Aug 2016, 22:52

06 Sep 2016, 18:02

My autocomplete behaviour must be like this: the user may type a primary key number or a description and types ENTER to process the AJAX request to the server.
For example, if my autocomplete is to show a filter from all countries in the world and I have a table like this:
TABLE CITY:
CODCITY INTEGER
DESCRIPTION VARCHAR(100)
with data:
1 - BRAZIL
2 - ARGENTINA
3 - ENGLAND
4 - GERMANY
5 - ITALY
6 - SOUTH AFRICA
7 - SOUTH KOREA
etc
I need:
1) if user types 1 + ENTER, I would like to show 1 - BRAZIL
2) if user types ARG + ENTER (minQueryLength="3"), I would like to show 2 - ARGENTINA
3) if user types SOU + ENTER, I would like to show 6 - SOUTH AFRICA and 7 - SOUTH KOREA
But I don't want that the searching works "without" ENTER key press, because sometimes
I have tables with many records and I need the user has sure about the searching he wants.
Today I user RichFaces autocomplete for this with onkeypress and oncomplete JS events,
where there I use minChars="100" and when the user types ENTER, onkeypress change
dinamically minChars to 3 and oncomplete returns to 100. And also if the value typed by
the user is a number, I change minChars to 1, to work searches like 1 - BRAZIL.
My JS code from RichFaces is this:

Code: Select all

// onkeypress call this
function enterToAutoComplete(objName, minChars, evtKeyPress) {
  if (isEnter(evtKeyPress)) {
    var input = getComponent(objName);
    var autocomplete = input.rf.component;
    var codigo = Number(autocomplete.value);
    if (!isNaN(codigo))
      minChars = 1;
    autocomplete.options.minChars = minChars;
    autocomplete.isFirstAjax = true;
    autocomplete.__updateState(evtKeyPress);
    return false;
  }
  else
    return true;
}
// oncomplete call this
function afterEnterToAutoComplete(objName) {
  var input = getComponent(objName);
  var autocomplete = input.rf.component;
  autocomplete.options.minChars = 100;
  return true;
}
How to do the same in PrimeFaces?

Post Reply

Return to “PrimeUI”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 5 guests