From: Coleman Watts Date: Sat, 19 Oct 2013 15:39:25 +0000 (-0700) Subject: CRM-13608 - API - Support 'sequential' param with getoptions X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=15a1171a6cc7b2089165c0128e17dc38464f4324;p=civicrm-core.git CRM-13608 - API - Support 'sequential' param with getoptions ---------------------------------------- * CRM-13608: Api3 'getoptions' ordering doesn't work as JSON http://issues.civicrm.org/jira/browse/CRM-13608 --- diff --git a/api/v3/Generic.php b/api/v3/Generic.php index 392fd4cf72..f02af9ed9d 100644 --- a/api/v3/Generic.php +++ b/api/v3/Generic.php @@ -228,11 +228,18 @@ function civicrm_api3_generic_getoptions($apiRequest) { unset($apiRequest['params']['context'], $apiRequest['params']['field']); $baoName = _civicrm_api3_get_BAO($apiRequest['entity']); - $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']); + $options = $output = $baoName::buildOptions($fieldName, $context, $apiRequest['params']); if ($options === FALSE) { return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list."); } - return civicrm_api3_create_success($options); + // Support 'sequential' output as a non-associative array + if (!empty($apiRequest['params']['sequential'])) { + $output = array(); + foreach ($options as $key => $val) { + $output[] = array('key' => $key, 'value' => $val); + } + } + return civicrm_api3_create_success($output); } /** diff --git a/templates/CRM/Contact/Form/Search/Builder.js b/templates/CRM/Contact/Form/Search/Builder.js index f1d0cdf1e4..71b981f92a 100644 --- a/templates/CRM/Contact/Form/Search/Builder.js +++ b/templates/CRM/Contact/Form/Search/Builder.js @@ -74,10 +74,10 @@ */ function fetchOptions(row, field) { if (CRM.searchBuilder.fieldOptions[field] === 'yesno') { - CRM.searchBuilder.fieldOptions[field] = {1: ts('Yes'), 0: ts('No')}; + CRM.searchBuilder.fieldOptions[field] = [{key: 1, value: ts('Yes')}, {key: 0, value: ts('No')}]; } if (typeof(CRM.searchBuilder.fieldOptions[field]) == 'string') { - CRM.api(CRM.searchBuilder.fieldOptions[field], 'getoptions', {field: field}, { + CRM.api(CRM.searchBuilder.fieldOptions[field], 'getoptions', {field: field, sequential: 1}, { success: function(result, settings) { var field = settings.field; if (result.count) { @@ -121,9 +121,9 @@ options = [options[0]]; } } - $.each(CRM.searchBuilder.fieldOptions[field], function(value, label) { - var selected = ($.inArray(value, options) > -1) ? 'selected="selected"' : ''; - select.append(''); + $.each(CRM.searchBuilder.fieldOptions[field], function(key, option) { + var selected = ($.inArray(option.key, options) > -1) ? 'selected="selected"' : ''; + select.append(''); }); select.change(); }