Swap ngb-tabset tabs by clicking on button

Asked

Viewed 562 times

0

I have two tabs and I need to change tabs by clicking on a button. I usually work with routes, but in this case I need to change tab only, on the same web page.

I have the following Tabs, TAB 1 and TAB 2. Whereas I am on TAB 1, what would be a possible way to go to TAB 2 by clicking on the button GO?

I use Angular 2 and Ng-bootstrap tabs.

NOTE: I cannot use Jquery.

<ngb-tabset>
<ngb-tab title="TAB 1" id="tab1">
  <ng-template ngbTabContent>
      <div class="tab-pane active" id="litaDados" role="tabpanel">
        <form method="post" id="dados" class="form-horizontal">
          <a class="btn btn-secondary"><i _ngcontent-c1 class="fa fa-plus" aria-hidden="true"></i>&nbsp;ABA 1</a>
            <div class="form-group">
             <p>ABA 1</p>
              <button type="button" title="GO" click="go()" </button>
            </div>
          </form>
      </div>
    </ng-template>
  </ngb-tab>

 <ngb-tab title="TAB 2" id="tab2">
  <ng-template ngbTabContent>
  <div class="tab-pane active" id="litaDados2" role="tabpanel">
    <form method="post" id="dados2" class="form-horizontal">
      <a class="btn btn-secondary"><i _ngcontent-c1 class="fa fa-plus" aria-hidden="true"></i>&nbsp;ABA 2</a>
        <div class="form-group">
         <p>ABA 2</p>
        </div>
      </form>
  </div>
  </ng-template>
  </ngb-tab>
</ngb-tabset>

1 answer

1


Unfortunately I could not run a demo, but according to the documentation, it would be basically like this:

<!-- Repare que aqui adicionei o #tabset="ngbTabset" -->
<ngb-tabset #tabset="ngbTabset">
  <ngb-tab title="TAB 1" id="tab1">
    <ng-template ngbTabContent>
      <div class="tab-pane active" id="litaDados" role="tabpanel">
        <form method="post" id="dados" class="form-horizontal">
          <a class="btn btn-secondary">
            <i _ngcontent-c1 class="fa fa-plus" aria-hidden="true"></i>&nbsp;ABA 1
          </a>
          <div class="form-group">
            <p>ABA 1</p>
            <!-- Ao invés de usar Jquery ou uma função,
                 basta passar o select com o #tabset inserido lá acima,
                 desta forma você vai conseguir chamar a tab pelo id -->
            <button (click)="tabset.select('tab2')">GO</button> 
          </div>
        </form>
      </div>
    </ng-template>
  </ngb-tab>
  <ngb-tab title="TAB 2" id="tab2">
    <ng-template ngbTabContent>
      <div class="tab-pane active" id="litaDados2" role="tabpanel">
        <form method="post" id="dados2" class="form-horizontal">
          <a class="btn btn-secondary">
            <i _ngcontent-c1 class="fa fa-plus" aria-hidden="true"></i>&nbsp;ABA 2
          </a>
          <div class="form-group">
            <p>ABA 2</p>
          </div>
        </form>
      </div>
    </ng-template>
  </ngb-tab>
</ngb-tabset>

Browser other questions tagged

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