(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.)
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | autocomplete.patch | 2.44 KB | skari |
| #10 | autocomplete.patch.txt | 2.44 KB | skari |
| #7 | autocomplete-blur-2.patch | 3.28 KB | mgifford |
| #5 | autocomplete.patch.txt | 3.15 KB | skari |
| #1 | diff.txt | 2.79 KB | skari |
Comments
Comment #1
skari commentedComment #2
mgiffordI'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.
Comment #3
skari commentedHi 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.
Comment #4
mgiffordThere 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.
Comment #5
skari commentedOk I am attaching the patch for D7. Equivalent changes for D7.
Please check out the enhancements also.
Comment #6
skari commentedmoving back to 8.x-dev
Comment #7
mgiffordGotta 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.
Comment #8
nod_Lots of formatting issue here. needs spaces around
&&in the condition, always put{ }inif/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
Comment #9
skari commentedHi,
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:
in onkeydown:
Comment #10
skari commentedI 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.
Comment #11
skari commentedmoving back to 8.x-dev
Comment #12
skari commentedattaching with the correct ending
Comment #13
mgiffordI 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.
Comment #14
skari commentedI 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...
Comment #15
mgiffordIt'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.
Comment #16
robloachOver at #675446: Use jQuery UI Autocomplete, we gain some advantages.
Comment #16.0
robloachjust updating this to reflect changes for version 7 and 8
Comment #17
nod_Using jquery ui now.