diff --git a/app/View/CoPetitions/petition-attributes.inc b/app/View/CoPetitions/petition-attributes.inc
index 97dff6401..e73b62d17 100644
--- a/app/View/CoPetitions/petition-attributes.inc
+++ b/app/View/CoPetitions/petition-attributes.inc
@@ -390,7 +390,7 @@
'fieldName' => $fieldName,
'modelName' => $ea['model'],
'required' => $ea['required'],
- 'default' => $ea['default']
+ 'default' => $ea['default'] ?? ''
);
print $this->element('enumerableField', $args);
break;
diff --git a/app/View/Elements/enumerableField.ctp b/app/View/Elements/enumerableField.ctp
index 3e8683502..2bcbc49df 100644
--- a/app/View/Elements/enumerableField.ctp
+++ b/app/View/Elements/enumerableField.ctp
@@ -70,7 +70,7 @@ $allowOther = isset($vv_enums[$field]['allow_other']) && $vv_enums[$field]['allo
-
+
name);
// And whether or not other values are permitted
var other = [];
-
+
+ // Holding the default values
+ var p = {};
+
$cfg) {
@@ -87,7 +90,7 @@ $tname = Inflector::tableize($this->name);
$cfg = $vv_enums[$attr];
// The current persisted value in the database (if any)
- $curval = "";
+ $curval = '';
if(!empty($$tname[0][ $attrBits[0] ][ $attrBits[1] ])) {
// Regular model
@@ -98,12 +101,13 @@ $tname = Inflector::tableize($this->name);
if(isset($ea['model'])
&& $ea['model'] == $attrBits[0]
&& $ea['field'] == $attrBits[1]) {
- $curval = $ea['default'];
+ $curval = $ea['default'] ?? '';
}
}
}
- print "var p" . Inflector::camelize($attrBits[1]) . " = \""
+ // We are passing the defaultValue to a tmp variable
+ print "p['" . $attrBits[0] . Inflector::camelize($attrBits[1]) . "'] = \""
. $curval
. "\";\n";
@@ -213,42 +217,56 @@ $tname = Inflector::tableize($this->name);
// Always hide the actual value field when the dictionary is in use
$("#-field").hide("fade");
- var select = document.getElementById(attrid + 'Select');
-
- if(select.options.length > 0) {
- select.options.length = 0;
- }
-
- var found = 0; // Did we find the current value in the select list?
- var i = 0;
-
- select.options[i++] = new Option('', '');
-
- if(coded[curattr]) {
- // We want the code as the select value
- for(j in enums[curattr]) {
- select.options[i++] = new Option(enums[curattr][j], j);
-
- if(p == j) {
- select.selectedIndex = i-1;
- found++;
- }
+ const select = document.getElementById(attrid + 'Select');
+
+ if(select !== undefined && select !== null) {
+ if(select.options.length > 0) {
+ select.options.length = 0;
}
- } else {
- // No code, so just use the entry as the value
- for(j in enums[curattr]) {
- select.options[i++] = new Option(enums[curattr][j]);
-
- if(p == enums[curattr][j]) {
- select.selectedIndex = i-1;
- found++;
+
+ let found = 0; // Did we find the current value in the select list?
+ let i = 0;
+
+ // Add an empty option
+ select.options[i++] = new Option('', '', false, false);
+
+ if(coded[curattr]) {
+ // sort the fields
+ const sortedEnumCurAttr = sortProperties(enums[curattr])
+ // We want the code as the select value
+ for(let j in sortedEnumCurAttr) {
+ select.options[i++] = new Option(
+ sortedEnumCurAttr[j][1],
+ sortedEnumCurAttr[j][0],
+ false,
+ p[attrid] === sortedEnumCurAttr[j][0]
+ );
+
+ if(p[attrid] === sortedEnumCurAttr[j][0]) {
+ found++;
+ }
+ }
+ } else {
+ // No code, so use the entry as the value
+ // todo: when do i get in here??
+ for(let j in enums[curattr]) {
+ select.options[i++] = new Option(
+ enums[curattr][j],
+ enums[curattr][j],
+ false,
+ p[attrid] == j.toString()
+ );
+
+ if(p[attrid] == j.toString()) {
+ found++;
+ }
}
}
- }
- if(!found) {
- // Set the default value in the other field
- document.getElementById(attrid + 'Other').value = p;
+ if(found == 0) {
+ // Set the default value in the other field
+ document.getElementById(attrid + 'Other').value = p[attrid];
+ }
}
} else {
// Standard form element, hide the enumeration widgets
diff --git a/app/webroot/js/comanage.js b/app/webroot/js/comanage.js
index 97f4c1ace..111c13ca4 100644
--- a/app/webroot/js/comanage.js
+++ b/app/webroot/js/comanage.js
@@ -512,4 +512,28 @@ function callRegistryAPI(url, method, dataType, successCallback, entityId, failu
return xhr;
}
});
+}
+
+
+/**
+ * Sort a list of key:value properties and return an array of sorted objects.
+ * @param obj {key: value} List of key:value properties
+ * @return []{key: value} sorted
+ */
+function sortProperties(obj)
+{
+ // convert object into array
+ var sortable=[];
+ for(var key in obj)
+ if(obj.hasOwnProperty(key))
+ sortable.push([key, obj[key]]); // each item is an array in format [key, value]
+
+ // sort items by value
+ sortable.sort(function(a, b)
+ {
+ var x=a[1].toLowerCase(),
+ y=b[1].toLowerCase();
+ return xy ? 1 : 0;
+ });
+ return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ]
}
\ No newline at end of file
|