Extjs4.x Ext.tree.Panel基本树控件的使用案例、源码

1.Model

Ext.define('WMS.model.VPTree', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'url', type: 'string' },
        { name: 'text', type: 'string' },
        { name: 'key', type: 'string' }
    ]
});

2.Store

/// <reference path="http://www.cnblogs.com/Ext.js" />
Ext.define("WMS.store.VPTreeMenu", {
    extend: 'Ext.data.TreeStore',
    autoLoad: true,
    model: 'WMS.model.VPTree',
    root: {
        text: '功能节点',
        id: 'root',
        expanded: true
    },
    proxy: {
        type: 'ajax',
        api: {
            read: '/Admin/Main/GetMenu'
        },
        reader: {
            type: 'json'
        }
    }
});

3.View

Ext.define("WMS.view.Tree", {
    extend: 'Ext.tree.Panel',
    alias: 'widget.wmsTree',
    title: "功能导航",
    margins: '0 0 0 3',
     200,
    region: 'west',
    animate: true,
    store: 'VPTreeMenu',
    autoScroll: true,
    rootVisible: false,
    loadMsg:true,
    collapsible: true,//是否可以折叠
    split: true
});

4.后台构造json

/// <summary>
        /// 获取系统的模块
        /// </summary>
        /// <returns></returns>
        public string GetMenuModule()
        {
            //根据用户获取所有的角色
            //根据角色获取所有的功能
            //根据SystemId构造树
            List<WMS_Module> Module = dal.GetMenu(AuthToken.CurrentUser.Id.ToString()).ToList();
            List<WMS_Module> fatherList = Module.FindAll(c => c.ParentId == AuthToken.SystemGuid).OrderBy(c => c.ModuleIndex).ToList();
            List<WMS_Module> childList = Module.FindAll(c => c.ParentId != AuthToken.SystemGuid).OrderBy(c => c.ModuleIndex).ToList();
            return "[" + CreateMenuTree(fatherList, childList).ToString().TrimEnd(',') + "]";
        }

        //递归
        private StringBuilder CreateMenuTree(List<WMS_Module> fatherList, List<WMS_Module> childList)
        {
            StringBuilder sb = new StringBuilder();
            foreach (WMS_Module menu in fatherList)
            {
                StringBuilder sub = new StringBuilder();
                //核心
                string guid = menu.Id.ToString("D").ToUpper();
                List<WMS_Module> stack = childList.FindAll(c => c.ParentId == guid).OrderBy(c => c.ModuleIndex).ToList();
                if (stack.Count > 0) //说明是父节点
                {
                    sub.Append("{text:'" + menu.ModuleName + "',expanded:true,id:'" + menu.Id.ToString() + "',children: [");
                    sub.Append(CreateMenuTree(stack, childList).ToString().TrimEnd(',') + "]},");
                }
                else //说明是子节点
                {
                    sub.Append("{text:'" + menu.ModuleName + "',url:'" + menu.ModuleUrl + "',key:'"+menu.ModuleKey+"',leaf:true,iconCls:'" + menu.ModuleIcon + "',id:'" + menu.Id.ToString() + "'},");
                }
                sb.Append(sub);
            }
            return sb;
        }
原文地址:https://www.cnblogs.com/qidian10/p/3060390.html