(Ok, I am just editing this a little bit to point out the fact that this was initially reported for version 6.x, but is )

This patch fixes one minor bug in autocomplete.js and adds a little functionality. The addition of functionality doesn't break any prior behavior or code.

The bug:
The original behavior attach function takes a "context" parameter, but doesn't use it.

Description of the new functionality:

Assume we have an id and a name form element, i.e.

        $form['id'] = array(
                '#type' => 'textfield',
                '#title' => t('Id'),
                '#required' => true,
                '#default_value' => $custdata['sid'],
                '#autocomplete_path' => 'somepath/id-lookup';
                '#size' => 11
        );
        $form['name'] = array(
                '#type' => 'textfield',
                '#title' => t('Name'),
                '#required' => true,
                '#size' => 30,
                '#attributes' => array('class'=>array('autocomplete-id')),
        );

Note the attribute for name element. It hooks the name element with autocomplete select handler for the id element. I.e. when the id element gets populated with a value from the autocomplete lookup, the name element is auto populated with the associated name value for the id value.

The autocomplete lookup function can return whole records, using the simple rule that the returned keys are potential values for the main-lookup field, while individual fields from the records map to form elements with the same name (provided they have their class set to 'autocomplete-
'

An example autocomplete lookup function for the above case could be:

function somelookup($id) {
     $data = get_custdata_by_id($id);
     $values = array();
     foreach ($data as $cust) {
        $mapped = array();
        $mapped['id'] = $cust['identification_number'];
        $mapped['name'] = $cust['name'];
        $values[$cust['identification_number']] = $mapped;
     }       
     drupal_json_output($values);
}

Another change is also in the jsAC function. The change consists in forcing the select function (and hence the autopopulation of associated elements) to trigger if the user enters a value exisiting in the "drop-down" list without actually selecting/highlighting it. The only thing that remains to be considered for that (and it is simply a question of selecting an appropriate timeout) is to handle the case when the user enter the value "too-fast".

I've been using this for quite some time now and found it very useful.

(Note in the select handler there is also a check for if the element has the attribute "stripid". This is a special case I use in dynamic tables, i.e. tables where users can add new rows to an input form. In that case I can have form elements like id1, id2,... name1, name2,... so when pairing the name attributes with their corresponding id values, I need to strip the id from the name in order to match it in the returned record.)

Comments

skari’s picture

StatusFileSize
new2.79 KB
mgifford’s picture

Status: Needs review » Postponed (maintainer needs more info)

I'm marking this as postponed as all the activity is now all in D8. I'm not certain if this has been fixed in D7.

Please let us know if this is still a concern.

skari’s picture

Hi mgifford,

Wow, it took a long time to get any feedback on this one! :)
Well, I can tell you that it has not been fixed in D7, and I have just recently made some similar changes to the autocomplete.js that comes with D7. I will probably upload it as I think the fix is necessary and the enhancements are very useful.

mgifford’s picture

Version: 6.x-dev » 8.x-dev
Status: Postponed (maintainer needs more info) » Active
Issue tags: +JavaScript

There are a lot of issues, and sometimes you've got to be persistent before it makes it to the top of the priority list.

I look forward to seeing the code. Since it's still a problem in 7, I'm going to bump this up as a D8 issue. Hopefully it's something we can backport to D7.

skari’s picture

Version: 8.x-dev » 7.x-dev
StatusFileSize
new3.15 KB

Ok I am attaching the patch for D7. Equivalent changes for D7.
Please check out the enhancements also.

skari’s picture

Version: 7.x-dev » 8.x-dev

moving back to 8.x-dev

mgifford’s picture

Status: Active » Needs review
StatusFileSize
new3.28 KB

Gotta run the bot and have it working with D8. I've fixed up the code accordingly and created a patch following http://drupal.org/node/707484

It's a bit easier for folks to evaluate it in any case.

nod_’s picture

Status: Needs review » Needs work

Lots of formatting issue here. needs spaces around && in the condition, always put { } in if/else, indentation is two spaces, no tabs.

Is the other change related to that by any chance?
#1483554: Autocomplete textfield and ajax: tab key fires up ajax before setting autocomplete value
#1455842: Wrong benaviour of exposed autocomplete js

skari’s picture

Hi,

Sorry about the formatting issues.
No this patch doesn't address these issues directly. However, I want to make a couple of remarks about them:
#1483554:
While not fixing the issue as it stands, the enhancements I made to autocomplete provide a different way for tackling the problem. Using the enhancements the ajax would fetch all relevant associated values for each value displayed in the drop-down. The selection of an item in the drop-down then triggers an auto-population of the associated fields.

#1455842:
This is a classic problem with the enter key. Should the enter key submit the form or should it just confirm the selection in the drop-down box? A simple way to tackle this is to move the handling of the enter key from the onkeyup into the onkeydown handler of the jsAC.

So in onkeyup we would have:

  case 9:  // Tab.
  case 27: // Esc.
      this.hidePopup(e.keyCode);
      return true;

in onkeydown:

  case 13:
       this.hidePopup();
       return true;
skari’s picture

Version: 8.x-dev » 7.x-dev
StatusFileSize
new2.44 KB

I am attaching a new patch.

Just discovered that the patch file I uploaded the other day was missing an important step for the new functionality. Please check this again. I also cleaned up the formatting issues.

I included the change I mention in comment #9, i.e. hitting enter in the drop-down list will select the currently highligted item, and not submit the current form.

skari’s picture

Version: 7.x-dev » 8.x-dev

moving back to 8.x-dev

skari’s picture

StatusFileSize
new2.44 KB

attaching with the correct ending

mgifford’s picture

Status: Needs work » Needs review

I set the patch to needs review, but it doesn't follow the new git format so don't think it will pass the automated testing.

skari’s picture

I am just wondering about who is the owner/maintainer of the autocomplete functionality in core. Is it you mgifford or...? I did try searching for this, but didn't find anything conclusive. But maybe my search wasn't thorough enough.

Anyway, I would be interested in discussing a few points regarding autocomplete. I have added a little more functionality to the unit (just a few lines), and was thinking about adding yet another patch. On the other hand I am not sure about if anyone is interested in this or willing to try it out etc. So...

mgifford’s picture

It's not me. You'll need to express your ideas here. Also, useful to take a look at some of the other issues involving autocomplete & jQuery.

robloach’s picture

robloach’s picture

Issue summary: View changes

just updating this to reflect changes for version 7 and 8

nod_’s picture

Issue summary: View changes
Status: Needs review » Closed (won't fix)

Using jquery ui now.