2017-02-27 71 views
1

我有一個有兩個單選按鈕的廣播組字段。默認情況下選中一個單選按鈕。當我點擊另一個時,更改事件觸發器並呈現一條確認消息,指出YES或NO。如果YES被點擊,則值會改變。如果單擊「否」,則值應保持不變。ExtJS廣播組 - 如何根據用戶確認設置數值?

例如第2項是默認選擇的。如果我點擊第1項,然後點擊NO。第2項應進行檢查,但沒有項目1.

var RG = Ext.create({ 
     xtype: 'radiogroup', 
     fieldLabel: 'Two Columns', 
     // Arrange radio buttons into two columns, distributed vertically 
     columns: 2, 
     vertical: true, 
     items: [ 
      { boxLabel: 'Item 1', name: 'rb', inputValue: '1' }, 
      { boxLabel: 'Item 2', name: 'rb', inputValue: '2', checked: true} 
     ], 
     listeners:{ 
      'change':function(cmp, newValue, oldValue){ 
       Ext.Msg.show({ 
        title:'Save Changes?', 
        message: 'Would you like to save your changes?', 
        buttons: Ext.Msg.YESNO, 
        icon: Ext.Msg.QUESTION, 
        fn: function(btn) { 
         if (btn === 'yes') { 
          alert('Yes pressed'); 
         } else if (btn === 'no') { 
          alert('No pressed'); 
          //New Code 
         } 
        } 
       }); 
      } 
     } 
    }); 

Ext.create('Ext.form.Panel', { 
    title: 'RadioGroup Example', 
    width: 300, 
    height: 125, 
    bodyPadding: 10, 
    renderTo: Ext.getBody(), 
    items:[RG] 
}); 

請讓我知道如何做出正確的改變來實現這種行爲。

在此先感謝!

回答

1

首先supend變化事件然後設置與舊值的組件能重新開始變化事件,查找下面

 Ext.Msg.show({ 
       title:'Save Changes?', 
       message: 'Would you like to save your changes?', 
       buttons: Ext.Msg.YESNO, 
       icon: Ext.Msg.QUESTION, 
       fn: function(btn) { 
        if (btn === 'yes') { 
         alert('Yes pressed'); 
        } else if (btn === 'no') { 
         cmp.suspendEvent('change'); 
         cmp.setValue(oldValue); 
         cmp.resumeEvent('change'); 
        } 
       } 
      }); 
+0

cmp.reset();而不是cmp.setValue(oldValue);也有效 – josei

1

方案中的代碼,您正在尋找可以用控制器來實現。在控制器

寫這個功能給一個函數名的更改聽衆如下: 聽衆:{ 'applicantRadioChange' 變化}:

applicantRadioChange : function(cmp,newValue,oldValue){ 

if(newValue != oldValue) 
{ 
Ext.Msg.show({ 
          title:'Save Changes?', 
          message: 'Would you like to save your changes?', 
          buttons: Ext.Msg.YESNO, 
          icon: Ext.Msg.QUESTION, 
          fn: function(btn) { 
           if (btn === 'yes') { 
            alert('Yes pressed'); 
           } else if (btn === 'no') { 
            cmp.setValue(oldValue); 

           } 
          } 
         }); 

} 
+0

這有問題,如果你設置oldvalue沒有暫停變化事件它將再次觸發變化事件,並可能成爲遞歸。 –

+0

它在我的結尾沒有成爲遞歸。實際上,Ext.Msg只有在佈局未被暫停時纔有效,因此不需要。反正其他的事情是,在主視圖文件Ext.Msg.Show中,你找不到cmp,oldValue和newValue,所以你需要它在控制器中。 –

+0

set.Cmp(oldValue)是一個肯定會觸發的更改。檢查小提琴https://fiddle.sencha.com/#view/editor&fiddle/1r0t並點擊否,它會再次觸發。 –

相關問題