如何根据下拉值的变化隐藏字段。
我添加了一个名为“A”的输入字段。我有个降落场。如果我在下拉列表中选择一个值,比如“移除字段A”,那么输入字段应该被删除。
我试过removeField。但这不起作用。还有其他方法吗?或者如何正确使用移除字段?
this.appendDummyInput()
.setAlign(Blockly.ALIGN_RIGHT)
.appendField('Type ')
.appendField(new Blockly.FieldDropdown(typeOptions), 'columnType');
// if columnType = Card, show the following:
this.appendDummyInput()
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(' Card: ')
.appendField(new Blockly.FieldDropdown(cardsList), 'cardValue');
// if columnType = view, show the following:
this.appendDummyInput()
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(' View ')
.appendField(new Blockly.FieldDropdown(viewsList), 'viewValue');发布于 2018-08-06 13:23:24
好吧,这没有你的完整代码,但我想我看到你的问题了。
简单地说,在您提供的代码中,在选择块中的新值时,我没有看到您在回调函数中执行任何操作,也没有看到您从XML中保存它/从XML中读取它。这其中有一些可能被忽略了,但为了不让您在注释中使用“包含更多代码”标记,我将在这里做一个简单的分析。
让我向您展示一些示例代码,并介绍一下我所做的一切,以便使这个案例工作起来:
Blockly.Blocks['mySampleBlock'] = {
/**
* Initiate the block. This runs before domToMutation.
*/
init: function () {
var typeOptions = [['Card', 'card'], ['View', 'view']];
this.appendDummyInput()
.setAlign(Blockly.ALIGN_RIGHT)
.appendField('Type ')
.appendField(new Blockly.FieldDropdown(typeOptions, this.handleTypeSelection.bind(this)), 'typeSelector');
// Initialize the value of this.columnType (used in updateShape)
this.columnType = this.getFieldValue('typeSelector');
// Avoid duplicating code by running updateShape to append your appropriate input
this.updateShape();
//@TODO: Do other block configuration stuff like colors, additional inputs, etc. here
},
/**
* This function runs each time you select a new value in your type selection dropdown field.
* @param {string} newType This is the new value that the field will be set to.
*
* Important note: this function will run BEFORE the field's value is updated. This means that if you call
* this.getFieldValue('typeSelector') within here, it will reflect the OLD value.
*
*/
handleTypeSelection: function (newType) {
// Avoid unnecessary updates if someone clicks the same field twice
if(this.columnType !== newType) {
// Update this.columnType to the new value
this.columnType = newType;
// Add or remove fields as appropriate
this.updateShape();
}
},
/**
* This will remove old inputs and add new inputs as you need, based on the columnType value selected
*/
updateShape: function () {
// Remove the old input (so that you don't have inputs stack repeatedly)
if (this.getInput('appendToMe')) {
this.removeInput('appendToMe');
}
// Append the new input based on the value of this.columnType
if(this.columnType === 'card') {
// if columnType = Card, show the following:
//@TODO: define values in cardsList here
var cardsList = [['Dummy Option','option']];
this.appendDummyInput('appendToMe')
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(' Card: ')
.appendField(new Blockly.FieldDropdown(cardsList), 'cardValue');
} else if (this.columnType === 'view') {
// if columnType = view, show the following:
//@TODO: define values in viewsList here
var viewsList = [['Dummy Option','option']];
this.appendDummyInput()
.setAlign(Blockly.ALIGN_RIGHT)
.appendField(' View ')
.appendField(new Blockly.FieldDropdown(viewsList), 'viewValue');
}
},
/**
* This function runs when saving your block to XML. This is important if you need to save your block to XML at any point and then either
* generate code from that XML or repopulate your workspace from that XML
*/
mutationToDom: function () {
var container = document.createElement('mutation');
// Do not use camelCase values for attribute names.
container.setAttribute('column_type', this.columnType);
// ALWAYS return container; this will be the input for domToMutation.
return container;
},
/**
* This function runs when loading your block from XML, after running init.
* It's very important for updating your block in response to values selected in a field.
*/
domToMutation: function (xmlElement) {
// This attribute should match the one you used in mutationToDom
var columnType = xmlElement.getAttribute('column_type');
// If, for whatever reason, you try to save an undefined value in column_type, it will actually be saved as the string 'undefined'
// If this is not an acceptable value, filter it out
if(columnType && columnType !== 'undefined') {
this.columnType = columnType;
}
// Run updateShape to append block values as needed
this.updateShape();
}
};关于这种情况,除了我的解释性评论外,还有几点要注意:
this.columnType结构。相反,您可以将一个columnType值传递给updateShape,并使用this.getFieldValue('typeSelector')或您的‘回调’函数(handleTypeSelection)的输入。我倾向于使用这种方法,因为我经常会创建更复杂的块,在这些块中,每次获得适当的值都很困难或效率低下,而且this.whateverMyValueNameIs更容易实现。this.removeInput和appendField代替updateShape中的removeField和this.appendDummyInput,这是您的第一本能。但是,如果您这样做,则需要确保您已经命名了要追加字段的输入,以便将其从其中删除。在大多数情况下,我倾向于只添加/删除整个输入,因为它还允许我更改标签等等。domToMutation和mutationToDom,以便将该值保存到突变属性中,然后读取它并相应地更新块。即使您的块上没有实际的变形器,这也是适用的。这可能有点令人困惑,所以如果你有后续问题,请询问。我花了一段时间才掌握了窍门,我自己。(不过,如果我不清楚您想做什么,我可能会要求您提供更多的代码示例。)
祝好运!
https://stackoverflow.com/questions/51667300
复制相似问题