Autocomplete multiple fields
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.
Download code
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
Comment
1 Can't get it working
Somehow my autosearch field dissapears after applying this, ( and its outside the hided div)
Comment
2 Solved it differently
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'];?>'
"
>
Comment
3 thanks
both jquery and prototype have very good plugins for that.
top voip service
Comment
4 Help
Designer Resources, Candy Floss Machine
Comment
5 allby
What results are you getting with your implementation?
No more Mortgage
Comment
6 thanks
Porno izle
Comment
7 film indir
Comment
8 too early
Comment
9 x
Convert Miles To Meters
Comment
10 thank you
images upload, Resim yukleme, resim upload Images upload and resim yukleme sitesi. Diziizle- ezel izle gonulcelen Canli dizi izleme sitesi. youtube izle, youtube giris, Youtube Watch, download, Youtube izle, indir Dizi izle, dizisi, ezl izle Canli dizi izleme sitesi. Bedava flash oyunlar, Free flash games, flash oyunlar
Comment
11 Thank you
oyunlar1 oyunları Free flash games - flash oyunlar 3D oyunlar Free flash games - flash oyunlar CryTurk.Com Blog youtube videolarıyoutube video, giriş
Comment
12 great
SEROTEA.COM | Day Trans Travel Jakarta Bandung | Day Trans Travel Jakarta Bandung Memang Mantap | Tulisan Pendukung Day Tans Travel Jakarta Bandung | Day Trans SEO Challenge | Info Kontes Day Trans Travel Jakarta Bandung | Tutorial SEO Pemula | Day Trans Travel Jakarta Bandung | Day Trans Travel Jakarta Bandung | Day Trans Travel Jakarta Bandung | Day Trans Travel Jakarta Bandung | Day Trans Travel Jakarta Bandung | Day Trans Travel Jakarta Bandung | Day Trans Travel Jakarta Bandung | Day Trans Travel Jakarta Bandung | Travel Jakarta Bandung
Comment
13 ed
Comment
14 autocomplete
cunard cruises
Comment
15 thanks
Comment
16 eweewewewe
tramadol online buy tramadol tramadol tramadol rx buy from need pharmacy tramadol wizard 50 hcl mg tab tramadol online pharmacy sales tramadol buy cod tramadol tramadol drug test online pharmacy tramadol sales buy cheap online tramadol tramadol drug testing 120 tablet tramadol cheap tramadol buy online tramadol drug tests hydrochloride tablet tramadol tramadol best buy tramadol drugs tramadol online medication buy tramadol hcl urine drug testing of tramadol tramadol retard buy discount tramadol acetaminophen tramadol hcl cod pharmacy tramadol buy cheap prescription tramadol 50 hcl mg tramadol tramadol and dosage agcode buy tramadol tramadol hcl 50 mg tramadol and urine test buy tramadol cheap cod acetaminophen hcl par tramadol tramadol next day delivery 120 cheap tramadol medication tramadol tramadol on line cheap cod tramadol pill tramadol tramadol online cod very cheap tramadol tramadol pill