//if not setdefault any country
$selectElements = CRM_Core_PseudoConstant::$select();
}
- $element = $form->add('select', $name, $title, $selectElements, FALSE, array('class' => 'crm-select2'));
+ $element = $form->add('select', $name, $title, $selectElements);
}
elseif ($select == 'country') {
$selectElements = array('' => ts('- any -')) + CRM_Core_PseudoConstant::$select();
- $element = $form->add('select', $name, $title, $selectElements, FALSE, array('class' => 'crm-select2'));
+ $element = $form->add('select', $name, $title, $selectElements);
}
elseif ($select == 'county') {
if ( array_key_exists('state_province', $formValues) && !CRM_Utils_System::isNull($formValues['state_province'])) {
else {
$selectElements = array('' => ts('- any -'));
}
- $element = $form->add('select', $name, $title, $selectElements, FALSE, array('class' => 'crm-select2'));
+ $element = $form->add('select', $name, $title, $selectElements);
}
else {
$selectElements = array('' => ts('- any -')) + CRM_Core_PseudoConstant::$select();
if (empty($_GET['_value'])) {
CRM_Utils_System::civiExit();
}
+ $countries = (array) $_GET['_value'];
+ $elements = array();
+ $list = &$elements;
+ foreach ($countries as $val) {
+ $result = CRM_Core_PseudoConstant::stateProvinceForCountry($val);
- $result = CRM_Core_PseudoConstant::stateProvinceForCountry($_GET['_value']);
-
- $elements = array(array(
- 'name' => $result ? ts('- select a state -') : ts('- N/A -'),
- 'value' => '',
- ));
- foreach ($result as $id => $name) {
- $elements[] = array(
- 'name' => $name,
- 'value' => $id,
- );
+ // Option-groups for multiple countries
+ if ($result && count($countries) > 1) {
+ $elements[] = array(
+ 'name' => CRM_Core_PseudoConstant::country($val, FALSE),
+ 'children' => array(),
+ );
+ $list = &$elements[count($elements)-1]['children'];
+ }
+ foreach ($result as $id => $name) {
+ $list[] = array(
+ 'name' => $name,
+ 'value' => $id,
+ );
+ }
}
-
- echo json_encode($elements);
+ $placeholder = array(array('value' => '', 'name' => $elements ? ts('- select -') : ts('- N/A -')));
+ echo json_encode(array_merge($placeholder, $elements));
CRM_Utils_System::civiExit();
}
static function jqCounty() {
+ $elements = array();
if (!isset($_GET['_value']) || CRM_Utils_System::isNull($_GET['_value'])) {
$elements = array(
array('name' => ts('(choose state first)'), 'value' => '')
);
}
else {
- $result = CRM_Core_PseudoConstant::countyForState($_GET['_value']);
+ $states = (array) $_GET['_value'];
+ $list = &$elements;
+ foreach ($states as $val) {
+ $result = CRM_Core_PseudoConstant::countyForState($val);
- $elements = array(array(
- 'name' => $result ? ts('- select -') : ts('- N/A -'),
- 'value' => '',
- ));
- foreach ($result as $id => $name) {
- $elements[] = array(
- 'name' => $name,
- 'value' => $id,
- );
+ // Option-groups for multiple countries
+ if ($result && count($states) > 1) {
+ $elements[] = array(
+ 'name' => CRM_Core_PseudoConstant::stateProvince($val, FALSE),
+ 'children' => array(),
+ );
+ $list = &$elements[count($elements)-1]['children'];
+ }
+ foreach ($result as $id => $name) {
+ $list[] = array(
+ 'name' => $name,
+ 'value' => $id,
+ );
+ }
}
+ $placeholder = array(array('value' => '', 'name' => $elements ? ts('- select -') : ts('- N/A -')));
+ $elements = array_merge($placeholder, $elements);
}
echo json_encode($elements);
<script type="text/javascript">
{literal}
CRM.$(function($) {
+ var $form = $('form.{/literal}{$form.formClass}{literal}');
function chainSelect(e) {
var info = $(this).data('chainSelect');
var val = info.target.val();
var multiple = info.target.attr('multiple');
var placeholder = $(this).val() ? "{/literal}{ts escape='js'}Loading{/ts}{literal}..." : info.placeholder;
- !multiple && info.target.html('<option value="">' + placeholder + '</option>');
+ if (multiple) {
+ info.target.html('').prop('disabled', true).crmSelect2({placeholder: placeholder});
+ }
+ else {
+ info.target.html('<option value="">' + placeholder + '</option>').prop('disabled', true).crmSelect2();
+ }
if ($(this).val()) {
- if (multiple) {
+ $.getJSON(info.callback, {_value: $(this).val()}, function(data) {
var options = '';
- $.each($(this).val(), function(index, value) {
- $.getJSON(info.callback, {_value: value}, function(data) {
- $.each(data, function() {
- if (this.value) {
- options += '<option value="' + this.value + '">' + this.name + '</option>';
- }
- });
- info.target.html(options).val(val).trigger('change');
- });
- });
- }
- else {
- $.getJSON(info.callback, {_value: $(this).val()}, function(data) {
- var options = '';
+ function buildOptions(data) {
$.each(data, function() {
- if (this.name) {
+ if (this.children) {
+ options += '<optgroup label="' + this.name + '">';
+ buildOptions(this.children);
+ options += '</optgroup>';
+ }
+ else if (this.value || !multiple) {
options += '<option value="' + this.value + '">' + this.name + '</option>';
}
+ else {
+ info.target.crmSelect2({placeholder: this.name});
+ }
});
- info.target.html(options).val(val).trigger('change');
- });
- }
+ }
+ buildOptions(data);
+ info.target.html(options).val(val).prop('disabled', false).trigger('change');
+ });
+ }
+ else {
+ info.target.trigger('change');
}
}
+ function initField(selector) {
+ return $(selector, $form).css('min-width', '20em').crmSelect2();
+ }
{/literal}
{foreach from=$config->stateCountryMap item=stateCountryMap}
- {if $stateCountryMap.country && $stateCountryMap.state_province}
- $('select[name="{$stateCountryMap.country}"], #{$stateCountryMap.country}').data('chainSelect', {ldelim}
- callback: CRM.url('civicrm/ajax/jqState'),
- target: $('select[name="{$stateCountryMap.state_province}"], #{$stateCountryMap.state_province}'),
- placeholder: "{ts escape='js'}(choose country first){/ts}"
- {rdelim}).on('change', chainSelect);
- {/if}
{if $stateCountryMap.state_province && $stateCountryMap.county}
- $('select[name="{$stateCountryMap.state_province}"], #{$stateCountryMap.state_province}').data('chainSelect', {ldelim}
+ $('select[name="{$stateCountryMap.state_province}"], select#{$stateCountryMap.state_province}', $form).data('chainSelect', {ldelim}
callback: CRM.url('civicrm/ajax/jqCounty'),
- target: $('select[name="{$stateCountryMap.county}"], #{$stateCountryMap.county}'),
+ target: initField('select[name="{$stateCountryMap.county}"], #{$stateCountryMap.county}'),
placeholder: "{ts escape='js'}(choose state first){/ts}"
{rdelim}).on('change', chainSelect);
{/if}
+ {if $stateCountryMap.country && $stateCountryMap.state_province}
+ initField('select[name="{$stateCountryMap.country}"], select#{$stateCountryMap.country}').data('chainSelect', {ldelim}
+ callback: CRM.url('civicrm/ajax/jqState'),
+ target: initField('select[name="{$stateCountryMap.state_province}"], #{$stateCountryMap.state_province}'),
+ placeholder: "{ts escape='js'}(choose country first){/ts}"
+ {rdelim}).on('change', chainSelect).change();
+ {/if}
{/foreach}
{literal}
});