Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dropdown ordering #334

Closed
PhilBroadbent opened this issue Mar 31, 2015 · 5 comments
Closed

Dropdown ordering #334

PhilBroadbent opened this issue Mar 31, 2015 · 5 comments

Comments

@PhilBroadbent
Copy link

Is it possible to have the dropdowns sorted in alphabetical order?
I have some enumeration mapping 'object' which I have. I loop through this and if it has an enum which matches a field i have, then it assigns the enum mapping to the formDefinition fields titleMap.

  populateFormDefinition : function(formDefinition, enums){
          _.forEach(formDefinition,function(formElementMeta,columnIndex){
            _.forEach(formDefinition[columnIndex],function(formElementMeta,rowIndex){
                if(typeof(enums[formElementMeta.key]) !== 'undefined' ){
                  formDefinition[columnIndex][rowIndex].titleMap = enums[formElementMeta.key];
                }
            });
          });
        },

If you debug and inspect the titleMap, it's in order, e.g.
'a' : 'a'
'b' : 'b' ....
However, when it is rendered onthe screen, it seems to be in a random order?

Is this a bug or am I doing something wrong, can anyone advise please?
Thanks.

Also while I'm here - is there a way to have an empty dropdown value - so that you can 'unselect' a value.....I could simply add a blank mapping to the titlemap, i.e. '':'' but is there a more elegent solution?

@davidlgj
Copy link
Contributor

davidlgj commented Apr 1, 2015

Do you use the object or the array form of the titleMap?

@PhilBroadbent
Copy link
Author

object.

e.g. enums[formElementMeta.key] could be:

Object
"": ""
AP: "Asia Pacific"
EM: "EMEA"
GL: "Global"
NA: "North America"
proto: Object

@davidlgj
Copy link
Contributor

davidlgj commented Apr 1, 2015

Ok, it should be in the object order, but since object order is not guaranteed in javascript I'll recommend you use the array syntax for the titleMap (the object syntax is there for JSON Form compatability). I wonder if this popped up when we changed from a ng-repeat of options to an ng-options... Anyways, I'm in the process of merging #294 and that will solve the "empty" option problem for you next release.

@PhilBroadbent
Copy link
Author

Ahh nice! I thought it would have to be an array of strings, but I've just looked at docs and its an array of objects, I will try this out in the morning, thanks a lot!

I must say I love this project - the documentation is great and the team is so quick to resond, thank you!

I'll leave this open for now, and providing it works I'll close it in morning.

@PhilBroadbent
Copy link
Author

Okay so if anyone else has this - you should use array of objects containing single value/name pair, rather than just object containing many mappings. (See my above object I used). To convert this object into a sorted array of objects I did this:

  var sortedArrayTitleMap = [];
  var valueArray = [];

  for(var key in enums[formElementMeta.key]) {
      valueArray.push(enums[formElementMeta.key][key]);
  }
  valueArray.sort();

  valueArray.forEach(function(value){
        sortedArrayTitleMap.push({value: (_.invert(enums[formElementMeta.key]))[value] , name: value});
  });

  formDefinition[columnIndex][rowIndex].titleMap = sortedArrayTitleMap;

Thanks David for pointing this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants