Is there any rhyme or reason to the order in which the fields are returned from JSForm->getFields? It does not seem to be A-Z or Z-A or in the order the fields are actually placed on a form. Also it does not appear to use the tab order.
Is there any rhyme or reason to the order in which the fields are returned from JSForm->getFields? It does not seem to be A-Z or Z-A or in the order the fields are actually placed on a form. Also it does not appear to use the tab order.
Hi Jeff,
Perhaps it used the order of creation ? UUID order ?
Could be. I am wondering though if Servoy could tell us what order objects gotten from JSForm → get… come back in so that I know, and so that I can implement some sort of sorting routine.
Thanks
Superficially, I would guess they are in no order. If they are in Java in some sort of HashMap construct, order is unpredictable – isn’t it? Your Java’s better than mine, Jeff.
greg.
Would it make sense to have an optional parameter for all the SM->JSForm->get… methods which could be a method that acts as a callback fx for sorting?
This works…
var flds = frm.getFields().sort();
g.
Just for completeness – this is probably the “better” way to do it…
var flds = frm.getFields().sort(function(a,b) {return a.name > b.name;});
As it stands, the Java native field object gets coerced to a string by the default sort behavior. That currently returns “Field: fieldname” – so the default sort works, but you probably are better directly comparing the actual name properties of the field objects because you can’t guarantee the string coercion wouldn’t change sometime down the road.
You could mod this sort function to compare different field properties as well, if it’s not name you want to sort by.
g.
Hey Greg, thanks for the reply…
This works…
var flds = frm.getFields().sort();
But what exactly is that sorting on? The array sort fx obviously does dictionary order unless a callback is specified, but dictionary order on what values? The dp, the field name, the field text, the JSField obj itself? Then also how does that change depending on what “get” fx you use. Meaning if you did getFormVariables.sort() what would you be sorting on then? Maybe var names?
Yeah, that makes sense actually. Helpful post! Thank you
What are you trying to accomplish that requires you to have them sorted?
And if you want to sort on the i18n text property of each JSField obj after it has been converted to the appropriate locale string…
var SMFieldsSorted = solutionModel.getForm('myForm').getFields().sort(function(a,b) {return i18n.getI18NMessage(a.text) > i18n.getI18NMessage(b.text);});