我打算根據在組合框中選擇的值更改表單(隱藏)中幾個字段的狀態。有條件地綁定組件狀態
這可以使用諸如setVisible()或setHidden()之類的方法來完成。
使用綁定組件狀態可以實現這個目標嗎?
解決
小提琴https://fiddle.sencha.com/#fiddle/1itf
我打算根據在組合框中選擇的值更改表單(隱藏)中幾個字段的狀態。有條件地綁定組件狀態
這可以使用諸如setVisible()或setHidden()之類的方法來完成。
使用綁定組件狀態可以實現這個目標嗎?
解決
小提琴https://fiddle.sencha.com/#fiddle/1itf
是的。使用ViewModel formulas。從文檔引用:
許多配置您想要綁定的是布爾值,如可見(或隱藏),禁用,檢查和按下。綁定模板支持模板中的「inline」布爾否定。其他形式的代數被降級爲公式(見下文),但是布爾反轉是很常見的,對此有特殊的規定。
基本上,您可以使用綁定來控制可見屬性,但綁定需要是布爾值。您可以通過您的'isAdmin'檢查來查看。所以你需要做的是在ViewModel上創建一個公式並綁定到該公式上。
Ext.define('My.ViewModel', {
extend: 'Ext.app.ViewModel',
formulas: {
isAlabama: function(get) {
return get('comboboxvalue') === 'AL';
}
}
}
要使用此功能,您需要說明您在面板中使用此視圖模型。另外...你看到comboboxvalue
位?好吧,它看起來像ComboBoxes不會自動發佈他們的價值屬性視圖模型 - 你需要明確這樣做,如下所示:
{ xtype: 'combobox',
fieldLabel: 'Choose State',
store: states,
queryMode: 'local',
displayField: 'name',
valueField: 'abbr',
reference:'combobox',
bind: {
value: '{comboboxvalue}'
}
}
有可能是一個更好的解決方案,但你可以添加屬性到你的店裏來確定要隱藏或沒有,那麼綁定到該屬性:
Ext.application({
name : 'Fiddle',
launch : function() {
}
});
var states = Ext.create('Ext.data.Store', {
fields: ['abbr', 'name'],
data : [
{"abbr":"AL", "name":"Alabama", "hide": 0},
{"abbr":"AK", "name":"Alaska", "hide": 1},
{"abbr":"AZ", "name":"Arizona", "hide": 1}
]
});
Ext.create('Ext.form.Panel', {
title: 'Sign Up Form',
width: 300,
height: 230,
bodyPadding: 10,
margin: 10,
layout: {
type:'anchor',
align: 'stretch'
},
viewModel: true,
items: [{
xtype: 'checkbox',
boxLabel: 'Is Admin',
reference: 'isAdmin'
},{
xtype: 'textfield',
fieldLabel: 'Admin Key',
bind: {
visible: '{!isAdmin.checked}'
}
},{
xtype : 'menuseparator',
width : '100%'
},{
xtype: 'combobox',
fieldLabel: 'Choose State',
store: states,
queryMode: 'local',
displayField: 'name',
valueField: 'abbr',
reference:'combobox'
},{
xtype: 'textfield',
fieldLabel: 'If Alabama, hide',
bind: {
visible: '{combobox.selection.hide}'
}
},{
xtype: 'textfield',
fieldLabel: 'If Alaska, hide',
bind: {
visible: '{}'
}
},{
xtype: 'textfield',
fieldLabel: 'If Arizona, hide',
bind: {
visible: '{}'
}
}],
renderTo: Ext.getBody()
});
感謝michwalk。這是一個有趣的解決方案和起點。但是,此解決方案不會根據在組合框中選擇的值動態地隱藏字段:也就是說,何時被選中,阿拉巴馬州計劃隱藏相應字段(或多個字段)並顯示其他字段;當我選擇阿拉斯加值意圖隱藏相應的字段(或多於一個)並顯示其他字段時, – josei
儘管對於不同的情況,下面給出的對帖子問題的回答是使用的公式,可能是這種情況的一個可能的解決方案。 http://stackoverflow.com/questions/30065978/extjs-5-1-binding-record-value-to-component-property?rq=1 – josei
謝謝羅伯特。很棒。我編輯了我的小提琴寬度您的解決方案。 – josei