萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

Ext JS 4 TreeGrid 實戰心得:資料載入、欄位渲染、常見坑點

環境準備

Ext JS 4 TreeGrid 為 Ext.tree.Panel 搭配 Ext.data.TreeStore,需引入:

<link rel="stylesheet" href="extjs/resources/css/ext-all.css">
<script src="extjs/ext-all.js"></script>

關鍵修正:renderTo 必須指定容器 ID

官方範例常寫 renderTo: Ext.getBody(),實務上請指定具體容器 div ID:

<div id="tree-example"></div>

<script>
Ext.onReady(function() {
    Ext.define('Task', {
        extend: 'Ext.data.Model',
        fields: ['task', 'user', 'duration']
    });

    var store = Ext.create('Ext.data.TreeStore', {
        model: 'Task',
        proxy: {
            type: 'ajax',
            url: 'treegrid.json' // 回傳陣列,每項含 children 陣列
        },
        folderSort: true
    });

    var tree = Ext.create('Ext.tree.Panel', {
        title: 'Core Team Projects',
        width: 600,
        height: 400,
        renderTo: 'tree-example', // ★ 關鍵:指定容器 ID
        collapsible: true,
        useArrows: true,
        rootVisible: false,
        store: store,
        multiSelect: true,
        singleExpand: true,
        columns: [{
            xtype: 'treecolumn', // 樹狀欄位
            text: 'Task',
            flex: 2,
            sortable: true,
            dataIndex: 'task'
        },{
            xtype: 'templatecolumn',
            text: 'Duration',
            flex: 1,
            sortable: true,
            dataIndex: 'duration',
            align: 'center',
            tpl: Ext.create('Ext.XTemplate', '{duration:this.formatHours}', {
                formatHours: function(v) {
                    if (v < 1) return Math.round(v * 60) + ' mins';
                    if (Math.floor(v) !== v) return Math.floor(v) + 'h ' + Math.round((v - Math.floor(v)) * 60) + 'm';
                    return v + ' hour' + (v === 1 ? '' : 's');
                }
            })
        },{
            text: 'Assigned To',
            flex: 1,
            dataIndex: 'user',
            sortable: true
        }]
    });
});
</script>

JSON 資料格式 (treegrid.json)

[
    {
        "task": "Project A",
        "user": "Tom",
        "duration": 3.5,
        "expanded": true,
        "children": [
            { "task": "Design", "user": "Tom", "duration": 1.5, "leaf": true },
            { "task": "Develop", "user": "Jerry", "duration": 2, "leaf": true }
        ]
    },
    {
        "task": "Project B",
        "user": "Alice",
        "duration": 1,
        "children": [
            { "task": "Test", "user": "Bob", "duration": 1, "leaf": true }
        ]
    }
]

常見坑點與解決

現象原因解決
TreeGrid 不顯示/空白renderTo 錯誤、store 未載入、JSON 格式錯檢查 renderTo ID、Network 面板看請求、JSON 驗證
展開節點無反應children 非陣列、缺少 leaf: true/false葉子節點加 "leaf": true,非葉子給 "children": []
排序失效TreeStore 預設 remoteSort:true,需後端處理設定 remoteSort: false 或後端實作排序
欄位不顯示資料dataIndex 與 model fields 不符核對 fields 定義與 JSON 鍵名大小寫
樣式錯亂未引入 ext-all.css 或主題衝突確保 CSS 載入、無其他 CSS 覆蓋 .x-grid-cell 等類別

進階技巧

  • 非同步載入子節點:節點設 loaded: false,展開時自動請求 url?node=ID,後端回傳該節點子項。
  • 拖曳排序:加入 viewConfig: { plugins: { ptype: 'treeviewdragdrop' } }。
  • 複選框:selModel: { type: 'checkboxmodel' }。
  • 自定義節點圖示:getIconCls 或 CSS 針對 .x-tree-icon-leaf 覆蓋。

遷移提醒:Ext JS 6/7 (Modern/Classic)

Ext JS 6+ 合併 Touch/Ext,TreeGrid 在 Classic 工具包中仍為 Ext.tree.Panel,API 大致相容。Modern 工具包使用 Ext.grid.Tree,配置略有不同。升級時請參考官方升級指南。

copyright © 萬盛學電腦網 all rights reserved