ExtJS 元件組合採用 Container Layout。Window 繼承自 Panel,可透過 items 配置嵌入任意元件(Form、Grid、TabPanel 等)。
items 中以陣列或 layout 包含兩者。Ext.onReady(function() {
// 1. Form Panel
var searchForm = Ext.create('Ext.form.Panel', {
title: '查詢條件',
bodyPadding: 10,
layout: 'anchor',
defaults: { anchor: '100%' },
items: [{
xtype: 'textfield',
fieldLabel: '關鍵字',
name: 'keyword'
}, {
xtype: 'datefield',
fieldLabel: '日期範圍',
name: 'startDate'
}],
buttons: [{
text: '查詢',
handler: function(btn) {
var form = btn.up('form').getForm();
if (form.isValid()) {
var grid = btn.up('window').down('grid');
grid.getStore().load({ params: form.getValues() });
}
}
}]
});
// 2. Grid Panel
var resultGrid = Ext.create('Ext.grid.Panel', {
title: '結果列表',
store: Ext.create('Ext.data.Store', {
fields: ['id', 'name', 'email', 'created'],
proxy: {
type: 'ajax',
url: '/api/users',
reader: { type: 'json', rootProperty: 'data' }
},
autoLoad: true
}),
columns: [
{ text: 'ID', dataIndex: 'id', width: 60 },
{ text: '姓名', dataIndex: 'name', flex: 1 },
{ text: 'Email', dataIndex: 'email', flex: 1 },
{ text: '建立時間', dataIndex: 'created', width: 140 }
],
flex: 1 // 佔據剩餘垂直空間
});
// 3. Window 包含 Form + Grid(垂直排列)
var win = Ext.create('Ext.window.Window', {
title: '用戶查詢',
width: 700,
height: 500,
layout: 'vbox', // 垂直盒子佈局
modal: true,
closeAction: 'hide',
items: [searchForm, resultGrid] // Form 在上,Grid 在下
});
// 顯示窗口
Ext.getCmp('showWinBtn').on('click', function() { win.show(); });
});
| 配置 | 作用 |
|---|---|
| layout: 'vbox' | 子元件垂直排列(Form 在上,Grid 在下) |
| flex: 1 (Grid) | Grid 佔據剩餘高度,Form 保持自適應高度 |
| closeAction: 'hide' | 關閉時隱藏而非銷毀,再次 show 可復用 |
| modal: true | 模態窗口,遮罩背景 |
ExtJS 安裝包 examples/form/form-grid.html 即為 Form + Grid 組合示範,可直接複製修改。