When loading page, open first tab with results

Asked

Viewed 38 times

1

I have five tabs, and each one displays or not the result of a query in the bank.

<div class="container">  
  <ul class="nav nav-tabs">
    <li class="active">
      <a data-toggle="tab" class="nav-link" href="#basic">Basic</a>
    </li>
    <li class="nav-item">
     <a data-toggle="tab" class="nav-link" href="#gold">Gold></a>
    </li>
     .
     .
     .
</ul>

the basic tab is activated when you load the page:

 <script>
    $(document).ready(function(){
        $('a[href="#basic"]').tab('show');
    });
</script>

It happens that as each tab is the result of a query, it may occur that the "basic" tab does not contain any data to display, and the other yes.

inserir a descrição da imagem aqui

I’m looking for a solution to display the first tab with result, by loading the page

NOTE: I use Listview to display the data.

  • What is the HTML of the tab?

  • <div class="tab-content"> <div id="basic" class="tab-pane fade in active">

  • Try this: $(document).ready(function(){&#xA; $('.tab-pane').each(function(){&#xA; if($(this).children().length){&#xA; $('a[href="#'+ this.id +'"]').tab('show');&#xA; return false;&#xA; }&#xA; });&#xA;});

  • @Sam opened the tab "basic", which is the first one. In the test, this tab could not open, because it did not contain data of the query, and the others yes. Returned only the Listview <Emptydatatemplate> return message.

  • Will checking whether it contains any text in the tab works?

  • i have these variables on the page that show whether or not the tab has data: <% Response.Write(Basic);%> and so on for each of the five tabs. I think these variables can help solve the problem.

Show 2 more comments

1 answer

0


As discussed in the chat, just search the tabs for the first different value of 0 on the tag <sup>:

<li class="active"> 
   <a data-toggle="tab" class="nav-link" href="#basic">
      Basic <sup><% Response.Write(Basic);%></sup>
   </a> 
</li>

inserir a descrição da imagem aqui

$(document).ready(function(){
   $('a[data-toggle=tab]').each(function(){
      if(Number($("sup", this).text())){
         $(this).tab('show');
         return false;
      }
   });
});
  • if everyone is "0" you have to open one of them to show the Listview message that there is no data in that view and so on. That detail was missing only.

  • Ah yes... I’ll see here

  • 1

    Have a look at the code: https://jsfiddle.net/kyq5dcao/

  • Perfect. Exactly the expected behavior. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.