简体   繁体   English

PrimeNG - 如何在 p-tabView 组件中动态添加和删除 p-tabPanel

[英]PrimeNG - How to dynamically add and remove a p-tabPanel in p-tabView component

I'm on a project where I need to use a p-tabView.我在一个需要使用 p-tabView 的项目中。 A new p-tabPanel is to be created when clicking on a button in the p-tabView.单击 p-tabView 中的按钮时,将创建一个新的 p-tabPanel。

How can I dynamically add new panels to a p-tabView?如何将新面板动态添加到 p-tabView?

I figured out a way.我想了个办法。 I am using ngFor to control the # of tabPanel.我正在使用 ngFor 来控制 tabPanel 的数量。

My component.ts code will manipulate myModel.leads.我的 component.ts 代码将操作 myModel.leads。 Thus, the number of tabPanel will change accordingly.因此,tabPanel 的数量会相应改变。

<!--Lead-->
<p-tabView  >  
  <p-tabPanel [selected]="i==0" *ngFor="let lead of procedureDetail.leads; let i = index" header="lead {{i+1}}"  >
           <div id="{{lead.id}}" class="my_dynamica_lead_tabPanel"> <!-- I can add content to this div with tabView.onChange() api -->
          {{lead.id}} - {{i}}
           </div>
           <div class="row">
              <div class="col-md-3">
                 Date:
              </div>
           <div class="col-md-9">
              <p-calendar [style]="{'width':'180px'}" [monthNavigator]="true" [yearNavigator]="true" yearRange="1900:2020" name="lead.{{i}}.date" [(ngModel)]="lead.date" [showIcon]="true" ></p-calendar> 
          </div>
       </div>

   </p-tabPanel>
</p-tabView> 

Note - If you forget to include [selected] attribute while using *ngFor in p-tabPanel.注意 -如果在 p-tabPanel 中使用 *ngFor 时忘记包含 [selected] 属性。 You will get following error你会得到以下错误

TabPanel.html:2 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'none'. Current value: 'block'.

I just found a possible sollution for this problem.我刚刚为这个问题找到了一个可能的解决方案。 Making a list of object type of TabPanel.制作 TabPanel 的对象类型列表。

tabs: TabPanel = [{header: 'tabbdfd', selected : false, disabled : false ,closable : true, closed : false}, {header: '3333', selected : false, disabled : false ,closable : true, closed : false}];

The component looks like:该组件看起来像:

<p-tabView>  
     <p-tabPanel *ngFor="#tab of tabs" [header]="tab.header" >
            <CONTENT>   
    </p-tabPanel>
</p-tabView> 

Maybe will be usefull for somebody :)也许对某人有用:)

你可以像下面这样做:

<ng-template pTemplate="header"> Header Text </ng-template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM