set value of Blockly.FieldLabel dynamically

1,326 views
Skip to first unread message

kushal bhattacharya

unread,
Jul 27, 2016, 3:34:19 AM7/27/16
to Blockly
hello everyone .I  now want to change the value of  Blockly.FieldLabel whixh is inside a dummy input  i wat to change the value of Blockly.FieldLabel  when i click the second block shown in the image below there  would be a pop up dialod which would show list of program names in the  dropdown list. When i select an option on the dropdown list and click ok the value will  replace the original value of the second block (which is put in Blockly.FieldLabel).
Thank u . 
 i am also posting the code in the next comment 
setdynamic.png

kushal bhattacharya

unread,
Jul 27, 2016, 3:35:31 AM7/27/16
to Blockly

                Blockly.Blocks['require_file'] = {
                    init: function () {
                        //Require_block=this;
                        this.appendValueInput("Req")
                                .setCheck(null)
                                .appendField("Require");
                        this.setInputsInline(false);

                        this.setColour(65);
                        // this.setTooltip('');
                        //this.setHelpUrl('http://www.example.com/');
                    }
                };
                Require_block = Blockly.Blocks['require_file'];
                Blockly.JavaScript['require_file'] = function (block) {
                    // String or array length.
                    var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE',
                            Blockly.JavaScript.ORDER_FUNCTION_CALL) || '\'\'';
                    return [argument0 + '.length', Blockly.JavaScript.ORDER_MEMBER];
                };
                Blockly.Blocks['file_name'] = {
                    init: function () {
                        file_block = this;
                        this.appendDummyInput()
                                .appendField("<            >");
                        this.setOutput(true, null);
                        this.setColour(120);
                        this.setTooltip('');
                        this.setHelpUrl('http://www.example.com/');
                    }
                };
                file_block = Blockly.Blocks['file_name'];
                Blockly.JavaScript['file_name'] = function () {
                    var code = '../n';
                    return code;
                }
  var parentBlock = Blockly.Block.obtain(Blockly.getMainWorkspace(), 'require_file');
                parentBlock.initSvg();
                parentBlock.render();

                var childBlock = Blockly.Block.obtain(Blockly.getMainWorkspace(), 'file_name');
                //childBlock.setFieldValue('Hello', 'TEXT');
                childBlock.initSvg();
                childBlock.render();
             console.log(childBlock);
                var parentConnection = parentBlock.getInput('Req').connection;
                var childConnection = childBlock.outputConnection;
                parentConnection.connect(childConnection);

Wolfgang Koehler

unread,
Jul 27, 2016, 7:10:32 AM7/27/16
to Blockly
Use the change event. Something along this line:

function eventHandler(event) {
  if (event.type == Blockly.Events.CHANGE) {
    if (event.name == "BlockName") {   
      var parentBlock = workspace.getBlockById(event.blockId);
      if (parentBlock.type == "type") {
        var childBlock = parentBlock.childBlocks_[n];
        childBlock.setFieldValue('whatever');

kushal bhattacharya

unread,
Jul 27, 2016, 1:18:59 PM7/27/16
to Blockly
but sir how do i trigger Blockly.Events.CHANGE  when click is done on the block click event is triggerd so sir how do we trigger the change event
Message has been deleted

kushal bhattacharya

unread,
Jul 27, 2016, 1:28:36 PM7/27/16
to Blockly
But sir i have question about my revious post how do i programmatically join  the 2 blocks  not on the workspace but  in the toolbox.The code that u have given joins the the block at runtime on the workspace.

kushal bhattacharya

unread,
Jul 27, 2016, 1:35:24 PM7/27/16
to Blockly
sir the  change event is trigggerd  during initialialisation of the block how do i trigger it again

kushal bhattacharya

unread,
Jul 27, 2016, 1:42:24 PM7/27/16
to Blockly
thanx a lot sir  the problem has been resolved

Rahul Pandit

unread,
Aug 17, 2018, 8:57:19 AM8/17/18
to Blockly
Hi kushal,
can you psst the solution to this problem you were facing.
Thanks
Reply all
Reply to author
Forward
0 new messages