3

Is there some method or property to get the count of a multi-value field without iterating with .getNextFieldSibling?

Example:

var MV_Field = $display.getView().properties.controls.fieldBuilder.getField("MultiValueField_Name");
while (MV_Field.getNextFieldSibling() != null) {
    count = count+1;
    MV_Field = MV_Field.getNextFieldSibling();
}
7

I thought you could call field.getValues() to get all the field's values as an array. Then, you'll be able to count the number of values inside the array

so you would need to do something like

var values = MV_field.getValues();
var nrOfValues = values.length;

UPDATE

getValues() is indeed not the right way to get the items, although the naming does look confusing. The solution proposed in the accepted answer does return the number of items fields, but might be risky If i have a multi-value field with two items, and click the plus icon, but don't fill in any value, then the statement

 var count = fieldObj.getElement().parentElement.children.length - 1; 

returns 3, although, the actual number of values is two (as i didn't fill in a third value yet.

I did some quick testing and debugging with the gui extensions, and found out you can also get the values like this

 var fieldBuilder = $display.getView().properties.controls.fieldBuilder;
 var count = fieldBuilder.properties.dataRoot.getElementsByTagName("MultiValueField_Name").length;

That gives you the exact number of values entered, and also gives you direct access to the items (as an xml object).

  • 1
    field.getValues() on a multivalued field just returns you the first value, its length is always 1 (unless the field has no value). – Bart Koopman Oct 31 '13 at 15:51
6

Do this way

var fieldObj = $display.getView().properties.controls.fieldBuilder.getField("FieldName");
var count = fieldObj.properties.element.parentElement.children.length - 1;

OR

var count = fieldObj.getElement().parentElement.children.length - 1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.