关于extjs中动态添加TabPanel的tab项并以iframe显示的整理

 近来的项目中用到了Extjs 的TabPanel,这也是Extjs最基本,最常用的组件了

网上或者书上的例子里大都是把tab项渲染到一个div中,

这对于在每个Tab页里加载一个页面的情况就不适合了 

用ifame加载不同的页面应该是最合适的方式 

 

网上也有用ifame显示子项的例子,

是把每一个子项都渲染成了一个ifame

感觉这样有问题,因为切换TabPanel的子项时,子页面不刷新

可能是因为每个ifame里对应的内容都都加载到了主页面,再切换时就不重新加载页面了

同时发现ifame对应的页面里再有js弹出窗口的话,窗体的返回值也取不到

而且采用这种方式,文档结构树上会有多个iframe!

加载多ifame的代码如下:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
    <HEAD>
        <TITLE></TITLE>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="../../extjs2.0/resources/css/ext-all.css" />
        <script type="text/javascript" src="../../extjs2.0/adapter/ext/ext-base.js">
        </script>
        <script type="text/javascript" src="../../extjs2.0/ext-all.js">
        </script>
        <script type="text/javascript" src="../../extjs2.0/source/locale/ext-lang-zh_CN.js">
        </script>
        <script type="text/javascript">
            Ext.onReady(function(){
                Ext.BLANK_IMAGE_URL = '../../extjs2.0/resources/images/default/s.gif';
                var strTabs = '[{"id":"BaseInfo","text":"基本信息","url":"http://www.baidu.com"},{"id":"fromInfo","text":"供货信息","url":"http://www.sohu.com"},{"id":"toInfo","text":"物流信息","url":"http://www.163.com"}]';
                var oTabs = eval('(' + strTabs + ')');
                if (oTabs != null && oTabs.length > 0) {
                    var tabs = new Ext.TabPanel({
                        renderTo: 'tabsContent',
                        activeTab: 0,                        
                        height: 700,
                        frame: true,                        
                        items: [{
                            id: oTabs[0].id,
                            title: oTabs[0].text,
                            html: ' <iframe scrolling="auto" frameborder="0" width="100%" height="100%" src="' + oTabs[0].url + '"> </iframe>'
                        }]
                    });
                    for (var j = 1; j < oTabs.length; j++) {
                        var oItem = {};
                        oItem.id = oTabs[j].id;
                        oItem.title = oTabs[j].text;
                        oItem.html = '<iframe scrolling="auto" frameborder="0" width="100%" height="100%" src="' + oTabs[j].url + '"> </iframe>'
                        tabs.add(oItem);
                    }
                }
                else {
                    document.getElementById("tabsContent").style.display = "none";
                }
            });
        </script>
    </HEAD>
    <BODY>
        <div id="tabsContent" style="margin-top: 2px;">
        </div>
    </BODY>
</HTML>

 附图:

 

 

 

后来经过反反复复的测试,终于整理出了一个个人感觉比较好的解决方案

这样页面上只有一个iframe ,加载iframe 内容页面正常。

 

代码如下

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
    <HEAD>
        <TITLE></TITLE>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="../../extjs2.0/resources/css/ext-all.css" />
        <script type="text/javascript" src="../../extjs2.0/adapter/ext/ext-base.js">
        </script>
        <script type="text/javascript" src="../../extjs2.0/ext-all.js">
        </script>
        <script type="text/javascript" src="../../extjs2.0/source/locale/ext-lang-zh_CN.js">
        </script>
        <script type="text/javascript">
            Ext.onReady(function(){
                Ext.BLANK_IMAGE_URL = '../../extjs2.0/resources/images/default/s.gif';
                var strTabs = '[{"id":"BaseInfo","text":"基本信息","url":"http://www.baidu.com"},{"id":"fromInfo","text":"供货信息","url":"http://www.sohu.com"},{"id":"toInfo","text":"物流信息","url":"http://www.163.com"}]';
                var oTabs = eval('(' + strTabs + ')');
                if (oTabs != null && oTabs.length > 0) {
                    document.getElementById("frmContent").src = oTabs[0].url;
                    var tabs = new Ext.TabPanel({
                        renderTo: 'tabsContent',
                        activeTab: 0,
                        collapsed: true,
                        items: [{
                            id: oTabs[0].id,
                            title: oTabs[0].text,
                            contentEl: 'tabItem'
                        }]
                    });
                    
                    for (var j = 1; j < oTabs.length; j++) {
                        var oItem = {};
                        oItem.id = oTabs[j].id;
                        oItem.title = oTabs[j].text;
                        oItem.contentEl = 'tabItem';                      
                        tabs.add(oItem);
                    }
                    
                    tabs.addListener("tabchange", function(tabs, nowtab){
                        for (var s = 0; s < oTabs.length; s++) {
                            if (oTabs[s].id == nowtab.id) {
                                document.getElementById("frmContent").src = oTabs[s].url;
                                break;
                            }
                        }
                    });
                }
                else {
                    document.getElementById("tabsContent").style.display = "none";
                }
            });
        </script>
    </HEAD>
    <BODY>
        <div id="tabsContent" style="margin-top: 2px;">
            <div id="tabItem" style="width: 0px; height: 0px;">
            </div>
        </div>
        <div id="tabItemsRender" style="height: 440px; overflow: auto; border-left-style: solid; border-left-width: 1px; border-left-color: #8DB2E3; border-right-style: solid; border-right-width: 1px; border-right-color: #8DB2E3; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: #8DB2E3;">
            <iframe id="frmContent" name="frmContent" src="" frameborder="0" height="100%" width="100%">
            </iframe>
        </div>
    </BODY>
</HTML>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值