how to call a function on closing a tab in primeng tab component, p-tabPanel, p-tabView in Angular

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><p-tabView [controlClose]="true">
<p-tabPanel *ngFor="let tab of tabs"
[header]="tab.actionName"
(click)="selectTab(tab)"
[selected]="tab.selected"
(onClose) = "close(tab)"
[closable]="tab.closable">
</p-tabPanel>
</p-tabView>
<ng-container *ngIf="selectedTab">
<ng-container *ngComponentOutlet="selectedTab.actionComponent; inputs: selectedTab.inputs"></ng-container>
</ng-container>
</code>
<code><p-tabView [controlClose]="true"> <p-tabPanel *ngFor="let tab of tabs" [header]="tab.actionName" (click)="selectTab(tab)" [selected]="tab.selected" (onClose) = "close(tab)" [closable]="tab.closable"> </p-tabPanel> </p-tabView> <ng-container *ngIf="selectedTab"> <ng-container *ngComponentOutlet="selectedTab.actionComponent; inputs: selectedTab.inputs"></ng-container> </ng-container> </code>
<p-tabView [controlClose]="true">
    <p-tabPanel *ngFor="let tab of tabs"
                [header]="tab.actionName"
                (click)="selectTab(tab)"
                [selected]="tab.selected"
                (onClose) = "close(tab)"
                [closable]="tab.closable">
    </p-tabPanel>
  </p-tabView>
  
  <ng-container *ngIf="selectedTab">
    <ng-container *ngComponentOutlet="selectedTab.actionComponent; inputs: selectedTab.inputs"></ng-container>
  </ng-container>

i want to call a funtion to perform some action whenever i close some tab, unfortunately the onclose is not working on p-tabPanel

I recently faced the same issue, the problem is the (onClose) does not work on p-tabPanel, it works on p-tabView only so we need to use the (onClose) on p-tabView only and pass an Event the event gives the index of tab to be removed and then we can remove the tab in our .ts code, i am providing code snippet below for better understanding

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><p-tabView (onClose)="onTabClose($event)" [controlClose]="true">
<p-tabPanel *ngFor="let tab of tabs; let i = index"
[header]="tab.title"
[closable]="true">
<!-- Tab Content -->
<ng-container *ngIf="selectedTab === tab">
<!-- Display content for the selected tab -->
</ng-container>
</p-tabPanel>
</p-tabView>`
</code>
<code><p-tabView (onClose)="onTabClose($event)" [controlClose]="true"> <p-tabPanel *ngFor="let tab of tabs; let i = index" [header]="tab.title" [closable]="true"> <!-- Tab Content --> <ng-container *ngIf="selectedTab === tab"> <!-- Display content for the selected tab --> </ng-container> </p-tabPanel> </p-tabView>` </code>
<p-tabView (onClose)="onTabClose($event)" [controlClose]="true">
  <p-tabPanel *ngFor="let tab of tabs; let i = index"
              [header]="tab.title"
              [closable]="true">
    <!-- Tab Content -->
    <ng-container *ngIf="selectedTab === tab">
      <!-- Display content for the selected tab -->
    </ng-container>
  </p-tabPanel>
</p-tabView>`
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { Component, OnInit } from '@angular/core';
import { TabViewCloseEvent } from 'primeng/tabview';
@Component({
selector: 'app-tab-example',
templateUrl: './tab-example.component.html',
styleUrls: ['./tab-example.component.css']
})
export class TabExampleComponent implements OnInit {
tabs = [
{ title: 'Tab 1', content: 'Content 1' },
{ title: 'Tab 2', content: 'Content 2' },
{ title: 'Tab 3', content: 'Content 3' }
];
selectedTab = this.tabs[0]; // Initially select the first tab
ngOnInit(): void {}
onTabClose(event: TabViewCloseEvent) {
const index = event.index; // Get the index of the tab to close
this.tabs.splice(index, 1); // Remove the tab from the list
// Select a new tab if the closed tab was the selected one
if (this.selectedTab === this.tabs[index]) {
this.selectedTab = this.tabs.length ? this.tabs[0] : null; // Select a new tab or clear selection
}
}
}
</code>
<code>import { Component, OnInit } from '@angular/core'; import { TabViewCloseEvent } from 'primeng/tabview'; @Component({ selector: 'app-tab-example', templateUrl: './tab-example.component.html', styleUrls: ['./tab-example.component.css'] }) export class TabExampleComponent implements OnInit { tabs = [ { title: 'Tab 1', content: 'Content 1' }, { title: 'Tab 2', content: 'Content 2' }, { title: 'Tab 3', content: 'Content 3' } ]; selectedTab = this.tabs[0]; // Initially select the first tab ngOnInit(): void {} onTabClose(event: TabViewCloseEvent) { const index = event.index; // Get the index of the tab to close this.tabs.splice(index, 1); // Remove the tab from the list // Select a new tab if the closed tab was the selected one if (this.selectedTab === this.tabs[index]) { this.selectedTab = this.tabs.length ? this.tabs[0] : null; // Select a new tab or clear selection } } } </code>
import { Component, OnInit } from '@angular/core';
import { TabViewCloseEvent } from 'primeng/tabview';

@Component({
  selector: 'app-tab-example',
  templateUrl: './tab-example.component.html',
  styleUrls: ['./tab-example.component.css']
})
export class TabExampleComponent implements OnInit {
  tabs = [
    { title: 'Tab 1', content: 'Content 1' },
    { title: 'Tab 2', content: 'Content 2' },
    { title: 'Tab 3', content: 'Content 3' }
  ];
  selectedTab = this.tabs[0]; // Initially select the first tab

  ngOnInit(): void {}

  onTabClose(event: TabViewCloseEvent) {
    const index = event.index; // Get the index of the tab to close
    this.tabs.splice(index, 1); // Remove the tab from the list

    // Select a new tab if the closed tab was the selected one
    if (this.selectedTab === this.tabs[index]) {
      this.selectedTab = this.tabs.length ? this.tabs[0] : null; // Select a new tab or clear selection
    }
  }
}

In PrimeNG, when you click the close icon on a tab (using p-tabPanel inside p-tabView), the following happens:

The onClose event is triggered.
The event provides information about which tab was clicked.
You handle the event by removing the tab from the list of tabs.
The p-tabView component updates the UI to reflect the removal of the tab.
This process allows users to dynamically close tabs, and the p-tabView component ensures the view remains consistent with the updated list of tabs.

Hope, this helps!!

New contributor

Keshav Vats is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật