How to remove a text input field from a dummyInput block in Blockly Dynamically.

1,255 views
Skip to first unread message

Abhigya jain

unread,
May 29, 2017, 5:35:15 AM5/29/17
to Blockly
Hi,

I am new to blockly and have created a custom block where i want certain fields to be disabled based on my selection from a dropdown on the block.
I am not able to disable the text fields appended to a dummyInput

i try removeField("fieldName") ; // doesnt work
I want the fields "Sheet" and "Macro" to be removed when i select copy_sheet from the dropdown

following is my block:

Blockly.Blocks['excel_functions'] = {
  init: function() {
 
 this.FUNCTIONS =
[
     ["Copy Sheet", 'copy_sheet'],
     ["Run Macro", 'run_macro'],
     ["Delete Sheet", 'delete_sheet']
];
  
    this.appendValueInput("excel_opeartions")
        .setCheck(null)
        .appendField(new Blockly.FieldImage("http://icons.iconarchive.com/icons/carlosjj/microsoft-office-2013/256/Excel-icon.png", 20, 20, "*"))
        .appendField("Excel Operations")
.appendField(new Blockly.FieldDropdown(this.FUNCTIONS, function(option){
var input=this.sourceBlock_.getInput("ti");
if(option=='copy_sheet'){
if(this.sourceBlock_.getInput('destination_path')==null){
this.sourceBlock_.appendValueInput("destination_path")
.setCheck(null)
.appendField("Destination Path");
}
input.removeField("Sheet");
input.removeField('sheet_name');
input.removeField('Macro');
input.removeField('macro_name');
}
}), "excel_options")
        .appendField("Source Path");
    this.appendValueInput("destination_path")
        .setCheck(null)
        .appendField("Destination Path");
    
this.appendDummyInput("ti")
        .appendField("Sheet")
        .appendField(new Blockly.FieldTextInput("Sheet Name"), "sheet_name")
        .appendField("Macro")
        .appendField(new Blockly.FieldTextInput("Macro Name"), "macro_name")
        .appendField("From Sheet")
        .appendField(new Blockly.FieldTextInput("Sheet Name"), "from_sheet")
        .appendField("To Sheet")
        .appendField(new Blockly.FieldTextInput("Sheet Name"), "to_sheet");
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(120);
    this.setTooltip('');
    this.setHelpUrl('');
  }
};

Any suggestions are welcome

Thanks
Abhigya

Cory Diers

unread,
May 30, 2017, 1:48:33 PM5/30/17
to blo...@googlegroups.com
Hi Abhigya,

You're looking for mutators. Init will only be called when a block is first created, so updating values there won't allow the block to change inputs when your dropdown is created. There are essentially 3 things that need to be implemented to get this working properly. First, write mutationToDom and domToMutation, to make sure your blocks save and load properly to/from XML. Then, write a function to update the shape of your block. Finally, register the mutator with Blockly.Extensions, and set a validator on your dropdown, so your update function gets called on every change. The "is divisible by" block in math.js is a good example of this, if you want more reference to go by.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Cory Diers | Software Engineer | cory...@google.com | 

Reply all
Reply to author
Forward
0 new messages