Stay organized with collections
Save and categorize content based on your preferences.
blockly > FieldVariable
FieldVariable class
Class for a variable's dropdown field.
Signature:
export declare class FieldVariable extends FieldDropdown
Extends: FieldDropdown
Constructors
Properties
Property |
Modifiers |
Type |
Description |
defaultVariableName |
|
string |
|
menuGenerator_ |
protected |
MenuGenerator | undefined |
|
SERIALIZABLE |
|
boolean |
Serializable fields are saved by the serializer, non-serializable fields are not. Editable fields should also be serializable. |
variableTypes |
|
string[] | null |
All of the types of variables that will be available in this field's dropdown. |
Methods
Method |
Modifiers |
Description |
configure_(config) |
protected |
Configure the field based on the given map of options. |
doClassValidation_(newValue) |
protected |
Ensure that the ID belongs to a valid variable of an allowed type. |
doValueUpdate_(newId) |
protected |
Update the value of this variable field, as well as its variable and text. The variable ID should be valid at this point, but if a variable field validator returns a bad ID, this could break. |
dropdownCreate(this) |
static |
Return a sorted list of variable names for variable dropdown menus. Include a special option at the end for creating a new variable name. |
fromXml(fieldElement) |
|
Initialize this field based on the given XML. |
getDefaultType() |
protected |
Gets the type of this field's default variable. |
getText() |
|
Get the text from this field, which is the selected variable's name. |
getValidator() |
|
Gets the validation function for this field, or null if not set. Returns null if the variable is not set, because validators should not run on the initial setValue call, because the field won't be attached to a block and workspace at that point. |
getValue() |
|
Get the variable's ID. |
initModel() |
|
Initialize the model for this field if it has not already been initialized. If the value has not been set to a variable by the first render, we make up a variable rather than let the value be invalid. |
initView() |
|
|
onItemSelected_(menu, menuItem) |
protected |
Handle the selection of an item in the variable dropdown menu. Special case the 'Rename variable...' and 'Delete variable...' options. In the rename case, prompt the user for a new name. |
setSourceBlock(block) |
|
Attach this field to a block. |
shouldAddBorderRect_() |
|
|
toXml(fieldElement) |
|
Serialize this field to XML. |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-05-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-05-16 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eFieldVariable\u003c/code\u003e class in Blockly is used to create dropdown fields for selecting variables within a Blockly workspace.\u003c/p\u003e\n"],["\u003cp\u003eIt extends the \u003ccode\u003eFieldDropdown\u003c/code\u003e class and provides functionality to manage variable selection, creation, and renaming.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eFieldVariable\u003c/code\u003e allows developers to specify the types of variables allowed in the dropdown and provides validation for user input.\u003c/p\u003e\n"],["\u003cp\u003eThis class handles the display and interaction of the dropdown menu, including special options for renaming and deleting variables.\u003c/p\u003e\n"],["\u003cp\u003eThe class also supports serialization to XML for saving and loading workspaces.\u003c/p\u003e\n"]]],["The `FieldVariable` class, extending `FieldDropdown`, manages a dropdown for selecting variables in Blockly. Key actions include constructing a new instance, setting allowed variable types, and managing a menu generator. It validates variable IDs, updates values, and handles dropdown item selection, including renaming and deleting variables. Other core functions include getting and setting the variable's ID and name, initializing the variable model, configuring the field based on options, and serializing/deserializing to/from XML.\n"],null,["[blockly](./blockly.md) \\\u003e [FieldVariable](./blockly.fieldvariable_class.md)\n\nFieldVariable class\n-------------------\n\nClass for a variable's dropdown field.\n\n**Signature:** \n\n export declare class FieldVariable extends FieldDropdown \n\n**Extends:** [FieldDropdown](./blockly.fielddropdown_class.md)\n\nConstructors\n------------\n\n| Constructor | Modifiers | Description |\n|---------------------------------------------------------------------------------------------------------------------------------------|-----------|--------------------------------------------------------|\n| [(constructor)(varName, validator, variableTypes, defaultType, config)](./blockly.fieldvariable_class._constructor__1_constructor.md) | | Constructs a new instance of the `FieldVariable` class |\n\nProperties\n----------\n\n| Property | Modifiers | Type | Description |\n|--------------------------------------------------------------------------------------|-------------|--------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|\n| [defaultVariableName](./blockly.fieldvariable_class.defaultvariablename_property.md) | | string | |\n| [menuGenerator_](./blockly.fieldvariable_class.menugenerator__property.md) | `protected` | [MenuGenerator](./blockly.menugenerator_typealias.md) \\| undefined | |\n| [SERIALIZABLE](./blockly.fieldvariable_class.serializable_property.md) | | boolean | Serializable fields are saved by the serializer, non-serializable fields are not. Editable fields should also be serializable. |\n| [variableTypes](./blockly.fieldvariable_class.variabletypes_property.md) | | string\\[\\] \\| null | All of the types of variables that will be available in this field's dropdown. |\n\nMethods\n-------\n\n| Method | Modifiers | Description |\n|----------------------------------------------------------------------------------------------|-------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [configure_(config)](./blockly.fieldvariable_class.configure__1_method.md) | `protected` | Configure the field based on the given map of options. |\n| [doClassValidation_(newValue)](./blockly.fieldvariable_class.doclassvalidation__1_method.md) | `protected` | Ensure that the ID belongs to a valid variable of an allowed type. |\n| [doValueUpdate_(newId)](./blockly.fieldvariable_class.dovalueupdate__1_method.md) | `protected` | Update the value of this variable field, as well as its variable and text. The variable ID should be valid at this point, but if a variable field validator returns a bad ID, this could break. |\n| [dropdownCreate(this)](./blockly.fieldvariable_class.dropdowncreate_1_method.md) | `static` | Return a sorted list of variable names for variable dropdown menus. Include a special option at the end for creating a new variable name. |\n| [fromXml(fieldElement)](./blockly.fieldvariable_class.fromxml_1_method.md) | | Initialize this field based on the given XML. |\n| [getDefaultType()](./blockly.fieldvariable_class.getdefaulttype_1_method.md) | `protected` | Gets the type of this field's default variable. |\n| [getText()](./blockly.fieldvariable_class.gettext_1_method.md) | | Get the text from this field, which is the selected variable's name. |\n| [getValidator()](./blockly.fieldvariable_class.getvalidator_1_method.md) | | Gets the validation function for this field, or null if not set. Returns null if the variable is not set, because validators should not run on the initial setValue call, because the field won't be attached to a block and workspace at that point. |\n| [getValue()](./blockly.fieldvariable_class.getvalue_1_method.md) | | Get the variable's ID. |\n| [initModel()](./blockly.fieldvariable_class.initmodel_1_method.md) | | Initialize the model for this field if it has not already been initialized. If the value has not been set to a variable by the first render, we make up a variable rather than let the value be invalid. |\n| [initView()](./blockly.fieldvariable_class.initview_1_method.md) | | |\n| [onItemSelected_(menu, menuItem)](./blockly.fieldvariable_class.onitemselected__1_method.md) | `protected` | Handle the selection of an item in the variable dropdown menu. Special case the 'Rename variable...' and 'Delete variable...' options. In the rename case, prompt the user for a new name. |\n| [setSourceBlock(block)](./blockly.fieldvariable_class.setsourceblock_1_method.md) | | Attach this field to a block. |\n| [shouldAddBorderRect_()](./blockly.fieldvariable_class.shouldaddborderrect__1_method.md) | | |\n| [toXml(fieldElement)](./blockly.fieldvariable_class.toxml_1_method.md) | | Serialize this field to XML. |"]]