Loading ...
Sorry, an error occurred while loading the content.

Re: Autocomplete - Is there a way to determine how selection was made?

Expand Messages
  • allanbjork
    ... After thinking for a bit, and trying a few more ideas, I came up with a way to do what I want. Essentially, I do a removeListener for keydown on the
    Message 1 of 4 , Jan 6, 2009
    • 0 Attachment
      --- In ydn-javascript@yahoogroups.com, "allanbjork" <allan@...> wrote:
      >
      > Hello Everyone,
      >
      > I'm replacing a custom, homegrown, tempermental between browsers
      > "Combobox" control with Autocomplete, but am having difficulty
      > replicating some of the behaviors.
      >
      > The existing control has different behaviors depending on if an item
      > was selected by Tab completion/Mouse Selection, or by Enter key.
      >
      > The itemSelectEvent doesn't indicate which of those actions
      > triggered it, and textboxKeyEvent doesn't propogate the Tab or Enter
      > key strokes to the subscriber.
      >
      > Also, I've tried adding a listener for "keydown" on the
      > Autocomplete's input element via YAHOO.util.Event.addListener, but
      > that is also not getting invoked.
      >
      > I see where I can modify the Autocomplete code (in
      > _onTextboxKeyDown) to capture the Tab/Enter key actions, but I really
      > don't want to do that.
      >
      > Does anyone know of a way within the YUI framework to determine how
      > the selection was triggered in an autocomplete?
      >
      > Thanks.
      >
      > --Allan
      >


      After thinking for a bit, and trying a few more ideas, I came up with a way to do what I want.

      Essentially, I do a removeListener for "keydown" on the input element of the Autocomplete. E.g.:

      elInput = oAutocomplete.getInputEl();
      YAHOO.util.Event.removeListener(elInput,"keydown",oAutocomplete._onTextboxKeyDown);

      I then add a custom listener with addListener that checks for the tab or enter key, setting a value for them
      in an additional dynamic property I added to the individual Autocomplete control. If it's neither, that
      property is set to null. This new listener then calls the original _onTextboxKeyDown handler for the
      individual Autocomplete control. E.g.:

      YAHOO.util.Event.addListener(elInput,"keydown",
      function(eventData,oEventAutocomplete) {
      switch (eventData.keyCode) {
      case 9 :
      case 13: {
      oEventAutocomplete.__X_selectionTrigger = eventData.keyCode;
      break;
      }

      default : {
      oEventAutocomplete.__X_selectionTrigger = null;
      break;
      }
      }

      oEventAutocomplete._onTextboxKeyDown(eventData, oEventAutocomplete);
      },oAutocomplete,true);



      Then in the itemSelectEvent callback I check the new property I defined, assuming a null represents a mouse
      selection, and take appropriate action.

      It works, and wasn't that much extra code, but having that information be a native part of the control
      would be nicer. :-)

      --Allan
    Your message has been successfully submitted and would be delivered to recipients shortly.