2016-10-20 82 views

回答

1

是的。使用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}' 
    } 
} 
+0

謝謝羅伯特。很棒。我編輯了我的小提琴寬度您的解決方案。 – josei

1

有可能是一個更好的解決方案,但你可以添加屬性到你的店裏來確定要隱藏或沒有,那麼綁定到該屬性:

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() 
}); 
+0

感謝michwalk。這是一個有趣的解決方案和起點。但是,此解決方案不會根據在組合框中選擇的值動態地隱藏字段:也就是說,何時被選中,阿拉巴馬州計劃隱藏相應字段(或多個字段)並顯示其他字段;當我選擇阿拉斯加值意圖隱藏相應的字段(或多於一個)並顯示其他字段時, – josei

+0

儘管對於不同的情況,下面給出的對帖子問題的回答是使用的公式,可能是這種情況的一個可能的解決方案。 http://stackoverflow.com/questions/30065978/extjs-5-1-binding-record-value-to-component-property?rq=1 – josei