"Utilizing Primeng's dynamic functionality to create a selected p-tab

Utilizing the TabView module from primeng, I have created a dynamic tab where only the last tab remains static. The property used is 'selected', and for the dynamic tab, it is set as [selected]="'tab' + $index", where $index represents the index of ngFor loop.

Since the list contains only one item, the property 'selected' will be tab0. Therefore, I have defined the attribute tab0 as false.

The issue arises when both tabs are selected despite the attributes being defined as false.

<p-tabView>
    <p-tabPanel header="{{category.categoryLabel}}" *ngFor="let category of categories; let $index=index" [selected]="'tab' + $index">
        <button type="button" class="btn btn-primary">{{'button.previous'|translate}}</button>
        <button type="button" class="btn btn-primary">{{'button.next'|translate}}</button>
        <button class="btn btn-default">{{'button.reset'|translate}}</button>
    </p-tabPanel>
    <p-tabPanel header="{{'tab.getProduct'|translate}}" [selected]="tab">
        <button type="button" class="btn btn-primary">{{'button.next'|translate}}</button>
        <button class="btn btn-default">{{'button.reset'|translate}}</button>
    </p-tabPanel>
</p-tabView>


 public tab0: boolean = false;
 public tab: boolean = false;

https://i.sstatic.net/9QPEQ.png

Answer №1

To ensure flexibility with dynamic tabs, it is recommended to use activeIndex instead of selected. This approach is also endorsed by PrimeNG.

<button type="button" pButton icon="fa fa-chevron-up" (click)="openPrev()"></button>
<button type="button" pButton icon="fa fa-chevron-down" (click)="openNext()"></button>

<p-tabView [activeIndex]="index">
    <p-tabPanel header="Header 1">
        Content 1
    </p-tabPanel>
    <p-tabPanel header="Header 2">
        Content 2
    </p-tabPanel>
    <p-tabPanel header="Header 3">
        Content 3
    </p-tabPanel>
</p-tabView>

In the typescript file, you have the flexibility to set the active index value anywhere or within any method.

export class TabViewDemo {

    index: number = 0;

    openNext() {
        this.index = (this.index === 2) ? 0 : this.index + 1;
    }

    openPrev() {
        this.index = (this.index === 0) ? 2 : this index - 1;
    }

}

For more information on Programmatic Control, visit PrimeNG

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for validating the loading variable during testing of the mockservice in angular5

In the process of creating a test case suite for my application, I am faced with the challenge of verifying and validating the loading variable in my component spec file. The scenario involves calling an API service from my component to retrieve data, show ...

When the file is active on a local machine, the bot commands run smoothly. However, these commands do not execute on a remote

Lately, while working on coding a discord bot using discord.js, I came across an issue. Whenever I run my bot on my local machine, all the commands work perfectly fine. However, after committing and pushing the code to GitHub, and then allowing buddy.works ...

Issue encountered during Angular upgrade from version 2 to 4 due to a mismatch in node versions

Encountering an error while trying to run npm start: ERROR in Cannot read property 'getSymbolByModule' of undefined. Checked the node version in cmd using node -v, resulted in V6.11.1, however, when executing ng-v cmd, got: angular-cli: 1.0.0-be ...

Struggling to effectively organize data routing within Angular? Let's tackle the challenges of

As a newcomer to Angular, I initially had success with CRUD operations without using routing. However, after implementing routing, I encountered an issue where the added values were not displaying in the content table on another page. It seems like there ...

Having trouble retrieving information from the API in Angular

Error: There was a Forbidden error (403) when trying to access the URL . xyz.service.ts import { Injectable } from '@angular/core'; import { HttpErrorResponse } from "@angular/common/http"; import {Http, Response} from '@angular/http&ap ...

Testing Ag Grid's column headers using Jest and Angular CLI has proven challenging, as some columns from columnDefs remain elusive

Currently, I am using Jest and Angular Cli testing to validate column headers. In my attempt to replicate the process outlined on https://www.ag-grid.com/javascript-grid-testing-angular/#testing-grid-contents, I have encountered an issue where only 2 out ...

When a link containing an ID hash in the URL is opened in a new tab, the ID within the link

Is there a way to create a link that leads to http://localhost:4300/?id=RTnySsxr8T2lPIihu2LqMw==&lang=en-us Upon clicking the link, a new tab opens in the browser with that URL. The issue arises when the hashed ID in the address bar changes to http:/ ...

Guide to navigating to a specific route based on location in a Node.js Express application

Currently, I am integrating the official i18n library to localize my Angular Universal application and utilizing a proxy to deliver the localized versions. The app is functioning properly when there is a language specified in the URL (e.g: /en/page), but e ...

NextJS VSCode Typescript results in breakpoints becoming unbound

I have been following the instructions provided by Next.js from their official documentation on debugging using Visual Studio Code found here: https://nextjs.org/docs/advanced-features/debugging#using-the-debugger-in-visual-studio-code When attempting to ...

Creating a cutting-edge object using Angular 4 class - The power of dynamism

Attempting to create a dynamic object within a function, but encountering recognition issues. function1(object: object) { return new object(); } The function is invoked as follows: function1(Test) 'Test' represents a basic Class implementatio ...

What are the circumstances under which JavaScript GCP libraries return null values?

My current project involves working with GCP and Firebase using typescript. I have been utilizing the provided libraries, specifically version 8 of Firebase, and have encountered some unexpected behavior. For instance (firebase, ver. 8.10.1) import 'f ...

What is the alternative method for applying a CSS class in the click event in Angular 7 without relying on ng-Class or ng-Style?

Is there a way to dynamically add or remove CSS classes to HTML elements in Angular7 without relying on Ng-Style and Ng-Class directives? I'd like to achieve this functionality when clicking on the elements. Appreciate any insights you can provide. T ...

When the async keyword is added, the return type in Typescript can vary

This situation is really puzzling to me. I wrote a function to calculate the number of documents in a collection getDocCount(): Promise<number> { return MyModel.countDocuments({}); } Everything seemed fine. However, when I removed async since I ...

Display a pop-up directly below the specific row in the table

I am working on creating a table where clicking on a row will trigger a popup window to appear right below that specific row. Currently, the popup is displaying under the entire table instead of beneath the clicked row. How can I adjust my Angular and Bo ...

typescript exploring the versatility of dynamic types and generics

Understanding TypeScript dynamic and generic types can be challenging for me. The goal is to create a function that generates an object with a specific type, where some properties of the object must match the parameters provided to the function. Essentia ...

How to effectively filter a JSON array using multiple keys?

I need help filtering my JSON data by finding the objects with key 'status' equal to 'p' within the lease array. I attempted to use the following function, but it did not achieve the desired result: myActiveContractItems.filter((myActiv ...

Upon creation, SockJS and Stomp immediately terminate the connection

I am facing an issue with establishing WebSocket connection between Angular and Spring Cloud. The connection gets established but closes immediately after creation. I am not sure what might be causing this problem. Any insights? Here are the Angular depen ...

An informative step-by-step approach to constructing Angular applications utilizing npm and TypeScript

When I first encountered Angular2, I was introduced to TypeScript, npm, and more for the very first time. I was amazed by their power, but I know I've only scratched the surface. While I can navigate through the "development mode," my ultimate goal i ...

What external libraries does Angular 4 utilize during execution, aside from RxJS?

Angular 4 relies on RxJS types in its public API and also internally depends on RxJS. It would be beneficial to explore if Angular utilizes other external packages for certain functionalities, allowing us to incorporate them into our own projects. This ap ...

Ionic2: expanding menu options in the sidemenu

I'm not very familiar with ionic, but I have a question on behalf of my friend who is hesitant to ask on StackOverflow because she's unsure of how to frame her question. She simply wants to learn how to implement a submenu in an ionic 2 side men ...