Autocomplete multiple fields

By Martin Lissmats (MartinLissmats)
This short tutorial will show how to autocomplete data to multiple fields.
In this tutorial I will assume you have a working autocomplete function up and running already, as seen in http://book.cakephp.org/view/632/autoComplete.

In my case, I have a model called "Customer" which stores information about customers, such as customer address data. A second model is "Invoice". When I add a new invoice I want to bind it to a customer and also fetch certain customer information to be added to the invoice.

You will see that I am fetching data from the customer model with the intent to store it in the invoice model as well. This may seem redundant, but in my case, once an invoice is created and sent out to a customer, the invoice stored in the database is not supposed to change even if the customer data is updated afterwards.

Form view

Here are some snippets of how my view with all the forms for adding a new invoice looks. When including the javascript to create a new autocompleter take note that I have added the option afterUpdateElement with the value setCustomerValues which will call a custom javascript function called setCustomerValues after an item from the autocomplete list has been selected.

View Template:

Download code
...
<?php
echo $form->input('customer_id', array('type' => 'hidden'));
echo 
$form->input('addr_name');
echo 
$form->input('addr_street');
echo 
$form->input('addr_city');
?>
<div id="customer_choices" class="autocomplete"></div>
...


...
<div id="javascripts" style="display:none;">
<script type="text/javascript">new Ajax.Autocompleter("InvoiceAddrName", "customer_choices", "/customers/autocomplete/", { frequency: '0.6', afterUpdateElement : setCustomerValues }, {});
</script>
</div>
...

Autocomplete view

With a normal autocomplete view, you have a list of items with the data inside the li html element. To bake in hidden additional data that is ment to be sent to other input fields, I include the data as attributes to the li element.

View Template:

Download code
<ul>
<?php foreach($customers as $customer): ?>
    <li addr_street="<?php echo @$customer['Customer']['addr_street'];?>" addr_city="<?php echo @$customer['Customer']['addr_city'];?>" customer_id="<?php echo @$customer['Customer']['id'];?>"><?php echo $customer['Customer']['name'];?></li>
<?php endforeach; ?>
</ul>

The javascript

As you hopefully remember, we added the option to call a custom function called setCustomerValues after an item is selected in the autocomplete list. This function has to be created and you need to put it in a javascript file and make sure to include the javascript file in your view.
Download code
//Use -1 as "attr" parameter value to setValue to clear the element.
function setValue(element,attr) {
    var e = document.getElementById(element);
    if (e==null){return;}
    if (attr == -1) {e.value = '';} 
    else if (attr != null && attr != ' ' && attr != '') {e.value = attr;}
}

function setCustomerValues(text, li)
{
    setValue('InvoiceCustomerId',li.getAttribute('customer_id'));
    setValue('InvoiceAddrStreet',li.getAttribute('addr_street'));
    setValue('InvoiceAddrCity',li.getAttribute('addr_city'));
}

 

Comments 1432

CakePHP Team Comments Author Comments
 

Comment

1 Can't get it working

Hmm i tried this method but I can't get it working.

Somehow my autosearch field dissapears after applying this, ( and its outside the hided div)
Posted May 11, 2010 by Niels Klijn
 

Comment

2 Solved it differently

I just made an onClick event in my autocomplete.ctp

View Template:


    onclick="BestelregelProductId.value = '<?php echo @$product['Product']['id'];?>',
             ProductGarantie.value = '<?php echo @$product['Product']['garantie'];?>',
             ProductVoorraad.value = '<?php echo @$product['Product']['voorraad'];?>',
             BestelregelPrijs.value = '<?php echo @$product['Product']['verkoop_prijs'];?>'
            "
>
Posted May 12, 2010 by Niels Klijn
 

Comment

3 thanks

Another thing i would suggest is using some sort of JS validaton, that auto generates from the validation rules.
both jquery and prototype have very good plugins for that.
top voip service
Posted Jul 14, 2010 by Jim
 

Comment

4 Help

i tried this method but I can't get it working.

Designer Resources, Candy Floss Machine
Posted Jul 15, 2010 by allby sie
 

Comment

5 allby

Hey allby,

What results are you getting with your implementation?

No more Mortgage
Posted Jul 15, 2010 by jim jones
 

Comment

6 thanks

What results are you getting with your implementation?
Porno izle
Posted Jul 17, 2010 by ahmet
 

Comment

7 film indir

film indir thank you admin..
Posted Jul 18, 2010 by maxilla
 

Comment

8 too early

it's a bit too early to comemnt, more live testing is needed. I'll put it on my denver co foreclosures Tulsa ok foreclosures evansville in foreclosures Wichita Ks foreclosures websites as well.
Posted Jul 19, 2010 by jim jones
 

Comment

9 x

i tried this method but I can't get it working.
Convert Miles To Meters
Posted Jul 19, 2010 by Mike
 

Comment

10 thank you

Posted Jul 21, 2010 by mark
 

Comment

11 Thank you

THANK YOU..
oyunlar1 oyunları Free flash games - flash oyunlar 3D oyunlar Free flash games - flash oyunlar CryTurk.Com Blog youtube videolarıyoutube video, giriş
Posted Jul 21, 2010 by mark
   

Comment

13 ed

When a few of us realize the exact soluton that some of the time that it begins to blend last week. car shipping Yes, It can be explained by your instructor.
Posted Jul 22, 2010 by dan kaylee
 

Comment

14 autocomplete

autocomplete really saves a lot of time, i know some might not be agreeing with me but you will after some time, cheers!


cunard cruises
Posted Jul 25, 2010 by bad anooj
 

Comment

15 thanks

This may seem redundant, but in my case, once an invoice is created and sent out to a customer, the invoice stored in the database is not supposed to change even if the customer data is updated afterwards.Gold Chains
Posted Jul 30, 2010 by emma