2
0
forked from Wavyzz/dolibarr

Test: auto fill zip and town with jquery

This commit is contained in:
Regis Houssin
2010-10-12 17:14:53 +00:00
parent f2d26e54a9
commit e14802e8cb
4 changed files with 82 additions and 15 deletions

View File

@@ -610,19 +610,19 @@ class FormCompany
/** /**
* Retourne la liste deroulante des codes postaux et des villes associées * Retourne la liste deroulante des codes postaux et des villes associées
*/ */
function select_zipcode($selected='',$htmlname='zipcode') function select_zipcode($selected='',$field1='zipcode',$field2='town',$field3='fk_pays')
{ {
print ajax_autocompleter('',$htmlname,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n"; print ajax_autocompleter_ziptown($field1,$field2,$field3,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n";
print '<input id="search_'.$htmlname.'" type="text" name="'.$htmlname.'" size="6" value="'.$selected.'">'."\n"; print '<input id="'.$field1.'" type="text" name="'.$field1.'" size="6" value="'.$selected.'">'."\n";
} }
/** /**
* Retourne la liste deroulante des villes et des codes postaux associés * Retourne la liste deroulante des villes et des codes postaux associés
*/ */
function select_town($selected='',$htmlname='town') function select_town($selected='',$field1='town',$field2='zipcode',$field3='fk_pays')
{ {
print ajax_autocompleter('',$htmlname,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n"; print ajax_autocompleter_ziptown($field1,$field2,$field3,DOL_URL_ROOT.'/societe/ajaxziptown.php')."\n";
print '<input id="search_'.$htmlname.'" type="text" name="'.$htmlname.'" value="'.$selected.'">'."\n"; print '<input id="'.$field1.'" type="text" name="'.$field1.'" value="'.$selected.'">'."\n";
} }
} }

View File

@@ -128,6 +128,61 @@ function ajax_autocompleter($selected='',$htmlname,$url,$option='')
return $script; return $script;
} }
/**
* \brief Get value of field, do Ajax process and return result
* \param htmlname nom et id du champ
* \param url chemin du fichier de reponse : /chemin/fichier.php
* \return string script complet
*/
function ajax_autocompleter_ziptown($field1,$field2,$field3,$url,$option='')
{
$script='';
$script.= '<script type="text/javascript">';
$script.= 'jQuery(document).ready(function() {
jQuery("input#'.$field1.'").blur(function() {
//console.log(this.value.length);
if (this.value.length == 0)
{
jQuery("#'.$field1.'").val("");
jQuery("#'.$field2.'").val("");
}
});
jQuery("input#'.$field1.'").autocomplete({
source: function( request, response ) {
jQuery.get("'.$url.($option?'?'.$option:'').'", { '.$field1.': request.term }, function(data){
response( jQuery.map( data, function( item ) {
if (data.length == 1) {
jQuery("#'.$field1.'").val(item.value);
jQuery("#'.$field2.'").val(item.field2);
if (item.field3 > 0) {
jQuery("#'.$field3.'").val(ui.item.field3);
}
}
return {
label: item.label,
value: item.value,
field2: item.field2,
field3: item.field3
}
}));
}, "json");
},
dataType: "json",
minLength: 2,
select: function( event, ui ) {
jQuery("#'.$field2.'").val(ui.item.field2);
if (ui.item.field3 > 0) {
jQuery("#'.$field3.'").val(ui.item.field3);
}
}
});
});';
$script.= '</script>';
return $script;
}
/** /**
* Show an ajax dialog * Show an ajax dialog
* @param title Title of dialog box * @param title Title of dialog box

View File

@@ -96,9 +96,17 @@ if (! empty($_GET['zipcode']) || ! empty($_GET['town']))
$country = $row['fk_country']?($langs->trans('Country'.$row['country_code'])!='Country'.$row['country_code']?$langs->trans('Country'.$row['country_code']):$row['country']):''; $country = $row['fk_country']?($langs->trans('Country'.$row['country_code'])!='Country'.$row['country_code']?$langs->trans('Country'.$row['country_code']):$row['country']):'';
$row_array['label'] = $row['zip'].' '.$row['town'].' ('.$country.')'; $row_array['label'] = $row['zip'].' '.$row['town'].' ('.$country.')';
$row_array['zip'] = $row['zip']; if ($zipcode)
$row_array['town'] = $row['town']; {
$row_array['fk_country'] = $row['fk_country']; $row_array['value'] = $row['zip'];
$row_array['field2'] = $row['town'];
}
if ($town)
{
$row_array['value'] = $row['town'];
$row_array['field2'] = $row['zip'];
}
$row_array['field3'] = $row['fk_country'];
array_push($return_arr,$row_array); array_push($return_arr,$row_array);
} }

View File

@@ -3,7 +3,7 @@
* Copyright (C) 2003 Brian Fraval <brian@fraval.org> * Copyright (C) 2003 Brian Fraval <brian@fraval.org>
* Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005 Eric Seigne <eric.seigne@ryxeo.com> * Copyright (C) 2005 Eric Seigne <eric.seigne@ryxeo.com>
* Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr> * Copyright (C) 2005-2010 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2008 Patrick Raguin <patrick.raguin@auguria.net> * Copyright (C) 2008 Patrick Raguin <patrick.raguin@auguria.net>
* Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
* *
@@ -767,10 +767,14 @@ else
print '</textarea></td></tr>'; print '</textarea></td></tr>';
// Zip / Town // Zip / Town
print '<tr><td>'.$langs->trans('Zip').'</td><td><input size="6" type="text" name="cp" value="'.$soc->cp.'">'; print '<tr><td>'.$langs->trans('Zip').'</td><td>';
if ($conf->use_javascript_ajax && $conf->global->MAIN_AUTOFILL_TOWNFROMZIP) print ' <input class="button" type="button" name="searchpostalcode" value="'.$langs->trans('FillTownFromZip').'" onclick="autofilltownfromzip_PopupPostalCode(\''.DOL_URL_ROOT.'\',cp.value,ville,pays_id,departement_id)">'; $formcompany->select_zipcode($soc->cp,'zipcode','town','selectpays_id');
print '</td>'; //print '<input size="6" type="text" name="cp" value="'.$soc->cp.'">';
print '<td>'.$langs->trans('Town').'</td><td><input type="text" name="ville" value="'.$soc->ville.'"></td></tr>'; //if ($conf->use_javascript_ajax && $conf->global->MAIN_AUTOFILL_TOWNFROMZIP) print ' <input class="button" type="button" name="searchpostalcode" value="'.$langs->trans('FillTownFromZip').'" onclick="autofilltownfromzip_PopupPostalCode(\''.DOL_URL_ROOT.'\',cp.value,ville,pays_id,departement_id)">';
print '</td><td>'.$langs->trans('Town').'</td><td>';
$formcompany->select_zipcode($soc->ville,'town','zipcode','selectpays_id');
//print '<input type="text" name="ville" value="'.$soc->ville.'">';
print '</td></tr>';
// Country // Country
print '<tr><td width="25%">'.$langs->trans('Country').'</td><td colspan="3">'; print '<tr><td width="25%">'.$langs->trans('Country').'</td><td colspan="3">';