To get the Dropdown field value

332 views
Skip to first unread message

nishanth mathialagan

unread,
Feb 7, 2022, 3:39:59 AM2/7/22
to Blockly
Hello,
Screenshot (314).png
.appendField("Form selection")
.appendField(  new Blockly.FieldDropdown(forms),'formtitle')
 .appendField("FieldID")
.appendField(  new Blockly.FieldDropdown(option), 'fieldId')

The above one is my working blockly code and scrrenshot.I need to get the value form form selection depon upon i will get field values from that selection and display the options available in FieldID
.For that if i write function on fieldID like function(){...},How can i get the value from Form Selection in this whenever change.
Thank you



nishanth mathialagan

unread,
Feb 7, 2022, 3:45:52 AM2/7/22
to Blockly
like in Form selection if select Form1  =>get options from localstorage("form1") contains(["firstname","firstname"],...)option need to display in fieldID.If i select Form2=> get options from localstorage("form2") contains(["some","some"],...)option need to display in fieldID like dynmaic.for that how can i get select value from form selection to fieldID

Beka Westberg

unread,
Feb 7, 2022, 10:56:34 AM2/7/22
to blo...@googlegroups.com
Hello!

So it seems like you want to create a dynamic dropdown which depends on the value of another dropdown.

Firstly, to get the value of the first dropdown you can do `block.getFieldValue('formtitle')`.

Then you can use this to create your dynamic dropdown generator. For example, you could do something like:

```
Blockly.Blocks['my_block_type'] = {
  init: function() {
    this.appendDummyInpot()
.appendField("Form selection")
.appendField(  new Blockly.FieldDropdown(forms),'formtitle')
 .appendField("FieldID")
.appendField(  new Blockly.FieldDropdown(this.generate), 'fieldId')
  },

  // `this` is the 'fieldId' dropdown field
  generate: function() {
     const block = this.getSourceBlock();
     const form = block.getFieldValue('formtitle');
     const options = getOptionsFromForm(form); // some function to get the options.
     return options;
  }
}
```

I hope that helps! If you have any further questions please reply!
--Beka

--
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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/b8c6fd70-61ce-49d5-9a9a-eb93fcc8153cn%40googlegroups.com.

nishanth mathialagan

unread,
Feb 7, 2022, 11:26:51 PM2/7/22
to Blockly
Hi Beka ,
  It throwing error in getFieldValue..
Capture.JPG
But its not empty or Null.
Check my code
 init: function() {
 this.appendDummyInput()
    .appendField("Form selection")
    .appendField(new Blockly.FieldDropdown(this.newgenerate),'formtitle')
    .appendField("FieldID")
    .appendField(  new Blockly.FieldDropdown(this.generate), 'fieldId')
},
 newgenerate:function(){
  var titleoptions=[]
  var forms=[]
  var title=localStorage.getItem("title1")
  titleoptions=JSON.parse(title)
  for(const x in titleoptions){
    forms[x]=[titleoptions[x],titleoptions[x]]
  }
  return forms
 },
generate:function() {
    const block = this.getSourceBlock();
    const form = block.getFieldValue('formtitle');
    var option=[];
    var field=localStorage.getItem(form)
    var fieldId=[]
    fieldId=JSON.parse(field)
    for(const x in fieldId){
      option[x]=[fieldId[x],fieldId[x]]
    }
    return option;
 },
};
Thank you

Jason Schanker

unread,
Feb 8, 2022, 3:13:02 AM2/8/22
to Blockly
Hi,

this.getSourceBlock() will be null the first time before the field is mounted on the block.  I didn't get a chance to go through your code at the moment, but if you're working with dependent dropdowns, a previous discussion here might be useful: https://groups.google.com/g/blockly/c/uilMLN_VojA/m/sKd6LjNNAwAJ .

Hope this helps.

Best,
Jason

nishanth mathialagan

unread,
Feb 11, 2022, 7:07:01 AM2/11/22
to Blockly
Thank you Beka and Jason. Jason that discussion helped me to solve my problem.Thank you
Reply all
Reply to author
Forward
0 new messages