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

Automatically upgrade packages to the latest version 12.0.0-next.0 with ng update

Recently, I've encountered an issue while updating my projects from Angular 10 to Angular 11. Whenever I run ng update, some packages are being upgraded to version 12.0.0-next.0 instead of the stable release. It seems like ng update is installing pre- ...

Accessing video durations in Angular 2

Can anyone help me with retrieving the video duration from a list of videos displayed in a table? I attempted to access it using @ViewChildren and succeeded until encountering one obstacle. Although I was able to obtain the query list, when attempting to a ...

Angular 5: Display a blank URL with the source until the variables are resolved

In my template, if I have: <img src="/somepath/{{user?.UserGuid}}.png" /> When user is not yet resolved, the ?. prevents evaluating UserGuid, resulting in: <img src="/somepath/.png" /> Is there a way to prevent this without using *ngIf or c ...

Combining conditions and CSS classes within the ngClass directive in Angular

Is it possible to combine a condition and a class within the same "ngClass" in Angular? [ngClass]="{cssclass1 : object.cssclass1status , object.cssclass2}" The condition (bold) and CSS class (italic) can be used together. Previously, I have implemented t ...

Using Typescript with Momentjs: 'String type cannot be assigned to Date type'

Up until now, my approach to utilizing the momentjs library was as follows: import * as moment from 'moment'; // import moment. @Component({..}); export class TestClass { lastUpdated = Date constructor(private myService: MyService){ this ...

Navigating to the detail page following a POST request in RTK query: A step-by-step guide

In my React RTK-query form, I am facing an issue where after a POST request is made, the form should navigate to the next step. However, in order to do that, I need to obtain the id of the newly created record. The backend auto-increments the id and does n ...

Pictures are shown using the "text/html" layout

I'm having trouble identifying the error and have decided to carefully examine both the client and server code. It seems likely that there is a bug somewhere in Rails. The issue revolves around the photos being displayed in text/html format. Here&apos ...

Convert all key types into arrays of that key type using a TypeScript utility type

My interface (type) is currently defined as: interface User { name: string, id: string, age: number, town: string } I have a function now that will search for Users based on specific fields. I prefer not to manually declare an additi ...

Utilizing Typescript and Jest to prevent type errors in mocked functions

When looking to simulate external modules with Jest, the jest.mock() method can be utilized to automatically mock functions within a module. After this, we have the ability to modify and analyze the mocked functions on our simulated module as needed. As ...

Nested Elements in Java JSON with Jackson

I have a JSON string that contains nested values. It looks something like this: "[{"listed_count":1720,"status":{"retweet_count":78}}]" I am interested in extracting the value of retweet_count. Currently, I am using Jackson to work with this data. The ...

Tips for transfering variables from an electron application to the backend of an Angular project

My goal is to develop a website and desktop application using the same code base. However, due to some minor differences between the two platforms, I need a way for my Angular app to distinguish whether it has been called from the web or from Electron. I& ...

Using Angular to include more than two parameters in an HTTP GET request

Currently, I am developing an application that requires downloading a file upon clicking a button within a template. The screen displays multiple files, each with its own corresponding button. I need to send the index number of the array to Angular and pas ...

The error message "The file 'environment.ts' is not located within the specified 'rootDir' directory" was encountered during the Angular Library build process

When attempting to access the environment variable in an Angular 9 library, I encountered an error during the library build process. Here is how it was implemented: import { EnvironmentViewModel } from 'projects/falcon-core/src/lib/view-models/envir ...

Utilizing mouse movements to control the scroll bar within a dropdown menu when using Selenium WebDriver

I recently started learning about Selenium and have been focusing on using Selenium Webdriver. One challenge I've encountered is dealing with a large drop-down list where I need to scroll down using only the mouse click. My question is, "Is there a wa ...

Bringing in SCSS using Typescript, React, and Webpack

I am trying to utilize .scss classes by importing them and applying them to the className property of a React component. Here is the structure of my project : root/ ... config/ ... webpack.config.js src/ ... global.d.ts app/ ...

The issue here pertains to npm's inability to successfully retrieve a required dependency for download

C:\Users\Manoj\Desktop\accounts>npm install intro.js --save npm ERR! code ENOENT npm ERR! syscall spawn git npm ERR! path git npm ERR! errno ENOENT npm ERR! enoent Error while executing: npm ERR! enoent undefined ls-remote -h -t ssh: ...

Efficiently Parsing FormData in React with TypeScript for Improved Data Formatting

Recently, I've started working with React and Typescript and one of the challenges I'm facing is managing an editable table with default values that can be updated and submitted. The data format for the parameters is crucial as the fields and va ...

Ways to Execute the Constructor or ngOnInit Multiple Times

Here's the scenario I'm facing: I have developed an app with multiple screens. One of these screens displays a list of articles. When a user clicks on an article, they are directed to another screen that shows the details of that specific item. ...

transferring information seamlessly in Ionic using Angular router without the need for navigation

I have a list of names that are displayed, and when I click on any name in the list, it should take me to another page without actually navigating to that path. names.page.html: <ion-content> <ion-list *ngFor='let person of people'&g ...

Change a nested for-loop into an Observable that has been transformed using RxJS

Currently, the following function is operational, but I consider it a temporary solution as I'm extracting .value from a BehaviorSubject instead of maintaining it as an observable. Existing Code Snippet get ActiveBikeFilters(): any { const upd ...