萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

ExtJS 組件獨立定義實戰範例:ComboBox、Grid、Toolbar、Form Panel 完整架構

概述

本文提供一份 ExtJS 經典架構範例,展示如何獨立定義並組合 ComboBox、GridPanel、Toolbar、FormPanel 等核心組件,構建典型的「查詢工具列 + 資料列表 + 編輯表單」版面。適合維護舊版 ExtJS 專案或學習組件化寫法的開發者參考。

核心組件拆解

1. 資料存儲

// 簡單陣列存儲:下拉選單分類
var categoryStore = new Ext.data.SimpleStore({
    fields: ['value', 'text'],
    data: [
        ['all', '所有種類'],
        ['1', 'Beverages'],
        ['2', 'Condiments'],
        // ...
    ]
});

// Ajax 存儲:商品主檔
var productStore = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({ url: 'Default.aspx' }),
    reader: new Ext.data.JsonReader({
        totalProperty: 'totalProperty',
        root: 'root',
        fields: ['ProductID','ProductName','CategoryName','UnitPrice','Discontinued','QuantityPerUnit','CompanyName']
    })
});

2. 表單欄位與工具列

var cbCategory = new Ext.form.ComboBox({
    id: 'cbCategory', store: categoryStore,
    displayField: 'text', valueField: 'value',
    typeAhead: true, mode: 'local', triggerAction: 'all',
    emptyText: '請選擇商品種類...', editable: false,
    allowBlank: false, blankText: '商品種類必須選擇'
});
cbCategory.setValue('all');

var tfName = new Ext.form.TextField({ id: 'tfName' });

var btnSearch = new Ext.Button({
    id: 'btnSearch', text: '搜索', iconCls: 'btn_search',
    handler: function() {
        productStore.load({
            params: {
                start: 0, limit: 10,
                categoryName: Ext.getCmp('cbCategory').getValue(),
                productName: Ext.getCmp('tfName').getValue()
            }
        });
    }
});

var tb = new Ext.Toolbar({
    id: 'tb', items: ['商品種類:', cbCategory, '-', '商品名稱:', tfName, btnSearch, '->', {text:'幫助', iconCls:'btn_help'}]
});

3. 表格與分頁

var cm = new Ext.grid.ColumnModel([
    new Ext.grid.RowNumberer(),
    {header: '產品名稱', dataIndex: 'ProductName', sortable: true},
    {header: '產品種類', dataIndex: 'CategoryName', sortable: true},
    {header: '單價', dataIndex: 'UnitPrice', sortable: true},
    {header: '是否停產', dataIndex: 'Discontinued', sortable: true},
    {header: '規格', dataIndex: 'QuantityPerUnit', sortable: true},
    {header: '供貨商', dataIndex: 'CompanyName', sortable: true}
]);

var grid = new Ext.grid.GridPanel({
    id: 'grdProduct', title: '商品信息',
    cm: cm, store: productStore,
    autoWidth: true, height: screen.availHeight - 190,
    frame: true, loadMask: true,
    bbar: new Ext.PagingToolbar({
        store: productStore, pageSize: 10,
        displayInfo: true, displayMsg: '第{0}--{1}條,共{2}條',
        emptyMsg: '沒有數據'
    }),
    viewConfig: { forceFit: true }
});

4. 編輯表單面板

var formPanel = new Ext.Panel({
    id: 'pnProduct', title: '商品信息', autoHeight: true,
    items: [
        // Row 1: 名稱、是否停售
        new Ext.Panel({ layout: 'column', border: false, items: [
            new Ext.Panel({ columnWidth: .5, layout: 'form', labelWidth: 60, items: [{xtype:'textfield', id:'ProductName', fieldLabel:'商品名稱', anchor:'95%'}] }),
            new Ext.Panel({ columnWidth: .25, layout: 'form', labelWidth: 60, items: [{xtype:'radio', name:'Discontinued', inputValue:'1', boxLabel:'是', fieldLabel:'是否停售'}] }),
            new Ext.Panel({ columnWidth: .25, layout: 'form', labelWidth: 60, items: [{xtype:'radio', name:'Discontinued', inputValue:'0', boxLabel:'否', checked:true}] })
        ]}),
        // Row 2: 規格、單價
        // Row 3: 庫存量、供貨商下拉 (連結 supplierStore)
        // ... 略
    ]
});

5. 版面配置

new Ext.Viewport({
    layout: 'border',
    items: [
        { region: 'north', items: [tb] }, // 工具列
        { region: 'center', items: [grid] }, // 主表格
        { region: 'east', width: 400, items: [formPanel] } // 右側編輯表單
    ]
});

關鍵技巧總結

  • 組件獨立建構:各組件先 new 出來,再由容器 items 組合,便於復用與維護。
  • Store 分離:資料邏輯與 UI 分離,同一 Store 可共享給 Grid、ComboBox。
  • ID 與 Ext.getCmp:透過固定 ID 跨組件存取,但大型專案建議改用 ComponentQuery 或 MVC 架構降低耦合。
  • Layout 系統:善用 column、form、border 佈局實現複雜版面。

遷移建議

ExtJS 6+ 現代工具包推薦使用 Ext.define 類定義、ViewModel/ViewController (MVVM) 架構,並搭配 Ext.grid.Panel、Ext.form.Panel 等現代組件。舊專案可逐步重構,新專務請直接採用 Modern/Classic Toolkit 新寫法。

copyright © 萬盛學電腦網 all rights reserved