使用ext封装自定义组件-导航栏组件

/**
 * 导航组件。
 * User: zhangzg 492274248@qq.com
 * Date: 14-7-11
 * Time: 下午3:39
 *
 */
Ext.define('Exp.component.Navigation',{
    extend:'Ext.Component',
    cls:'student-sta-titlediv',
    navgData:{},
    initComponent:function(){
        this.callParent();
        Exp.loadCss('framework/expand/src/component/css/student-sta-style.css');

        this.tpl = new Ext.XTemplate('<div class="student-sta-titname"><tpl for="nodes">' +
            ' <a href="#" treeId="{id}">{text}</a> ' +
            '{[xindex === xcount ? "" : ">>"]} </tpl></div>' +
            '<div class="student-sta-iconname"><tpl for="children">{% if (xcount === 0) break; %} ' +
            '<a href="#" treeId="{id}">{text}</a></tpl> </div>');
        this.data = {};
        this.bindEvents();
    },

    /**
     * 绑定事件
     */
    bindEvents : function(){
        var me = this;
        this.on('afterrender',function(){
            this.update({nodes:[me.navgData[0]],
            children:me.navgData[0].children});
        });
        this.on({
            click : {
                element : 'el',
                fn : function(event,el){
                    if(el.tagName=='A'){
                        var data = {
                            id : event.getTarget().getAttribute('treeId'),
                            text : el.text
                        };
                        me.transData(data);
                        me.update(me.currentData);
                    }
                }
            }
        });
    },
    /**
     * 刷新导航栏组件。
     * @param data
     */
    transData:function(data){
        var nodeId = data.id,
            nodes = [],
            flag = true,
            newNodes = [],
            obj = {};
        while(flag){

            for(var key in this.navgData){
                var item = this.navgData[key];
                if(item.id == nodeId){
                    nodes.push(item);
                    nodeId = item.pid;
                    break;
                }
            }

            // 找到根节点之后,跳出while循环
            if(nodes.length!=0 && (nodes[nodes.length-1].pid == -1 || nodes[nodes.length-1].pid==null)){
                flag = false;
            }
            // 如果hashmap中没有该节点,那么跳出循环。
            if(typeof this.navgData[nodeId] == 'undefined'){
                flag = false;
            }
        }

        for(var i = nodes.length-1;i>=0;i--){
            newNodes.push(nodes[i]);
            if(i==0){
                obj.children = nodes[i].children;
            }
        }
        this.currentData = Ext.apply({nodes:newNodes},obj);
    },
    /**
     * 获取组件的值
     */
    getValue:function(){
        return this.currentData||{};
    }
});

使用方法

var navigation = new Exp.component.Navigation({navgData:{
            0:{
                id:0,
                text:'**师范学院',
                pid:-1,
                cclx:'XX',
                cc:1,
                children:[{
                    id:1,
                    text:'化学化工学院',
                    pid:0,
                    cclx:'XY',
                    cc:2,
                    children:[]
                },{
                    id:2,
                    text:'计算机科学与技术学院',
                    pid:0,
                    cclx:'XY',
                    cc:2,
                    children:[{
                        id:4,
                        text:'软件工程',
                        pid:2,
                        cclx:'ZY',
                        cc:3,
                        children:[]
                    }]
                },{
                    id:3,
                    text:'政法学院',
                    pid:0,
                    cclx:'XY',
                    cc:2,
                    children:[]
                }]
            },
            1:{
                id:1,
                text:'化学化工学院',
                pid:0,
                cclx:'XY',
                cc:2,
                children:[]
            },
            2:{
                id:2,
                text:'计算机科学与技术学院',
                pid:0,
                cclx:'XY',
                cc:2,
                children:[{
                    id:4,
                    text:'软件工程',
                    pid:2,
                    cclx:'ZY',
                    cc:3,
                    children:[]
                },{
                    id:5,
                    text:'软件技术(PHP)',
                    pid:2,
                    cclx:'ZY',
                    cc:3,
                    children:[]
                },{
                    id:6,
                    text:'软件技术(Java)',
                    pid:2,
                    cclx:'ZY',
                    cc:3,
                    children:[]
                },{
                    id:7,
                    text:'软件技术(.net)',
                    pid:2,
                    cclx:'ZY',
                    cc:3,
                    children:[]
                }]
            },
            3:{
                id:3,
                text:'政法学院',
                pid:0,
                cclx:'XY',
                cc:2,
                children:[]
            },
            4:{
                id:4,
                text:'软件工程',
                pid:2,
                cclx:'ZY',
                cc:3,
                children:[]
            }
        }});

效果图


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值