【问题标题】:How do I "get" the ID for a "clicked" tab in a dijit.layout.tabcontainer?如何在 dijit.layout.tabcontainer 中“获取”“单击”选项卡的 ID?
【发布时间】:2011-03-25 11:40:25
【问题描述】:

我无法为此找到明确定义的解决方案。大多数是不完整的sn-ps。

这是一个简单的示例。请参阅 doSomething() 注释:

<head>
    <style type="text/css">
        body, html { font-family:helvetica,arial,sans-serif; font-size:90%; }
    </style>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dijit/themes/claro/claro.css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js"
    djConfig="parseOnLoad: true">
    </script>
    <script type="text/javascript">
        dojo.require("dijit.layout.TabContainer");
        dojo.require("dijit.layout.ContentPane");

    function doSomething() {
        //Put code in here that identifies the clicked tab and displays it's id in the below alert
        alert("Hello World!");
    }   

    </script>

</head>

<body class=" claro ">
    <div id="mainTab" dojoType="dijit.layout.TabContainer" style="width: 400px; height: 100px;" onClick="doSomething();">
        <div id="tab1" dojoType="dijit.layout.ContentPane" title="My first tab" selected="true">
            Lorem ipsum and all around...
        </div>
        <div id="tab2" dojoType="dijit.layout.ContentPane" title="My second tab">
            Lorem ipsum and all around - second...
        </div>
        <div id="tab3" dojoType="dijit.layout.ContentPane" title="My last tab">
            Lorem ipsum and all around - last...
        </div>
    </div>
</body>

【问题讨论】:

    标签: javascript dojo dijit.layout


    【解决方案1】:

    这是一个在 Dojo 1.8 中工作的完整代码示例,我已经对其进行了测试:

    require(["dijit/registry",  "dojo/on", "dojo/ready", "dojo/domReady!"], function (registry, on, ready) {
        ready(function () { //wait till dom is parsed into dijits
            var panel = registry.byId('mainTab');   //get dijit from its source dom element
            on(panel, "Click", function (event) {   //for some reason onClick event doesn't work 
                $('.hidden_field_id').val(panel.selectedChildWidget.id);  //on click, save the selected child to a hidden field somewhere. this $ is jquery, just change it to 'dojo.query()'
            });
        });
    });
    
    //include this function if you want to reselect the tab on page load after a postback
    require(["dijit/registry", "dojo/ready", "dojo/domReady!"], function (registry, ready) {
        ready(function () {
            var tabId = $('.hidden_field_id').val();
            if (tabId == null || tabId == "")
                return;
            var panel = registry.byId('mainTab');
            var tab = registry.byId(tabId);
            panel.selectChild(tab);
        });
    });
    

    【讨论】:

      【解决方案2】:

      我相信dijit.byId('mainTab').selectedChildWidget 应该为您提供所选选项卡中小部件的引用(例如,您的一个 ContentPanes)。

      http://www.dojotoolkit.org/api/dijit/layout/TabContainer.html#selectedChildWidget

      【讨论】:

        【解决方案3】:

        您可以使用 dojo.connect 附加到 TabContainer 上的 selectChild 事件。类似的东西

        dojo.connect(dijit.byId("mainTab"), "selectChild", function(page){ alert(page.id); });
        

        您也可以通过对 mainTab-selectChild 主题执行 dojo.subscribe 来做到这一点。

        【讨论】:

          【解决方案4】:

          试试这个:

          <head>
              <style type="text/css">
                  body, html { font-family:helvetica,arial,sans-serif; font-size:90%; }
              </style>
              <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dijit/themes/claro/claro.css"/>
              <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js" djConfig="parseOnLoad: true">
              </script>
              <script type="text/javascript">
                  dojo.require("dijit.layout.TabContainer");
                  dojo.require("dijit.layout.ContentPane");
                  dojo.require("dojo.NodeList-traverse");
          
              function doSomething(e) {
                  var tab = dojo.query(e.target).parents('.dijitTab')[0];
                  if (tab) {
                      alert(tab.getAttribute('widgetId'));
                  }
              }   
          
              </script>
          
          </head>
          
          <body class=" claro ">
              <div id="mainTab" dojoType="dijit.layout.TabContainer" style="width: 400px; height: 100px;" onClick="doSomething">
                  <div id="tab1" dojoType="dijit.layout.ContentPane" title="My first tab" selected="true">
                      Lorem ipsum and all around...
                  </div>
                  <div id="tab2" dojoType="dijit.layout.ContentPane" title="My second tab">
                      Lorem ipsum and all around - second...
                  </div>
                  <div id="tab3" dojoType="dijit.layout.ContentPane" title="My last tab">
                      Lorem ipsum and all around - last...
                  </div>
              </div>
          </body>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多