简体   繁体   English

将请求发送到服务器以进行主动引导nav-pills

[英]send request to server for active bootstrap nav-pills

I am using Twitter Bootstrap v3 to create "nav-pills". 我正在使用Twitter Bootstrap v3创建“ nav-pills”。 Each of my tabs has a Bootstrap table that I update the values every 5 seconds. 我的每个选项卡都有一个Bootstrap表,我每5秒更新一次值。 My Java script code for one table update is like below: 我的一个表更新的Java脚本代码如下:

function LoadpData()
{ 
  $(function() {
  $.getJSON(..........);
  return false;
});
};

setInterval( LoadpData, 5000);

Sub section of my HTML file: 我的HTML文件的子部分:

<ul class="nav nav-pills" id="Tabs">
        <li class="active"><a href="#Press" data-toggle="tab">Pr</a></li>
        <li><a href="#Fl" data-toggle="tab">Fl</a></li>
        <li><a href="#Te" data-toggle="tab">Te</a></li>
        <li><a href="#Pu" data-toggle="tab">Pu</a></li>
        <li><a href="#Va" data-toggle="tab">Co</a></li>
</ul>

What I would like to do is to find which tab is active and then update the data only for the table on that tab. 我想做的是找到哪个选项卡处于活动状态,然后仅更新该选项卡上的表的数据。 So I was thinking of adding an if statement to the top of each LoadData() JS function to check and see if that tab is active or not, if yes then send the .getJSON request to the server to get the values, otherwise do nothing. 所以我正在考虑在每个LoadData()JS函数的顶部添加一条if语句,以检查该选项卡是否处于活动状态,如果是,则将.getJSON请求发送到服务器以获取值,否则不执行任何操作。 I searched and found that I should be able to find the active tab by using this selector $('#Tabs li.active'). 我搜索后发现,使用此选择器$('#Tabs li.active')应该可以找到活动选项卡。 Also each of my tabs has a unique href attribute so using this selector $('#Tabs a[href="#Fl"]'), I should be able to get the jquery object and see if it is the active one or not. 另外,我的每个选项卡都具有唯一的href属性,因此使用此选择器$('#Tabs a [href =“#Fl”]'),我应该能够获取jquery对象,并查看它是否是活动的对象。 I am new to jquery, I appreciate if you could help me to find the solution. 我是jquery新手,不胜感激,如果您能帮助我找到解决方案。

function LoadpData()
    {     
// if ($('#Tabs li.active') = $('#Tabs a[href="#Fl"]')){ 
//     $(function() {
//      $.getJSON(..........);
//      return false;
//      });
// }

You can use a script like : 您可以使用如下脚本:

  <script>
    $(document).ready(function() {
      setInterval(function() {
        findAndUpdate();
      }, 3000);

      function findAndUpdate() {
        // Find the active tab 
        var activeTab = $("#tabs").find("li.active");
        var activeTabIndex = activeTab.index();

        // Update the table according to index
        // var str = "";
        switch (activeTabIndex) {
          case 0:
            LoadpData0();
            //str="1";
            // update code for tab 0 table
            break;
          case 1:
            LoadpData1();
            //str="2";
            // update code for tab 1 table
            break;
          case 2:
            LoadpData2();
            //str="3";
            // update code for tab 2 table
            break;
          case 3:
            LoadpData3();
            //str="4";
            // update code for tab 3 table
            break;
          default:
            // default case             
        }
        //alert(str);
      }

      // if you want it should also be updated as soon as a user switches to a new tab, then    
      $("#tabs").on("click", "li", function() {
        setTimeout(function() {
          findAndUpdate();
        }, 30);
        // setting timeout is required as the tab should first change and then this function should be called
      });
    });
  </script>

You can set up a jquery listener on the group of html values on click. 您可以在点击的html值组上设置一个jquery侦听器。

function LoadpData() {
    $('ul.nav li a').click(function(){
    var $this = $(this);
    $this.text({your JSON values can go here});
  })
}

After that you need to call the LoadpData() . 之后,您需要调用LoadpData() Per convention, it is recommended you call the LoadpData in a document.ready(function(){}) block 根据约定,建议您在document.ready(function(){})块中调用LoadpData

Here's a jsFiddle that might help: https://jsfiddle.net/superjisan/hnxLLknv/ 这是一个可能有用的jsFiddle: https ://jsfiddle.net/superjisan/hnxLLknv/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM