Skip to content

Dropdown ordering #334

Closed
Closed
@PhilBroadbent

Description

@PhilBroadbent

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?

Activity

davidlgj

davidlgj commented on Apr 1, 2015

@davidlgj
Contributor

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

PhilBroadbent

PhilBroadbent commented on Apr 1, 2015

@PhilBroadbent
Author

object.

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

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

davidlgj

davidlgj commented on Apr 1, 2015

@davidlgj
Contributor

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

PhilBroadbent commented on Apr 1, 2015

@PhilBroadbent
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

PhilBroadbent commented on Apr 2, 2015

@PhilBroadbent
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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Dropdown ordering · Issue #334 · json-schema-form/angular-schema-form