如何在tabpanel onclick中创建选项卡?

时间:2012-03-29 10:37:22

标签: extjs tabs onclick

我在网格中有一个动作列。我点击此按钮后打开一个窗口,但现在我们要在tabpanel中打开一个新选项卡而不是窗口。这是我想在有人点击操作面板时生成的标签:

Ext.define('MyApp.view.MyTabPanel2', {
extend: 'Ext.tab.Panel',
alias: 'widget.mytabpanel2',

closable: true,
title: 'Report {name}',
activeTab: 1,

initComponent: function() {
    var me = this;

    Ext.applyIf(me, {
        dockedItems: [
            {
                xtype: 'toolbar',
                dock: 'top',
                items: [
                    {
                        xtype: 'textfield',
                        fieldLabel: 'Reference',
                        labelAlign: 'top'
                    },
                    {
                        xtype: 'datefield',
                        fieldLabel: 'From',
                        labelAlign: 'top'
                    },
                    {
                        xtype: 'datefield',
                        fieldLabel: 'To',
                        labelAlign: 'top'
                    },
                    {
                        xtype: 'tbfill'
                    },
                    {
                        xtype: 'button',
                        text: 'Open report'
                    },
                    {
                        xtype: 'button',
                        text: 'Save report'
                    },
                    {
                        xtype: 'button',
                        text: 'Export report'
                    },
                    {
                        xtype: 'button',
                        text: 'Refresh data'
                    }
                ]
            }
        ],
        items: [
            {
                xtype: 'gridpanel',
                title: 'Grid',
                forceFit: true,
                store: 'resultStore',
                columns: [
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'ccuDesignation',
                        text: 'CCU Designation'
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'carrierName',
                        text: 'Carrier Name'
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'dataPackageName',
                        text: 'Data package name'
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'bytesRx',
                        text: 'bytesRX'
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'bytesTx',
                        text: 'bytesTX'
                    }
                ],
                viewConfig: {

                }
            },
            {
                xtype: 'panel',
                title: 'Chart',
                dockedItems: [
                    {
                        xtype: 'chart',
                        height: 250,
                        animate: true,
                        insetPadding: 20,
                        store: 'resultStore',
                        dock: 'top',
                        axes: [
                            {
                                type: 'Category',
                                fields: [
                                    'ccuDesignation'
                                ],
                                position: 'bottom',
                                title: 'CCU Designation'
                            },
                            {
                                type: 'Numeric',
                                fields: [
                                    'bytesTx'
                                ],
                                position: 'left',
                                title: 'Bytes'
                            },
                            {
                                type: 'Numeric',
                                fields: [
                                    'bytesRx'
                                ],
                                position: 'left',
                                title: 'Bytes'
                            }
                        ],
                        series: [
                            {
                                type: 'line',
                                xField: 'x',
                                yField: [
                                    'bytesTx'
                                ],
                                smooth: 3
                            },
                            {
                                type: 'line',
                                xField: 'x',
                                yField: [
                                    'bytesRx'
                                ],
                                smooth: 3
                            }
                        ],
                        legend: {

                        }
                    }
                ]
            }
        ]
    });

    me.callParent(arguments);
}
});

我在sencha读过这篇文章:

// tab generation code
var index = 0;
while(index < 3){
    addTab(index % 2);
}

function addTab (closable) {
    ++index;
    tabs.add({
        title: 'New Tab ' + index,
        iconCls: 'tabs',
        html: 'Tab Body ' + index + '<br/><br/>' + Ext.example.bogusMarkup,
        closable: !!closable
    }).show();
}

Ext.createWidget('button', {
    renderTo: 'addButtonCt',
    text: 'Add Closable Tab',
    handler: function () {
        addTab(true);
    },
    iconCls:'new-tab'
});

Ext.createWidget('button', {
    renderTo: 'addButtonCt',
    text: 'Add Unclosable Tab',
    handler: function () {
        addTab(false);
    },
    iconCls:'new-tab',
    style: 'margin-left: 8px;'
});

但是我的脚本中没有var选项卡。那么我该如何添加标签:

Ext.define('MyApp.view.MyViewport', {
extend: 'Ext.container.Viewport',

layout: {
    type: 'border'
},

initComponent: function() {
    var me = this;

    Ext.applyIf(me, {
        items: [
            {
                xtype: 'tabpanel',
                id: 'tabs',
                activeTab: 1,
                region: 'center',
                items: [
                    {
                        xtype: 'gridpanel',
                        title: 'Reports',
                        forceFit: true,
                        store: 'ReportsStore',
                        columns: [
                            {
                                xtype: 'gridcolumn',
                                dataIndex: 'Name',
                                text: 'Name'
                            },
                            {
                                xtype: 'gridcolumn',
                                dataIndex: 'Type',
                                text: 'Type'
                            },
                            {
                                xtype: 'gridcolumn',
                                dataIndex: 'Description',
                                text: 'Description'
                            },
                            {
                                xtype: 'actioncolumn',
                                dataIndex: 'queryFields',
                                items: [
                                    {
                                        handler: function(view, rowIndex, colIndex, item, e) {
                                            addTab;
                                            alert('clicked');
                                        },
                                        altText: 'Open report',
                                        icon: 'img/report-arrow.png',
                                        tooltip: 'Open report'
                                    }
                                ]
                            }
                        ],
                        viewConfig: {

                        },
                        dockedItems: [
                            {
                                xtype: 'toolbar',
                                dock: 'bottom',
                                items: [
                                    {
                                        xtype: 'tbfill'
                                    },
                                    {
                                        xtype: 'button',
                                        iconCls: 'addReport',
                                        text: 'Add report',
                                        listeners: {
                                            click: {
                                                fn: me.onButtonClick,
                                                scope: me
                                            }
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    });

    me.callParent(arguments);
},

onButtonClick: function(button, e, options) {
    Ext.create('MyApp.view.addReport').show();
}

});
var tabs = Ext.getCmp('tabs');

function addTab (closable) {
alert('yes');
tabs.add(Ext.create('MyApp.view.MyTabPanel2'));
 }

我该怎么做?我和extjs designer 2一起工作

2 个答案:

答案 0 :(得分:1)

在上面显示的第一个视图中,您正在创建另一个tabpanel而不是单个选项卡。如果您尝试将其插入到视口中定义的tabpanel中,则在另一个tabpanel内部会有一个tabpanel。我认为这不是你想做的事。

您可以将上面的第一个视图创建为包含网格面板的Ext.tab.Tab,或者只是将其创建为网格面板本身,并在add方法调用中包含标签配置选项。要在没有将其定义为变量时回答关于引用tabpanel的问题:您应该只给它一个id配置(例如id: 'tabs'),然后您可以使用Ext.getCmp('tabs') 。例如:

// a piece of your viewport config
Ext.applyIf(me, {
    items: [
        {
            xtype: 'tabpanel',
            activeTab: 1,
            region: 'center',
            id: 'tabs', // <-- include this config
            // other configs...

然后可以像这样添加标签:

// get a reference to the tab panel
var tabs = Ext.getCmp('tabs');

// if you create the view as a gridpanel you could do it like this
tabs.add({
    title: sometitle,
    iconCls: someicon,
    closable: yayOrNay,
    items: [Ext.create('MyApp.view.MyGridPanel')]
});

// OR if you create the view as an Ext.tab.Tab which already contains the gridpanel
tabs.add(Ext.create('MyApp.view.MyTab'));

答案 1 :(得分:0)

阅读并使用以下代码:

&#13;
&#13;
function allExpenseTypeReport(){
    var reportByExpenseType=Ext.getCmp("reportByExpenseType");
    if(reportByExpenseType==null){
    	reportByExpenseType = new Ext.tm.reports.ExpenseTypeReport({
        title:WtfGlobal.getLocaleText("ec.reportbyexpensetype"),
        layout:'fit' ,
        closable: true,
        iconCls:'pwnd tabreportsWrap',
        id:"reportByExpenseType"
     });
        Ext.getCmp('as').add(reportByExpenseType);
    }
        
     Ext.getCmp("as").setActiveTab(Wtf.getCmp("reportByExpenseType"));
     reportByExpenseType.doLayout();   
}







 Define Here:

Ext.tm.reports.ExpenseTypeReport = function(config){
    Ext.apply(this, config);
    Ext.tm.reports.ExpenseTypeReport.superclass.constructor.call(this, config);
   
  Define your Code Here:
};
&#13;
&#13;
&#13;