9

Following this very nice page, I am trying to insert multiple component links in one go through JavaScript:

var fieldsBuilder = $display.getView().properties.controls.fieldBuilder;

// The Field Type = Tridion.FieldBuilder.FieldTypeHandlers.ComponentLinkField 
var currentField = fieldsBuilder.getField(currentFieldName);

// Get the multivalued data
var selectList = document.getElementById("selectedList");
var selectListOptions = selectList.getElementsByTagName("option");
var newValues = new Array();
for (var i=0;i<selectListOptions.length;i++){
    newValues.push(selectListOptions[i].value);
}

currentField.setValues(currentField.getValues().concat(newValues));

For this field type I get a "This field allows a single value only." error, which makes a lot of sense, because the setValues function for this field type gives this:

function (b){
  var a=this,c=false;
  if(b&&a.properties.input)if(b.length>1)
  $assert.raiseError("This field allows a single value only.");
  else if(b.length==1){
  // ETC

.. while I am trying to insert an array of tcm IDs.

I probably have to do this in a totally different way, but can't really find any. Can anyone here help me out?

  • Could this simply be that your schema field supports multiple values? – johnwinter May 31 '13 at 15:43
  • It does allow multiple values. I checked that in Tridion and through the isMultivalued() method on the field. – Raimond May 31 '13 at 16:06
8

Ok, I've found out how to do it:

var currentField = fieldsBuilder.getField(fields[0].getFieldName());
var selectList = document.getElementById("selectedList");
var selectListOptions = selectList.getElementsByTagName("option");

// start at the first empty index or 0 if the first element has no value
var startNumFields = fields.length;
if (fields[0].getValues() == null)
{
    startNumFields = 0;
}

var selectOptionsLength = selectListOptions.length;

for (var j = startNumFields; j < selectOptionsLength + startNumFields; j++)
{
    // console.log("setting: " + selectListOptions[j-fields.length].value);
    if (j == 0)
    {
        fields[0].setValues([selectListOptions[j].value]);
    }
    else
    {
        var addedField = fieldsBuilder.properties.input.insertField(fields[0]);
        addedField.setValues([selectListOptions[j-fields.length].value]);
    }
}

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.