Merge pull request #20960 from homer8173/patch-37

Close #20930 NEW Use ajax for state loading after country change
This commit is contained in:
Laurent Destailleur 2023-09-11 11:53:08 +02:00 committed by GitHub
commit e20f9ca44a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View file

@ -143,6 +143,9 @@ if (GETPOST('zipcode') || GETPOST('town')) {
}
echo json_encode($return_arr);
} elseif (GETPOSTISSET('country_codeid')) {
$formcompany = new FormCompany($db);
print $formcompany->select_state(GETPOST('selected', 'int', 1), GETPOST('country_codeid', 'int', 1), GETPOST('htmlname', 'alpha', 1), GETPOST('morecss', 'alpha', 1));
}
$db->close();

View file

@ -368,6 +368,32 @@ class FormCompany extends Form
return $out;
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Returns the drop-down list of departments/provinces/cantons for all countries or for a given country.
* In the case of an all-country list, the display breaks on the country.
* The key of the list is the code (there can be several entries for a given code but in this case, the country field differs).
* Thus the links with the departments are done on a department independently of its name.
*
* @param string $parent_field_id Parent select name to monitor
* @param integer $selected Code state preselected (mus be state id)
* @param integer $country_codeid Country code or id: 0=list for all countries, otherwise country code or country rowid to show
* @param string $htmlname Id of department. If '', we want only the string with <option>
* @param string $morecss Add more css
* @return string String with HTML select
* @see select_country()
*/
public function select_state_ajax($parent_field_id = 'country_id', $selected = 0, $country_codeid = 0, $htmlname = 'state_id', $morecss = 'maxwidth200onsmartphone minwidth300')
{
$html = '<script>';
$html.='$("select[name=\"'.$parent_field_id.'\"]").change(function(){
$.ajax( "'.dol_buildpath('/core/ajax/ziptown.php', 2).'", { data:{ selected: $("select[name=\"'.$htmlname.'\"]").val(), country_codeid: $(this).val(), htmlname:"'.$htmlname.'", morecss:"'.$morecss.'" } } )
.done(function(msg) {
$("span#target_'.$htmlname.'").html(msg);
})
});';
return $html.'</script><span id="target_'.$htmlname.'">'.$this->select_state($selected, $country_codeid, $htmlname, $morecss).'</span>';
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**

View file

@ -1142,7 +1142,7 @@ if ($action == 'create' || $action == 'adduserldap') {
// Country
print '<tr><td>'.$form->editfieldkey('Country', 'selectcountry_id', '', $object, 0).'</td><td class="maxwidthonsmartphone">';
print img_picto('', 'country', 'class="pictofixedwidth"');
print $form->select_country((GETPOST('country_id') != '' ?GETPOST('country_id') : $object->country_id));
print $form->select_country((GETPOST('country_id') != '' ?GETPOST('country_id') : $object->country_id), 'country_id');
if ($user->admin) {
print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
}
@ -1152,7 +1152,7 @@ if ($action == 'create' || $action == 'adduserldap') {
if (empty($conf->global->USER_DISABLE_STATE)) {
print '<tr><td>'.$form->editfieldkey('State', 'state_id', '', $object, 0).'</td><td class="maxwidthonsmartphone">';
print img_picto('', 'state', 'class="pictofixedwidth"');
print $formcompany->select_state($object->state_id, $object->country_code, 'state_id');
print $formcompany->select_state_ajax('country_id', $object->state_id, $object->country_id, 'state_id');
print '</td></tr>';
}