Home > Back-end >  How can I change a selected TAB with [Previous], [Next] buttons using type script function
How can I change a selected TAB with [Previous], [Next] buttons using type script function

Time:10-01

How can I switch tabs using previous and next buttons in angular type sript. Below I'm written some sample code, I don't know how to sync between buttons and tabs.

Sample code:

<div>
  <ng-container>
      <tabset>
        <tab>
          <ng-template tabHeading>
            <span title="Flash mobile">Flash</span>
          </ng-template>
          <p> Flashing mobile </p>
        </tab>
        <tab>
          <ng-template tabHeading>
            <span title="Restore mobile">Restore</span>
          </ng-template>
          <p> Restore mobile </p>
        </tab>
      </tabset>
    </ng-container>
 </div>

<div>
<button class="btn" (click)="updatePrev()"> Previous </button>
<button class="btn" (click)="updateNext()"> Next </button>
</div>

CodePudding user response:

If you are using bootstrap tab, you can provide ids to your tabs. Reference your tabset component using template reference variable and use the select method to select the tabs by id. Refer the sample code below:

<ngb-tabset #t="ngbTabset">
  <ngb-tab id="tab-selectbyid1" title="Simple">
    <ng-template ngbTabContent>
      <p>Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth
      master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh
      dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum
      iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p>
    </ng-template>
  </ngb-tab>
  <ngb-tab id="tab-selectbyid2">
    <ng-template ngbTabTitle><b>Fancy</b> title</ng-template>
    <ng-template ngbTabContent>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid.
      <p>Exercitation  1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table
      craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl
      cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia
      yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean
      shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero
      sint qui sapiente accusamus tattooed echo park.</p>
    </ng-template>
  </ngb-tab>
</ngb-tabset>

<p>
  <button class="btn btn-outline-primary" (click)="t.select('tab-selectbyid2')">Selected tab with "tab-selectbyid2" id</button>
</p>

CodePudding user response:

Please check this solution as per your example, with some minor changes.

Change 1: Added Id's to each tabs. So that each Tabs will have unique id's

<tab>
   <ng-template tabHeading>
    <span title="Restore mobile">Restore</span>
   </ng-template>
   <p> Restore mobile </p>
</tab>

to

<tab id="1">
  <ng-template tabHeading>
    <span title="Restore mobile">Restore</span>
  </ng-template>
 <p> Restore mobile </p>
</tab>

During Next and previous operations, the IDs will be added/subtracted by one. The below Stackblitz sample with more than two tabs is self-explanatory. The sample is developed upon ngx-bootstrap.

https://stackblitz.com/edit/ngx-bootstrap-kdtihy?file=app/app.component.html

  • Related