Bootstrap tab panel - How to go to a specific tab?

Asked

Viewed 560 times

2

I have, in PHP, a method whose code returns values, passing them to a View, as block below:

return view('PesquisaView')
     ->with('clientes',$resultadoClientes)
     ->with('advs',$resultadoAdvs)
     ->with('bps',$resultadoBPs)
     ->with('mensagemDoUploadRemessa',$avisoUpload);

However, this View has several tabs (tabs) based on the Boostrap framework and the 'Return' displays the View in the first tab, the one that was defined as 'active' at the beginning of the page use.

I would like the 'Return' statement, which causes the View to open, to show directly the tab I want, not the 'default'. In the case of 'id' 'Idpanecitipesquisarremessa'.

The html code that defines the guides is

<ul class="nav nav-tabs">
     <li><a href="#idQExpert" data-toggle="tab">QExpert</a></li>
     <li>
       <a href="#idPaneCitiPesquisarRemessa" data-toggle="tab">Citibank - Remessa</a>
     </li>
     <li>
        <a href="#idPaneCitiPesquisar" data-toggle="tab">Citibank - Retorno</a>
     </li>
</ul>

Updating:

Taking advantage of @Virgilio Novic’s valuable suggestion (as well as @wmengue and @massreuy’s), I changed my code to the following:

Method in PHP:

$tab = 2;
return view('PesquisaView')
 ->with('clientes',$resultadoClientes)
 ->with('advs',$resultadoAdvs)
 ->with('bps',$resultadoBPs)
 ->with('mensagemDoUploadRemessa',$avisoUpload);
 ->with('tab',$tab);

HTML markup:

<ul class="nav nav-tabs" id="myTab">
 <li class="{{$tab==1?'active':''}}"><a href="#idQExpert" data-toggle="tab">QExpert</a></li>
 <li class="{{$tab==2?'active':''}}"><a href="#idPaneCitiPesquisarRemessa" data-toggle="tab">Citibank - Remessa</a>
 </li>
 <li class="{{$tab==3?'active':''}}">
    <a href="#idPaneCitiPesquisar" data-toggle="tab">Citibank - Retorno</a>
 </li>
</ul>   

After executing the method, it returns the value 2 for the $tab variable, the View Blade accepts the parameter and highlights the 'Citibank - Shipment' tab (the middle tab, in the image below, which has the white background), but note that the content, which is a 'form' html, does not appear.

Tela após o return do código PHP However, if the tabs are clicked, for page exchange, be the first or the third and, back to the second, there yes the form is rendered, as new image below: Imagem correta, que deveria estar exibida após o retorno do método

That is, the click event does not happen when becoming the 'active' class via code.

3 answers

1

You can do a treatment in your code by sending a parameter to the view that will determine the active tab.

<li class="active">

The parameter will tell you when that class should enter or not. Or if you need it to always be fixed, just put it in the tab you need.

  • I confess that I don’t know how to do this. I imagine it’s something like ...->with('class','<li class="active">')..., but I don’t know how to take advantage of your idea.

  • @Maurosimoes Your question is how to send this from Laravel to the view or how to treat the parameter sent to it to place the class?

  • I confess that I do not see how to pass 'with' and, in View-Blade, how to take advantage of what comes from PHP.

  • @Maurosimoes in the Blade view you can access the variable that went by php like this: {{ $nameVariavelUsadaNoWith }} so you will recover what was passed from php to the view-Blade

  • I have to test isset($nameVariavelUsadaNoWith) before applying the class, no?

  • @Maurosimoes You can use the Laravel isset/endisset directive to view more information on https://laravel.com/docs/5.4/blade#control-Structures

Show 1 more comment

1

Do the following, in your controller, add an additional variable containing the tab you want to mark as active, in this case:

return view('PesquisaView')
->with('clientes',$resultadoClientes)
->with('advs',$resultadoAdvs)
->with('bps',$resultadoBPs)
->with('mensagemDoUploadRemessa',$avisoUpload)
->with('activeTab', $activeTab);

Then create a new view with a treatment to receive this value. For example activeTab.blade.php com the following code:

@if($params['tab'] == 'idQExpert')
    active
@elseif($params['tab'] == 'idPaneCitiPesquisarRemessa')
    active
@elseif($params['tab'] == 'idPaneCitiPesquisar')
    active
@endif

And finally, in its main view, in each of the elements <li> add as follows:

<ul class="nav nav-tabs">
    <li class="@include('layouts/activeTab', ['params' => ['tab' => $activeTab]])">
        <a href="#idQExpert" data-toggle="tab">QExpert</a>
    </li>
    <li class="@include('layouts/activeTab', ['params' => ['tab' => $activeTab]])">
        <a href="#idPaneCitiPesquisarRemessa" data-toggle="tab">Citibank - Remessa</a>
    </li>
    <li class="@include('layouts/activeTab', ['params' => ['tab' => $activeTab]])">
        <a href="#idPaneCitiPesquisar" data-toggle="tab">Citibank - Retorno</a>
    </li>
</ul>

This will probably solve your problem.

  • I made two fixes to test your code: first, I added a bracket closure to the @include lines because it was missing; second, I removed the reference to the 'layouts' folder because my Views are in the same Views folder. However, when opening, the code is complaining about the $activeTab variable being empty, because it is only filled in the 'Return' of PHP, the central subject of our discussion. That is, I believe I have to do isset($activeTab) before.

  • Sorry for the syntax errors, I did everything right here stackoverflow editor. About the variable error, if this view is called only by this controller method does not need, because in the controller you will always send. If you are going to use it in other situations, then yes, you should put a isset even.

  • I chose another approach, because I couldn’t make it work. I restructured from scratch.

  • (I have chosen another approach, but I have not yet been able to make it work)

  • Fine, anything warns here. Anything enters the group of Slack

1

Send the information you need tab is the default at the time of loading:

Code

$tab = 2;
return view('PesquisaView')
     ->with('clientes',$resultadoClientes)
     ->with('advs',$resultadoAdvs)
     ->with('bps',$resultadoBPs)
     ->with('mensagemDoUploadRemessa',$avisoUpload);
     ->with('tab',(is_null($tab)) ? 1 : $tab);

And in the code of blade do:

Html

<ul class="nav nav-tabs">
  <li @if($tab == 1) {{ 'class="active"' }} @endif ><a href="#">Home</a></li>
  <li @if($tab == 2) {{ 'class="active"' }} @endif ><a href="#">Profile</a></li>
  <li @if($tab == 3) {{ 'class="active"' }} @endif ><a href="#">Messages</a></li>
</ul>
  • Virgil, it didn’t work. Only the tabs were alone, without highlight and the screen without content. I modified your code on one of the lines, changing to the ternary condition, to <li class="{{$tab=='1' ? 'active':''}}" ><a href="#idQExpert" data-toggle="tab">Qexpert</a></li> and the screen remains without content, but the desired tab was highlighted.

  • I meant: I changed the html lines completely. One of the lines was like the example above.

  • @Maurosimoes the above code is the reality of what you need, edit your question and put all html code and the controller please let me see what’s going on?

Browser other questions tagged

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