1

I'm trying to use PrimeNg TabView component along with confirmDialog unsuccessfully, here is my code:

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


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

Do you have an idea on how to prevent or allow tab change using confirm dialog ?

Thanks

Zakariaa.B
  • 133
  • 8

2 Answers2

4

Based on similar solution for material design tabs, here is the solution for my issue:

  1. in html Declare a local variable referencing TabView DOM object:

    <p-tabView #onglets>...</p-tabView>

  2. in component.ts, change default function called when click on tab with specific function to match your case:

    @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); }); }

Zakariaa.B
  • 133
  • 8
1

I had similar problem. Needed show dialog before tab change.
My solution:

HTML

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

TS

@ViewChild('tabView') tabView: TabView;

  onChange(event: any) {

    const previoustab = this.tabView.tabs[this.prevIndex]; //saved previous/initial 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]);
  }

With this code you will stay on the selected tab without tab style changes.

greybeard
  • 2,249
  • 8
  • 30
  • 66
Wookie
  • 11
  • 2