TabView with Confirmation Popup from PrimeNg

I'm having trouble making the PrimeNg TabView component work with a confirmDialog. Here's my code:

<p-tabView (onChange)="onTabChange($event)" [(activeIndex)]="index">...</p-tabView> 


  onTabChange(event){
    this.confirmationService.confirm({
      message: 'Do you want to proceed?',
      accept:  () => {
      this.index = event.index;
      },
      reject:() =>{        }
      });
   }

Any suggestions on how to handle tab change confirmation using a dialog box?

Thanks

Answer №1

Referencing a similar solution for material design tabs, I found the resolution to my problem:

  1. In your HTML file, declare a local variable that references the TabView DOM object:
<p-tabView #onglets>...</p-tabView>
  1. In the component.ts file, modify the default function triggered when clicking on a tab with a specific function that suits your requirements:
@ViewChild('onglets') onglets: TabView;
this.onglets.open = this.interceptOngletChange.bind(this);
...
interceptOngletChange(event: Event, tab: TabPanel){
  const result = confirm(`Do you really want to leave the tab?`);
  return result && TabView.prototype.open.apply(this.onglets, argumentsList);
});

Answer №2

I encountered a similar issue where I needed to display a dialog before changing tabs.
Here is my solution:

HTML

<p-tabView #tabView (onChange)="onChange($event)" />

TypeScript

@ViewChild('tabView') tabView: TabView;

  onChange(event: any) {

    const previousTab = this.tabView.tabs[this.prevIndex]; // storing the previous index
    previousTab.selected = true;
    const selectedTab = this.tabView.tabs[event.index];
    selectedTab.selected = false;
    this.tabView.activeIndex = this.prevIndex;
    this.nextIndex= event.index;
  }

  GoToNextTab() {

    this.tabView.activeIndex = this.nextIndex;
    this.prevIndex= this.nextIndex;
    this.tabView.open(undefined, this.tabView.tabs[this.nextIndex]);
  }

By using this code, you can switch tabs without altering the tab styles.

Answer №3

I found the accepted solution to be effective, but there are a few additional steps needed for it to function properly.

First, make sure to add the initialization section in the afterViewInit method.

Next, ensure that the argumentsList is configured as follows: [event, tab]

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 is the best way to animate a 3D render based on scrolling with JavaScript?

My idea is to create a captivating animated 3D render that changes on scroll, much like the one featured on Apple's website. After inspecting the elements, it seems that this effect is achieved by using numerous images, each representing a specific fr ...

Unable to locate an element on the webpage due to a JavaScript-based error, which then becomes hidden after a few seconds. (Registration form)

While completing a registration form, I encounter a hidden message after clicking on the register button. Struggling to locate this elusive element has been an ongoing challenge for me. Unfortunately, my attempts to find the element have been unsuccessful ...

Node.js is throwing an HPE_INVALID_CONTENT_LENGTH error when a HEAD request is made

When using the request module, I encountered an error while making HEAD requests to shortened URLs that redirect with a 301 status code: { [Error: Parse Error] bytesParsed: 123, code: 'HPE_INVALID_CONTENT_LENGTH' } For instance, this error occu ...

What is the best way to choose all checkboxes identified by a two-dimensional array?

I need help with a question div setup that looks like this: <div class="Q"> <div id="Q1"><span>1. </span>Which of the following have the same meaning?</div> <div class="A"><input type="checkbox" id="Q1A1Correct" /& ...

Error TS2339 occurs when attempting to migrate to TypeScript due to the absence of the 'PropTypes' property on the 'React' type

Currently in the process of converting a javascript/react project to a typescript/react/redux project. Encountering an issue with this particular file: import React from 'react'; import GoldenLayout from 'golden-layout'; import {Provi ...

Ensure the clarity tabs labels have the padding-left CSS property added

In my project, there are 2 clarity tabs (you can view the StackBlitz reference here). https://i.sstatic.net/jhLcn.png I want to apply the padding-left property specifically to Tab1 (the tab label itself, not the content) in order to create some space aro ...

Determine whether the last day of the month falls within the designated time frame, and

In my Vue JS application form, I am facing an issue related to the pay frequency <select>. To give some context about the situation, let me explain further. The application form includes a <select> box with options for users to choose their ne ...

Transform a string containing square brackets and double quotes into an array

How can I transform the following string into an array with individual values? ["X", "Y", "Z"] The output should be like: arr[0] // X arr[1] // Y arr[2] // Z ...

I would like a div element to slide up from the bottom of the page

Could someone please assist me in creating a popup div that appears from bottom to top when a specific button is clicked? I would like the div to be fixed without affecting the overall page content. Any help would be greatly appreciated. Thank you! $( ...

A guide to displaying SVG graphics in a Jupyter notebook with the help of jsdom, D3, and IJavascript

Although I'm not an expert in frontend development, I have been experimenting with Javascript and D3 lately. As someone who typically works on scientific analysis in Python using Jupyter Notebooks, I thought it would be interesting to apply a similar ...

What could be causing this ajax request to return a 400 error?

I'm encountering a persistent 400 error and can't seem to figure out why. I've tried multiple approaches, but the error persists when using the request through admin-ajax.php. JQUERY: function handle_ajax_request(){ if(is_page(1404)){ ?> ...

Utilize a helper function for all MongoDB/Meteor collections

Discover a library that offers the ability to link helpers to collections in this manner: Meteor.users.helpers({ profile: function() { return Profiles.findOne(this.profileId); } }); This method works well for established collections, but my goal ...

Using an iframe to access HTTP content from an HTTPS website

Currently, I am facing an issue with a page that contains an iframe loading an HTML template from an Amazon AWS S3 bucket. Within the iframe, my goal is to take the link of the parent window and add additional parameters. For example, let's say my pa ...

Value in the loop not being computed accurately

Currently in a loop, I am trying to calculate the sum of var packprice, var packprices, and var ala. Although I am able to retrieve all values in the console, the addition operation for tprice seems to be failing. Can someone please provide assistance wi ...

When using navigator.mediaDevices.getUserMedia on an iPhone, the webcam is activated in fullscreen mode

I'm facing an issue with the webcam functionality on my app. It works perfectly on Android and Windows, but when I try to use it on iPhone, the webcam opens in a separate full-screen view. Any ideas on how to resolve this? Thank you for your help in a ...

A guide on extracting all form fields with the maxlength attribute using JavaScript

Currently, I am utilizing html5 for the creation of my views and encountered an issue with the maxlength attribute in a few form fields. While maxlength is functioning correctly in desktop browsers, it seems to not be effective in android browsers. As a r ...

I encountered an error while attempting to lint my code because ESLint was not configured to ignore the

Trying to set up eslint in a Next.js project, here is the file structure: .eslintignore .eslintrc.js .next .prettierrc .stylelintrc.js components forms modals modules node_modules In my .eslintignore file, I have added node_modules/*. When running eslint ...

"Utilize AngularJS JavaScript to nest HTML elements within each other for dynamic web

I have developed a unique custom directive called hero. My goal is to set up a nested view for multiple instances of hero. Check out this demo to see it in action. The desired layout looks something like this: <hero a="1"> <hero a="2"> ...

Sending data from multiple HTML table rows at once to a Django model

After a user selects items from a list, they can submit the selected items to be saved in a table. The table is dynamically rendered using JavaScript and the data comes from a Map with keys as primary keys and values as descriptions and prices. function d ...

Utilize JavaScript variables with jQuery: A simple guide

I need assistance with adding a class after selecting the checkbox. The variable values come from a database using PHP. var a = user<?php echo $i; ?>; Although this displays the correct value, I am struggling to pass it to jQuery in order to add the ...