這有幫助嗎?
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
上,此容器設置了現有的子面板,始終保持倒數第二個摺疊面板展開爲。您應該修改算法以適應您的需求。
旁註:在視圖容器上設置變量並不理想。這些變量應該被移入控制器。
所以,你會喜歡的東西像[手風琴(https://fiddle.sencha.com/#view/editor&fiddle/26iu),但摺疊所有項目的能力嗎? – scebotari66
@ scebotari66是的,類似的東西,但用'Ext.resizer.Splitter'。其實,我幾乎忘記了手風琴的佈局!也許我可以用某種方式將它與分離器集成... –
如果由於某種原因,這個問題似乎對您不正確,那麼請寫下原因。不要只是downvote。 –