4

It looks like something pretty easy, but I can't find how to do this.
I need to clear value for some field via javascript. It could be regular text , component link , keyword or dropdown field type. I have following code:

var field = $display.getView().properties.controls.fieldBuilder.getField("some_field");
field.clear();// I need somesting like this

Is it possible to do this in elegant way or I have to look into DOM and clear it manually separately for each field type?

  • Do you mean Anguilla? Angular-js is something completely different. – Nuno Linhares Jul 1 '14 at 18:51
  • I haven't tried it, but FieldBuilder has a clearContents() method described in the documentation. Would that work for you? – Alvin Reyes Jul 1 '14 at 19:16
  • @NunoLinhares Yes, you are right. My mistake, I meant Anguilla. – Vitaliy Jul 1 '14 at 19:42
  • @AlvinReyes Unfortunately I can't see such method within my FieldBuilder – Vitaliy Jul 1 '14 at 19:44
7

I had quick look in the code for the component dialog and found the following snippet that should help you resolve your problem:

Tridion.FieldBuilder.FieldTypeHandlers.MultiLineTextField.prototype.setValues = function MultiLineTextField$setValues(values)
{
    var changed = false;
    if (values && this.properties.input)
    {
        if (values.length > 1)
        {
            throw Error("This field allows a single value only.");
        }
        else if (values.length == 1)
        {
            var value = values[0];
            if (this.properties.input.value != value)
            {
                changed = true;
                this.properties.input.value = value;
            }
        }
        else if (this.properties.input.value.length > 0)
        {
            changed = true;
            this.properties.input.value = "";
        }
    }
    if (changed)
    {
        this.fireChange();
    }
};

I'm assuming for other field types such as SingleLineTextField the setting/clearing of the value is the same, or if there are differences it wouldn't be too much work to write a generic function to clear your value based on the field type.

  • Your answer is correct. In most cases setValues([]) clearing value. However there are some issue with KeywordField type. For some reason settings property is not set for input control. I've done short workaround for it. So finally method looks like: function clear(field){ if(field.getTypeName() == "Tridion.FieldBuilder.FieldTypeHandlers.KeywordField") field.getInputControl().settings = {}; field.setValues([]); } – Vitaliy Jul 2 '14 at 12:59
  • hi @vitaly yeah as i mentioned i suspect different field types will vary in solution. glad you got it working :) – johnwinter Jul 2 '14 at 14:15
  • maybe also post your method or code snippet that deals with these various code types for all :) – johnwinter Jul 2 '14 at 14:16
4

OK. So, I post my final method appereance:

var clear = function(field) {
    if (field.getTypeName() == "Tridion.FieldBuilder.FieldTypeHandlers.EmbeddedSchemaField") {
        var fieldDefinitions = field.properties.input.properties.fieldsDefinition.children;
        $.each(fieldDefinitions, function(i, fieldDefinition) {
            var fieldName = fieldDefinition.firstChild.firstChild.data;
            var currField = field.getField(fieldName);
            clear(currField);
        });
    } else {
        if (field.getTypeName() == "Tridion.FieldBuilder.FieldTypeHandlers.KeywordField")
            field.getInputControl().settings = {};
        field.setValues([]);
    }
};

Unfortunately, I didn't find more elegant way to get all fields from embeddable field (field.getInputControl().getFields() doesn't work and always returns empty array). Anyway above function works for me perfectly

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.