JQuery EasyUI 选项卡联动demo(7)

先看代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Full Layout - jQuery EasyUI Demo</title>
    <link rel="stylesheet" type="text/css" href="easyUI/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyUI/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="easyUI/demo/demo.css">
    <script type="text/javascript" src="easyUI/jquery.min.js"></script>
    <script type="text/javascript" src="easyUI/jquery.easyui.min.js"></script>
</head>
<body>
        <div id="tt" class="easyui-tabs" data-options="tools:'#tab-tools'" style="width:700px;height:250px">
        </div>
        <div id="tab-tools">
            <a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'" οnclick="addPanel()"></a>
            <a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'" οnclick="removePanel()"></a>
        </div>
        <script type="text/javascript">
            var index = 0;
            function addPanel(){
                index++;
                $('#tt').tabs('add',{
                    title: 'Tab'+index,
                    content: '<div style="padding:10px">Content'+index+'</div>',
                    closable: true
                });
            }
            function removePanel(){
                var tab = $('#tt').tabs('getSelected');
                if (tab){
                    var index = $('#tt').tabs('getTabIndex', tab);
                    $('#tt').tabs('close', index);
                }
            }
        </script>
</body>
</html>



效果图:


点击右上角的绿色按钮,可以实现选项卡的添加和删除。

我们先对这段代码进行一下改造,首先将选项卡放进layout 布局里:

<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'north'" style="height:50px"></div>
    <div data-options="region:'south',split:true" style="height:50px;"></div>
    <div data-options="region:'east',split:true" title="East" style="width:100px;"></div>
    <div data-options="region:'west',split:true" title="West" style="width:250px;"></div>
        <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'">
            <div id="tt" class="easyui-tabs" data-options="tools:'#tab-tools'" style="height: 100%;">
            </div>
            <div id="tab-tools">
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'" οnclick="addPanel()"></a>
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'" οnclick="removePanel()"></a>
            </div>
        </div>
</div>

如图:


然后将选项卡的增加、删除功能移到布局的左边的框体中,如图:



代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Full Layout - jQuery EasyUI Demo</title>
    <link rel="stylesheet" type="text/css" href="easyUI/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyUI/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="easyUI/demo/demo.css">
    <script type="text/javascript" src="easyUI/jquery.min.js"></script>
    <script type="text/javascript" src="easyUI/jquery.easyui.min.js"></script>
    <script type="text/javascript">
        var index = 0;
        function addPanel(){
            index++;
            $('#tt').tabs('add',{
                title: 'Tab'+index,
                content: '<div style="padding:10px">Content'+index+'</div>',
                closable: true
            });
        }
        function removePanel(){
            var tab = $('#tt').tabs('getSelected');
            if (tab){
                var index = $('#tt').tabs('getTabIndex', tab);
                $('#tt').tabs('close', index);
            }
        }
    </script>
</head>
<body>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'north'" style="height:50px"></div>
    <div data-options="region:'south',split:true" style="height:50px;"></div>
    <div data-options="region:'east',split:true" title="East" style="width:100px;"></div>
    <div data-options="region:'west',split:true" title="West" style="width:250px;">
        <div οnclick="addPanel()" style="text-align: center; border: 1px solid red;cursor: pointer;margin-top: 10px;background-color: #00bbee">增加</div>
        <div οnclick="removePanel()" style="text-align: center; border: 1px solid red;cursor: pointer;margin-top: 10px;background-color: #00bbee">删除</div>
    </div>
    <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'" style="border: 0px;">
        <div id="tt" class="easyui-tabs" style="height: 100%;">
        </div>
    </div>
</div>
</body>
</html>
然后我们将增加和删除改成选项卡1、选项卡2,我们想要的效果,是点击左侧的选项卡1,那么右侧就会显示出选项卡1,点击选项卡2,右侧就会显示选项卡2;
代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Full Layout - jQuery EasyUI Demo</title>
    <link rel="stylesheet" type="text/css" href="easyUI/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyUI/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="easyUI/demo/demo.css">
    <script type="text/javascript" src="easyUI/jquery.min.js"></script>
    <script type="text/javascript" src="easyUI/jquery.easyui.min.js"></script>
    <script type="text/javascript">
        function addPanel(index){
            $('#tt').tabs('add',{
                title: '选项卡'+index,
                content: '<div style="padding:10px">Content'+index+'</div>',
                closable: true
            });
        }
        function removePanel(){
            var tab = $('#tt').tabs('getSelected');
            if (tab){
                var index = $('#tt').tabs('getTabIndex', tab);
                $('#tt').tabs('close', index);
            }
        }
    </script>
</head>
<body>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'north'" style="height:50px"></div>
    <div data-options="region:'south',split:true" style="height:50px;"></div>
    <div data-options="region:'east',split:true" title="East" style="width:100px;"></div>
    <div data-options="region:'west',split:true" title="West" style="width:250px;">
        <div οnclick="addPanel(1)" style="text-align: center; border: 1px solid red;cursor: pointer;margin-top: 10px;background-color: #00bbee">选项卡1</div>
        <div οnclick="addPanel(2)" style="text-align: center; border: 1px solid red;cursor: pointer;margin-top: 10px;background-color: #00bbee">选项卡2</div>
    </div>
    <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'" style="border: 0px;">
        <div id="tt" class="easyui-tabs" style="height: 100%;">
        </div>
    </div>
</div>
</body>
</html>

但这样做有一个问题没有解决,就是当我们的选项卡1处于打开状态时,再点击选项卡1的按钮,还会打开一个选项卡1。也就是说我们需要一个判断,每一个选项卡都只能打开一次。我们可以给每一个选项卡一个标识(flag=0),当选项卡打开后,标识就会发生变化(flag=1);那么我们就可以进行判断,如果flag=1,那么就不需要再打开一个选项卡了。我们稍微修改一下js代码:

<script type="text/javascript">
        var flag1=0;
        var flag2=0;
        function addPanel(index){
            if(index==1){
                if(flag1==0) {
                    flag1=1;
                    $('#tt').tabs('add',{
                        title: '选项卡'+index,
                        content: '<div style="padding:10px">Content'+index+'</div>',
                        closable: true
                    });
                }
            }else if(index==2){
                if(flag2==0){
                    flag2=2;
                    $('#tt').tabs('add',{
                        title: '选项卡'+index,
                        content: '<div style="padding:10px">Content'+index+'</div>',
                        closable: true
                    });
                }
            }
        }
</script>
这样一来,一个选项卡就只能打开一次了。

接下来我们需要做的是,如果这个选项卡关闭,就让flag=0;这样就可以在关闭选项卡之后可以再次打开。

添加script代码:

 $(function (){
            $('#tt').tabs({
                onBeforeClose:function(title){
                    if(title=='选项卡'+flag1){
                        flag1=0;
                    }
                    if(title=='选项卡'+flag2){
                        flag2=0;
                    }
                }
            });
        });
这段代码给选项卡添加了一个关闭前事件,如果点击选项卡的关闭按钮,就会触发该事件,将对应title的flag的值改成0;

完整代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Full Layout - jQuery EasyUI Demo</title>
    <link rel="stylesheet" type="text/css" href="easyUI/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyUI/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="easyUI/demo/demo.css">
    <script type="text/javascript" src="easyUI/jquery.min.js"></script>
    <script type="text/javascript" src="easyUI/jquery.easyui.min.js"></script>
    <script type="text/javascript">
        var flag1=0;
        var flag2=0;
        function addPanel(index){
            if(index==1){
                if(flag1==0) {
                    flag1=1;
                    $('#tt').tabs('add',{
                        title: '选项卡'+index,
                        content: '<div style="padding:10px">Content'+index+'</div>',
                        closable: true
                    });
                }
            }else if(index==2){
                if(flag2==0){
                    flag2=2;
                    $('#tt').tabs('add',{
                        title: '选项卡'+index,
                        content: '<div style="padding:10px">Content'+index+'</div>',
                        closable: true
                    });
                }
            }
        }
        $(function (){
            $('#tt').tabs({
                onBeforeClose:function(title){
                    if(title=='选项卡'+flag1){
                        flag1=0;
                    }
                    if(title=='选项卡'+flag2){
                        flag2=0;
                    }
                }
            });
        });
    </script>
</head>
<body>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'north'" style="height:50px"></div>
    <div data-options="region:'south',split:true" style="height:50px;"></div>
    <div data-options="region:'east',split:true" title="East" style="width:100px;"></div>
    <div data-options="region:'west',split:true" title="West" style="width:250px;">
        <div οnclick="addPanel(1)" style="text-align: center; border: 1px solid red;cursor: pointer;margin-top: 10px;background-color: #00bbee">选项卡1</div>
        <div οnclick="addPanel(2)" style="text-align: center; border: 1px solid red;cursor: pointer;margin-top: 10px;background-color: #00bbee">选项卡2</div>
    </div>
    <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'" style="border: 0px;">
        <div id="tt" class="easyui-tabs" style="height: 100%;">
        </div>
    </div>
</div>
</body>
</html>
这样就大体上实现了我们想要的功能。

不过还需要加上一点,就是当我们点击左侧“选项卡1”按钮的时候,当选项卡1已经被打开过,却并不是在选中状态时,我们可以将它设置为选中状态。
完整代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Full Layout - jQuery EasyUI Demo</title>
    <link rel="stylesheet" type="text/css" href="easyUI/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyUI/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="easyUI/demo/demo.css">
    <script type="text/javascript" src="easyUI/jquery.min.js"></script>
    <script type="text/javascript" src="easyUI/jquery.easyui.min.js"></script>
    <script type="text/javascript">
        var flag1=0;
        var flag2=0;
        function addPanel(index){
            if(index==1){
                if(flag1==0) {
                    flag1=1;
                    $('#tt').tabs('add',{
                        title: '选项卡'+index,
                        content: '<div style="padding:10px">Content'+index+'</div>',
                        closable: true
                    });
                }else{
                    $('#tt').tabs('select','选项卡'+flag1);
                }
            }else if(index==2){
                if(flag2==0){
                    flag2=2;
                    $('#tt').tabs('add',{
                        title: '选项卡'+index,
                        content: '<div style="padding:10px">Content'+index+'</div>',
                        closable: true
                    });
                }else{
                    $('#tt').tabs('select','选项卡'+flag2);
                }
            }
        }
        $(function (){
            $('#tt').tabs({
                onBeforeClose:function(title){
                    if(title=='选项卡'+flag1){
                        flag1=0;
                    }
                    if(title=='选项卡'+flag2){
                        flag2=0;
                    }
                }
            });
        });
    </script>
</head>
<body>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'north'" style="height:50px"></div>
    <div data-options="region:'south',split:true" style="height:50px;"></div>
    <div data-options="region:'east',split:true" title="East" style="width:100px;"></div>
    <div data-options="region:'west',split:true" title="West" style="width:250px;">
        <div οnclick="addPanel(1)" style="text-align: center; border: 1px solid red;cursor: pointer;margin-top: 10px;background-color: #00bbee">选项卡1</div>
        <div οnclick="addPanel(2)" style="text-align: center; border: 1px solid red;cursor: pointer;margin-top: 10px;background-color: #00bbee">选项卡2</div>
    </div>
    <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'" style="border: 0px;">
        <div id="tt" class="easyui-tabs" style="height: 100%;">
        </div>
    </div>
</div>
</body>
</html>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值