Skip to content
Snippets Groups Projects
Commit 9d145b38 authored by Regis Houssin's avatar Regis Houssin
Browse files

Test: auto fill zip and town with jquery

parent e14802e8
Branches
No related tags found
No related merge requests found
...@@ -612,8 +612,8 @@ class FormCompany ...@@ -612,8 +612,8 @@ class FormCompany
*/ */
function select_zipcode($selected='',$field1='zipcode',$field2='town',$field3='fk_pays') function select_zipcode($selected='',$field1='zipcode',$field2='town',$field3='fk_pays')
{ {
print ajax_autocompleter_ziptown($field1,$field2,$field3,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n"; print ajax_autocompleter_ziptown($selected='',$field1,$field2,$field3,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n";
print '<input id="'.$field1.'" type="text" name="'.$field1.'" size="6" value="'.$selected.'">'."\n"; print '<input id="search_'.$field1.'" type="text" name="search_'.$field1.'" size="6" value="'.$selected.'">'."\n";
} }
/** /**
...@@ -621,8 +621,8 @@ class FormCompany ...@@ -621,8 +621,8 @@ class FormCompany
*/ */
function select_town($selected='',$field1='town',$field2='zipcode',$field3='fk_pays') function select_town($selected='',$field1='town',$field2='zipcode',$field3='fk_pays')
{ {
print ajax_autocompleter_ziptown($field1,$field2,$field3,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n"; print ajax_autocompleter_ziptown($selected='',$field1,$field2,$field3,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n";
print '<input id="'.$field1.'" type="text" name="'.$field1.'" value="'.$selected.'">'."\n"; print '<input id="search_'.$field1.'" type="text" name="search_'.$field1.'" value="'.$selected.'">'."\n";
} }
} }
......
...@@ -134,13 +134,15 @@ function ajax_autocompleter($selected='',$htmlname,$url,$option='') ...@@ -134,13 +134,15 @@ function ajax_autocompleter($selected='',$htmlname,$url,$option='')
* \param url chemin du fichier de reponse : /chemin/fichier.php * \param url chemin du fichier de reponse : /chemin/fichier.php
* \return string script complet * \return string script complet
*/ */
function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='') function ajax_autocompleter_ziptown($selected='',$field1,$field2,$field3,$url,$option='')
{ {
$script=''; $script='';
$script.= '<input type="hidden" name="'.$field1.'" id="'.$field1.'" value="'.$selected.'" />';
$script.= '<script type="text/javascript">'; $script.= '<script type="text/javascript">';
$script.= 'jQuery(document).ready(function() { $script.= 'jQuery(document).ready(function() {
jQuery("input#'.$field1.'").blur(function() { jQuery("input#search_'.$field1.'").blur(function() {
//console.log(this.value.length); //console.log(this.value.length);
if (this.value.length == 0) if (this.value.length == 0)
{ {
...@@ -148,15 +150,16 @@ function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='') ...@@ -148,15 +150,16 @@ function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='')
jQuery("#'.$field2.'").val(""); jQuery("#'.$field2.'").val("");
} }
}); });
jQuery("input#'.$field1.'").autocomplete({ jQuery("input#search_'.$field1.'").autocomplete({
source: function( request, response ) { source: function( request, response ) {
jQuery.get("'.$url.($option?'?'.$option:'').'", { '.$field1.': request.term }, function(data){ jQuery.get("'.$url.($option?'?'.$option:'').'", { '.$field1.': request.term }, function(data){
response( jQuery.map( data, function( item ) { response( jQuery.map( data, function( item ) {
if (data.length == 1) { if (data.length == 1) {
jQuery("#'.$field1.'").val(item.value); jQuery("#'.$field1.'").val(item.value);
jQuery("#'.$field2.'").val(item.field2); jQuery("#'.$field2.'").val(item.field2);
jQuery("#search_'.$field2.'").val(item.field2);
if (item.field3 > 0) { if (item.field3 > 0) {
jQuery("#'.$field3.'").val(ui.item.field3); jQuery("#'.$field3.'").val(item.field3);
} }
} }
return { return {
...@@ -172,6 +175,7 @@ function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='') ...@@ -172,6 +175,7 @@ function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='')
minLength: 2, minLength: 2,
select: function( event, ui ) { select: function( event, ui ) {
jQuery("#'.$field2.'").val(ui.item.field2); jQuery("#'.$field2.'").val(ui.item.field2);
jQuery("#search_'.$field2.'").val(ui.item.field2);
if (ui.item.field3 > 0) { if (ui.item.field3 > 0) {
jQuery("#'.$field3.'").val(ui.item.field3); jQuery("#'.$field3.'").val(ui.item.field3);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment