2017-09-13 60 views
-1

我有一個有兩個面板的容器作爲它的項目。我希望兩個面板都是可摺疊的(同時沒有一個面板或只有一個面板可以摺疊),如果有任何面板摺疊,其他面板應占用所有剩餘空間。另外我想能夠調整這兩個面板與Ext.resizer.SplitterExtJS 4兩個帶分配器的可摺疊面板

我已經嘗試(h/v)框/邊框佈局的不同組合,但它們都不能正常工作。

看起來好像Ext.layout.container.Accordion是我需要的,但是,正如我所看到的,它可以與Ext.resizer.Splitter一起使用。

Check this fiddle

另外,我希望能夠與 Ext.resizer.Splitter坍塌兩個面板,但我可以看到它的不可開箱,我要覆蓋它。我對嗎?

我使用的是ExtJS 4.2.1版。

+1

所以,你會喜歡的東西像[手風琴(https://fiddle.sencha.com/#view/editor&fiddle/26iu),但摺疊所有項目的能力嗎? – scebotari66

+0

@ scebotari66是的,類似的東西,但用'Ext.resizer.Splitter'。其實,我幾乎忘記了手風琴的佈局!也許我可以用某種方式將它與分離器集成... –

+0

如果由於某種原因,這個問題似乎對您不正確,那麼請寫下原因。不要只是downvote。 –

回答

1

這有幫助嗎?

Ext.application({ 
name: 'Fiddle', 

launch: function() { 

    Ext.create('Ext.Container', { 
     height: 500, 
     renderTo: Ext.getBody(), 
     width: 500, 
     layout: { 
      type: 'vbox', 
      align: 'stretch' 
     }, 

     items: [{ 
      xtype: 'panel', 
      reference: 'panel1', 
      title: 'Top panel', 
      collapsible: true, // To allow collapse 
      flex: 1, 
      bodyStyle: 'background: #dadada', 
      listeners: { 
       collapse: function(){ 
        this.up().down("[reference='panel2']").expand(); 
       } 
      } 
     }, 
     { 
      xtype: 'splitter' 
     }, 
     { 
      xtype: 'panel', 
      reference: 'panel2', 
      title: 'Bottom panel', 
      collapsible: true, // To allow collapse 
      flex: 1, 
      bodyStyle: 'background: #999', 
      listeners: { 
       collapse: function(){ 
        this.up().down("[reference='panel1']").expand(); 
       } 
      } 
     }] 

    }); 
}}); 

這裏vbox用於豎置面板(該flex屬性告訴他們多少空間相互承擔)。 collapsible屬性設置爲true使它們可摺疊。然後每個面板都有一個事件,當目標面板崩潰時擴展兄弟面板。有了這個,至少有一個面板總是展開,你可以用分離器調整它們的大小!


編輯1

前面的代碼示例是不通用的,沒有按預期的反應,如果我們設置面板與animCollapse: false。一個通用的解決辦法如下所示:

// Our extended container 
Ext.define('MyVboxContainer', { 
    extend: 'Ext.Container', 

    layout: { 
     type: 'vbox', 
     align: 'stretch' 
    }, 

    // We do the setup on initiating the component 
    initComponent: function() { 
     var me = this; 
     var panelCount = 0; 

     // For each child component 
     this.items.forEach(function (comp) { 

      // If the component is a panel 
      if (comp.xtype === 'panel') { 

       // We add an unique ref 
       comp.reference = 'panel' + panelCount; 

       // Increment the total number of panels 
       panelCount++ 

       // And listeners for beforecollapse, collapse and expand 
       comp.listeners = { 
        // On collpase, we track the last collapsed panel 
        'collapse': function() { 
         me.closedCount++; 

         me.lastClosed = this.reference; 
        }, 

        // On expand we decrement the total number of panels collapsed 
        'expand': function() { 
         me.closedCount--; 
        }, 

        // If this is the last panel being collapsed, 
        // we expand the previous collapsed panel 
        // Note: this cannot be done on the expand event 
        // if the panel has animCollapse: false 
        'beforecollapse': function() { 
         if (me.closedCount + 1 == me.totalPanels) { 
          me.down("[reference='" + me.lastClosed + "']").expand(); 
         } 
        } 
       }; 
      } 
     }); 

     this.totalPanels = panelCount; // total number of panels 
     this.lastClosed = null; // Last collapsed panel 
     this.closedCount = 0; // How many panels we have closed 

     console.log("Total panels are: " + this.totalPanels) 

     this.callParent(); 
    } 
}); 

Ext.application({ 
    name: 'Fiddle', 

    launch: function() { 

     Ext.create('MyVboxContainer', { 
      height: 500, 
      width: 500, 
      renderTo: Ext.getBody(), 

      items: [{ 
       xtype: 'panel', 
       animCollapse: false, 
       title: 'Top panel', 
       collapsible: true, // To allow collapse 
       flex: 1, 
       bodyStyle: 'background: #dadada' 
      }, { 
       xtype: 'splitter' 
      }, { 
       xtype: 'panel', 
       title: 'Middle panel', 
       animCollapse: false, 
       collapsible: true, // To allow collapse 
       flex: 1, 
       bodyStyle: 'background: #999' 
      }, { 
       xtype: 'splitter' 
      }, { 
       xtype: 'panel', 
       title: 'Bottom panel', 
       animCollapse: false, 
       collapsible: true, // To allow collapse 
       flex: 1, 
       bodyStyle: 'background: #999' 
      }] 
     }); 
    } 
}); 

在這裏,我們創建一個使用vbox佈局垂直設置面板擴展容器。在initComponent上,此容器設置了現有的子面板,始終保持倒數第二個摺疊面板展開爲。您應該修改算法以適應您的需求。

旁註:在視圖容器上設置變量並不理想。這些變量應該被移入控制器。

+0

首先,謝謝你的回答!這個決定通常適合我,但我一直在尋找一種更通用的解決方案,我會等到賞金期結束,如果沒有出現更合適的解決方案,我會獎勵你。還有一點評論 - 如果其中一個面板崩潰了,我會嘗試摺疊第二個面板。 –

+0

我在測試樣品時沒有重現任何錯誤。你能告訴我哪個是錯誤嗎?如果你想要一個通用的解決方案,你應該使用一個控制器來監聽面板的崩潰/展開事件,並跟蹤那些被關閉的事件。這樣,當摺疊最後一個打開的面板時,您可以隨時通過手動調用'panel.expand()'來強制執行至少一個面板!我相信你可以用自定義插件實現同樣的解決方案。 – NAmorim

+0

你可以在這裏查看錯誤 - https://fiddle.sencha.com/#view/editor&fiddle/2758。只需摺疊第一個面板,然後摺疊第二個面板。 –

1

我寫了基於Ext.resizer.Splitter代碼的組件。它允許用單個分離器摺疊兩個面板。

解決方法是原始的,你可以在this fiddle檢查它。

0

在ExtJs有spliter

您可以在您的容器內使用。在這裏我創建了一個可摺疊面板的演示。

希望它能幫助你解決你的問題。 Sencha Fiddle

Ext.create('Ext.container.Container', { 
    height: 300, 
    layout: { 
     type: 'vbox', 
     align: 'stretch' 
    }, 
    width: 400, 
    renderTo: Ext.getBody(), 
    border: 1, 
    items: [{ 
     xtype: 'panel', 
     collapsible: true, 
     title: 'First Panel', 
     flex: 1 
    }, { 
     xtype: 'splitter', 
     height: 20 
    }, { 
     xtype: 'panel', 
     collapsible: true, 
     flex: 1, 
     maintainFlex: true, 
     title: 'Second Panel' 
    }] 
}); 
+0

Myabe我錯過了一些東西,但你的回答看起來像是我接受的答案的重複,但忽略了我的問題的這一部分「同時沒有人或只有一個面板可以被摺疊」。 –

+0

因此,您可以在時間放置條件,在摺疊事件之前只有一個面板可以摺疊。在崩潰事件之前,您可以檢查是否已經崩潰而不是返回false。所以事件不會執行。 –

+0

是的,這個soltuion已經在接受的答案中描述過了。你讀過了嗎? –