ExtJs4 筆記之 layout 布局
本篇講解Ext另一個重要的概念:布局。一般的容器類控制項都是通過配置項items添加子控制項的,這些子控制項相對於父控制項怎麼定位呢,這裡就要用到布局。 某些容器類控制項,它本身預設就整合了一種布局方式,例如比較典型的是:Ext.container.Viewport 布局控制項,它其實就是一個border布局的容器,還有Ext.form.Panel、Ext.tab.Panel等。本節我們系統的分析各種布局方式。
一、absolute
這種方式的布局可以對子項目相對於父級容器控制項進行絕對位置,它包含了x、y兩個配置項用於定位。
我們來看看一個例子: 精心開發5年的UI前端架構!
//absoluteExt.create('Ext.Panel', { title: '容器面板', renderTo: 'div1', width: 400, height: 300, layout: 'absolute', items: [{ title: '面板1', xtype: "panel", html: "子項目1", width: 200, height: 100, x: 50, y: 50 }, { title: '面板2', xtype: "panel", html: "子項目2", width: 200, height: 100, x: 100, y: 80 }]});
效果如下:
二、accZ喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcmRpb248L2gyPgo8cD4K09C1xGpzsuW8/sDvw+ZhY2NvcmRpb262vMrH0ru49nVpv9i8/qOstavKx0V4dMrHzai5/bK8vta1xLe9yr3Ktc/WtcSjrM7Sw8e/ydLU08PD5rDlv9i8/tf3zqrL/LXE1du1/s/uo6yyosfSu7m/ydLU08Nqc8C0t622r7vutq/P7qGjIL6r0MS/qreiNcTqtcRVScewtsu/8rzco6E8L3A+CjxwcmUgY2xhc3M9"brush:java;">//accordionExt.create('Ext.Panel', { title: '容器面板', renderTo: 'div2', width: 400, height: 300, layout: 'accordion', items: [{ tools: [{ type: 'gear', handler: function () { Ext.Msg.alert('提示', '配置按鈕被點擊。'); } }, { type: 'refresh'}], title: '面板1', xtype: "panel", html: "子項目1" }, { title: '面板2', xtype: "panel", html: "子項目2" }, { id: 'panel3', title: '面板3', xtype: "panel", html: "子項目3" }]});Ext.create("Ext.Button", { renderTo: 'div2', text: "開啟第三頁", handler: function () { Ext.getCmp('panel3').expand(true); }});
效果如下:
三、anchor
這個布局就是表單面板預設支援的,每一項佔據一行,支援用anchor配置項分配各個子項的高度和寬度。為百分比時表示當前大小占父容器的百分比,為數位時一般為負數,表示父容器的值減去差值,剩下的為子項的大小。 精心開發5年的UI前端架構!
//anchorExt.create('Ext.Panel', { title: '容器面板', renderTo: 'div3', width: 400, height: 300, layout: 'anchor', items: [{ tools: [{ type: 'gear', handler: function () { Ext.Msg.alert('提示', '配置按鈕被點擊。'); } }, { type: 'refresh'}], title: '面板1', xtype: "panel", html: "子項目1", anchor: '80% 20%' }, { title: '面板2', xtype: "panel", html: "子項目2", anchor: '-50 -200' }, { title: '面板3', xtype: "panel", html: "子項目3", anchor: '100% 30%' }]});
效果如下:
四、border
這個布局可以定義東南西北四個方向的子項目,還有一個置中的子項目,一般用它來做頁面整頁布局,所以Ext.container.Viewport預設就支援了這個布局方式。 精心開發5年的UI前端架構!
//borderExt.create('Ext.Panel', { title: '容器面板', renderTo: 'div4', width: 400, height: 300, layout: 'border', defaults: { split: true, //是否有分割線 collapsible: true, //是否可以摺疊 bodyStyle: 'padding:15px' }, items: [{ region: 'north', //子項目的方位:north、west、east、center、south title: '北', xtype: "panel", html: "子項目1", height: 70 }, { region: 'west', title: '西', xtype: "panel", html: "子項目2", width: 100 }, { region: 'east', title: '東', xtype: "panel", html: "子項目2", width: 100 }, { region: 'center', title: '主體', xtype: "panel", html: "子項目3" }, { region: 'south', title: '南', xtype: "panel", html: "子項目4", height: 70 }]});
效果如下:
五、card
這個布局可以像卡片一樣的切換每個子項目,各個子項目都會獨佔父元素的容器空間。我們可以定義翻頁按鈕來控制當前處於活動狀態的子項目。 精心開發5年的UI前端架構!
//cardvar cardNav = function (incr) { var l = Ext.getCmp('cardPanel').getLayout(); var i = l.activeItem.id.split('card')[1]; var next = parseInt(i, 10) + incr; l.setActiveItem(next); Ext.getCmp('cardPrev').setDisabled(next === 0); Ext.getCmp('cardNext').setDisabled(next === 2);};Ext.create('Ext.Panel', { title: '容器面板', renderTo: 'div5', width: 400, height: 300, layout: 'card', activeItem: 1, //預設活動項 id: 'cardPanel', items: [{ id: 'card0', title: '面板1', xtype: "panel", html: "子項目1" }, { id: 'card1', title: '面板2', xtype: "panel", html: "子項目2" }, { id: 'card2', title: '面板3', xtype: "panel", html: "子項目3" }], bbar: ['->', { id: 'cardPrev', text: '« 前一頁', handler: Ext.Function.bind(cardNav, this, [-1]) }, { id: 'cardNext', text: '後一頁 »', handler: Ext.Function.bind(cardNav, this, [1]) }] });
效果如下:
六、column
這個布局把子項目按照列進行劃分。 精心開發5年的UI前端架構!
//columnExt.create('Ext.Panel', { title: '容器面板', renderTo: 'div6', width: 400, height: 300, layout: 'column', defaults: { //設定沒一列的子項目的預設配置 layout: 'anchor', defaults: { anchor: '100%' } }, items: [{ columnWidth: 4 / 10, //設定列的寬度 items: [{ title: '面板1', border: false, html: '子項目1' }, { title: '面板2', border: false, html: '子項目2' }] }, { width: 120, items: [{ title: '面板3', border: false, html: '子項目3' }] }, { columnWidth: .40, items: [{ title: '面板4', border: false, html: '子項目4' }] }] });
效果如下:
七、fit
這個布局下子項目會獨佔全部的容器空間,一般用於只有一個子項的情況。
//fitExt.create('Ext.Panel', { title: '容器面板', renderTo: 'div7', width: 400, height: 300, layout: 'fit', items: [{ title: '面板', html: '子項目', border: false }]});
效果如下:精心開發5年的UI前端架構!
八、table
這個布局用表格定位的方式去組織子項目,我們可以像表格一樣設定rowspan和colspan。
//tableExt.create('Ext.Panel', { title: '容器面板', renderTo: 'div8', width: 400, height: 300, layout: { type: 'table', columns: 4 }, defaults: { frame: true, width: 70, height: 50 }, items: [ { html: '元素1', rowspan: 3, height: 150 }, { html: '元素2', rowspan: 2, height: 100 }, { html: '元素3' }, { html: '元素4' }, { html: '元素5', colspan: 2, width: 140 }, { html: '元素6' }, { html: '元素7' }, { html: '元素8' } ]});
效果如下:
九、vbox
這個布局把所有的子項目按照縱向排成一列。 精心開發5年的UI前端架構!
//vboxExt.create('Ext.Panel', { title: '容器面板', renderTo: 'div9', width: 400, height: 300, layout: { type: 'vbox', pack: 'start', //縱向對齊 start:從頂部;center:從中部;end:從底部 align: 'stretchmax' //對齊 center、left、right:置中、靠左對齊、靠右對齊;stretch:延伸;stretchmax:以最大的元素為標準延伸 }, defaults: { xtype: 'button' }, items: [{ text: '小按鈕', flex: 1 //表示當前子項目尺寸所佔的均分的份數。 }, { xtype: 'tbspacer', //插入的空填充 flex: 3 }, { text: '中按鈕', scale: 'medium' }, { text: '大按鈕', width: 120, scale: 'large', flex: 1 }]});
效果如下:
十、hbox
跟vbox類似,只不過變成了橫向的。
//hboxExt.create('Ext.Panel', { title: '容器面板', renderTo: 'div10', width: 400, height: 300, layout: { type: 'hbox', pack: 'end', align: 'middle' //對齊 top、middle、bottom:頂對齊、置中、底對齊;stretch:延伸;stretchmax:以最大的元素為標準延伸 }, defaults: { xtype: 'button' }, items: [{ text: '小按鈕' },{ text: '中按鈕', scale: 'medium' }, { text: '大按鈕', width: 120, scale: 'large' }]});
效果如下:精心開發5年的UI前端架構!