Prevent click events on disabled tabs in PrimeNG tabMenu

I am encountering an issue with PrimeNG's p-tabMenu when it comes to disabled menu items.

For example, suppose I have a tabMenu with 4 sub tabs labeled AAA, BBB, CCC, and DDD.

This is how the menuItems are set up in the TypeScript component:

//....

invDocs: InventoryDocs[] = [];
invMenu: InventoryDocs[] = [];
this.invMenu = this.data.cdMenu;
this.invDoc = this.data.cdDocs;
this.menuItems = []; 
if(this.invMenu != null && this.invMenu.length > 1){
    this.showMenu = true;
    for (let i = 0; i < this.invMenu.length; i++) {                  
        this.menuItems.push({label: this.invMenu[i].fileDescr, id:  this.invMenu[i].menuId, disabled: this.invMenu[i].subCount > 0});
        this.activeItem = this.menuItems[0];
    }
    this.currentPdf = this.invDoc.docBlob;
}
                
            


activateMenu(tab){ 
    console.log(tab);
    this.invDocId = tab.activeItem.id;
    this.showMenu = true;
    this.retriveCurrentPdf(this.invDocId);
}           
                
.....//

Here is a sample value pushed:

this.menuItems.push({lable: 'AAA', id: 1, disabled: false});
this.menuItems.push({lable: 'BBB', id: 1, disabled: true});
this.menuItems.push({lable: 'CCC', id: 1, disabled: true});
this.menuItems.push({lable: 'DDD', id: 1, disabled: false});

'AAA' is set as active by default.

The HTML of my component looks like this:

<div style="width: 3em;">
       <p-tabMenu #tab [model]="menuItems" [activeItem]="activeItem" (click)="activateMenu(tab)" class="customWrapper" ></p-tabMenu> 
</div> 

The page displays 4 tabs where AAA and DDD are enabled while BBB and CCC are disabled. The issue arises when clicking on disabled tabs triggering the click event, although the activeItem property remains unchanged, leading to unexpected behavior. How can I prevent clicks on disabled tabs?

Answer №1

It would be more suitable to utilize the command function which is a part of MenuItem. This function will only be activated upon click if the tab is not disabled.

this.items = [
  {
    label: "Home",
    icon: "pi pi-fw pi-home",
    command: event => {
      this.activateMenu(event);
    }
  },
  {
    label: "Edit",
    icon: "pi pi-fw pi-pencil",
    disabled: true,
    command: event => {
      this.activateMenu(event); // this one won't be triggered because tab is disabled
    }
  }
]

activateMenu(event) {
    console.log("click on " + event.item.label + " tab!");
}

Check out the demo

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

What could be the reason for the webpack:// not showing up in Chrome after running ng build?

Greetings, I'm relatively new to Angular and have noticed a difference between using ng serve and ng build. When I use ng serve, I can see webpack:// in the Chrome debugger which allows me to navigate my TypeScript files for debugging. However, when I ...

Error: Incorrect data type found in React Route component

I've encountered an issue while attempting to utilize the Route component in my application. The error message I'm receiving reads as follows: [ts] Type '{ path: "/:shortname"; component: typeof FirstComponent; }' is not assignable ...

Trouble arises from the object data type not being properly acknowledged in TypeScript

In the code snippet provided, I am facing a challenge where I need to pass data to an if block with two different types. These types are handled separately in the if block. How can I make TypeScript understand that the selected object could be either of ty ...

Angular Drag and Drop with Multiple Items (ng2-dragula)

Currently searching for a drag and drop library that can handle multiple drag capabilities. Unfortunately, none have been found specifically for Angular 2+. While ng2-dragula does meet some of our requirements, it falls short when it comes to supporting ...

The element in the iterator in next.js typescript is lacking a necessary "key" prop

Welcome to my portfolio web application! I have created various components, but I am facing an issue when running 'npm run build'. The error message indicates that a "key" prop is missing for an element in the iterator. I tried adding it, but the ...

Could not generate route route1

I'm currently facing an issue with this exception and I can't figure out where the error lies. Here's the Exception message: org.apache.camel.spring.boot.CamelSpringBootInitializationException: java.lang.RuntimeException: org.apache.camel.F ...

What are the reasons behind the restriction on using non-public members in TypeScript classes?

Consider the following scenario: class Trait { publicMethod() { this.privateMethod(); // do something more } private privateMethod() { // perform a useful action } } When attempting to implement it like this: cla ...

What is the best approach to resolve CORS issue for an Angular application hosted on Heroku that is connected to a Postgres express backend?

I am currently in the process of deploying my Angular application along with a Postgres Express backend on Heroku to establish communication between them. However, I have encountered a major issue where whenever I attempt to utilize fetch methods, it promp ...

Tips on pre-filling a form using ngModel bindings

I have designed a Modal page where I aim to display pre-populated data to the user. Whenever the user edits the data, it should be bound to my local variable collectDataInModalPage_afterEdit. Check out the demo here:: https://stackblitz.com/edit/ionic-jse ...

Is Webpack CLI causing issues when trying to run it on a .ts file without giving any error

I am facing an issue with my webpack.config.js file that has a default entrypoint specified. Here is the snippet of the configuration: module.exports = { entry: { main: path.resolve('./src/main.ts'), }, module: { rules: [ { ...

``Issue encountered when attempting to create a custom format for a `java.time.LocalDate

Is there a way to convert the date format YYYY-MM-DD into MMM dd, yyyy? I'm running into issues with my current code that is resulting in a DateTimeParseException: DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("MMM dd, yyyy& ...

Sending the value of "username" between two components within Angular 2

I have a good understanding of nesting child components within parent components in Angular 2, but I'm a bit unclear on how to pass a single value from one component to another. In my scenario, I need to pass a username from a login component to a cha ...

Step-by-step guide to integrating Google AdSense ads.txt file into an Angular project

If you're experiencing problems with Google AdSense in your Angular project, it could be related to how static files are served within Angular and handled by servers. Let's go through the necessary steps to ensure that your ads.txt file is proper ...

Whenever I try to import a function, I encounter the error message "no exported member."

I am facing an issue with my node/typescript application where I am attempting to import a function from another file. In order to export it, I utilized exports.coolFunc = coolFunc, and for importing, I used import {coolFunc} from '../controller/coolS ...

What is the best way to prevent images from being loaded with broken links?

Currently, I am working on a new feature that involves rendering images from servers. In the process of aligning these images, I noticed an excessive amount of white space due to loading images with broken links. https://i.stack.imgur.com/jO8BR.png Here ...

Angular setup prompting for anonymous usage statistics collection by the Angular development team

This is my first time installing Angular and I ran into confusion at this step. The terminal message prompted me with the option to share anonymous usage data with the Angular Team at Google under their Privacy Policy. Here's what it said: ? Would y ...

Unlocking the full potential of Ionic 4 with push notifications, omitting

Currently, I am encountering an issue where I need to disable the display of notification alerts (local notification) while the application is running in the foreground on both Android and iOS. I only want to capture the push notification in the code witho ...

Discovering the second value with a matching CSS class using Selenium WebDriver

How can I select the second value from a list item that appears as a drop-down on a web page? In this case, the second value is 'Bellevue'. Refer to the screenshot below: I am implementing Page Objects and trying to locate elements using @FindBy ...

Selenium customer refuses to shut down

Recently, we updated Selenium from version 0.9.x to 2.17 without making changes in the code to use the new web driver. As a result, we are still using: BrowserConfigurationOptions options = new BrowserConfigurationOptions(); options.setSingleWindow(); opt ...

Creating a custom currency input directive for ion-input: A step-by-step guide

Update: Initially, I believed the issue lied within the implementation of the ControlValueAccessor, but later discovered that the problem was related to applying the ControlValueAccessor to child elements. The question has been edited to reflect this. I a ...