在Prime NG的tabview文档的可关闭部分中,我看到当我删除最后一个选项卡时,它会转到第一个选项卡.我的问题是:我如何才能让它转到左侧的选项卡而不是第一个选项卡? (我猜这是一种​​的值为0和-1的情况).当更改TabIndex的值时,有没有办法使选项卡位于关闭的选项卡的左侧,而不是第一个选项卡? 示例行为应该如下所示:设有选项卡1、2和3.当我关闭编号3时,我希望显示编号2).先谢谢你.

Example from geeks for geeks个 我想像这样做,但我不知道它实际上是如何工作的?

以下是我的一些源代码:

<div  style="float: left;" [style.width] ="this.splitWindows ? '50%' : '100%'">
                  <p-tabView  (onClose)="handleCloseLeft($event)" [scrollable]="isScrollable" (click)="consolelog(activeTabIndex)" (onChange)="handleOnChange($event)" [(activeIndex)]="activeTabIndex"] >
                    <p-tabPanel *ngFor="let item of leftTabItems; let i = index" [selected]="i == activeTabIndex"  [closable] = "item.closable">
                      <ng-template pTemplate="header">
                        <div #tabPanelRightClick>
                          {{item.header}}
                        </div>
                        <p-contextMenu appendTo="body" [target]="tabPanelRightClick" [model]="items"></p-contextMenu>
                      </ng-template>
                      <ng-content *ngComponentOutlet='item.component.component; inputs:item.params'/>
                    </p-tabPanel>
                  </p-tabView>
                </div>

活动选项卡索引的值为​​,如0、1、2、3、4.而我预计的行为是,当第四个activeTabIndex关闭时,第三个activeTabIndex出现在屏幕上,但它似乎转到了我的主页所在的第0个索引.(这是无论它是什么指数.它的工作原理如下.)

HandleCloseLeft的代码如下:

handleCloseLeft(event): void {
this.tabService.removeTabItem(event.index);

console.log("event index'i  : " + event.index);
console.log(this.leftTabItems.length);
// this.activeTabIndex = 0;
if (event.index < this.leftTabItems.length) {
   this.activeTabIndex = event.index;
 } else {
   this.activeTabIndex = this.leftTabItems.length - 1;
 }

this.leftTabItems.splice(event.index, 1);

// if(event.index > this.activeIndex) {
//   this.activeIndex = event.index - 1;
// }

}

推荐答案

如果我们更改活动索引,Prime-ng中有一些错误,它不会以编程方式更新当前选项卡.

你可以在他们的GitHub上发布一个错误来修复它.作为临时解决方案,请使用以下解决方案!请通过下面的闪电战,让我知道如果有任何疑问!

ts

import { Component, ViewChild } from '@angular/core';
import { TabView } from 'primeng/tabview';

@Component({
  selector: 'tab-view-closable-demo',
  templateUrl: './tab-view-closable-demo.HTML',
})
export class TabViewClosableDemo {
  @ViewChild('tabView') tabView: TabView;
  activeTabIndex = 0;
  tabArray = [1, 2, 3, 4, 5];

  handleCloseLeft(event: any) {
    console.log(event);
    this.tabArray.splice(event.index, 1);
    this.activeTabIndex = this.tabArray.length - 1;
    this.tabView.cd.detectChanges(); // = this.activeTabIndex;
  }
}

HTML

<div class="card">
  <p-tabView
    #tabView
    id="closableTabView"
    (onClose)="handleCloseLeft($event)"
    [(activeIndex)]="activeTabIndex"
  >
    <p-tabPanel
      header="Header I"
      [selected]="first"
      *ngFor="let tab of tabArray; let i = $index;let  first = $first"
      [closable]="true"
    >
      <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
        veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
        commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
        velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
        occaecat cupidatat non proident, sunt in culpa qui officia deserunt
        mollit anim id est laborum.
      </p>
    </p-tabPanel>
  </p-tabView>
</div>

stackblitz

Angular相关问答推荐

Angular 服务错误处理响应类型=BLOB

带有搜索功能的CDK-VIRTUAL-SCROLL-VIEPORT不显示该值

如何将管道异步化添加到不在html模板中的观察对象的Angular ?

NGNEW不会在src根文件夹ANGLI CLI 17.0.10中生成app.mode.ts

@ngrx/调度时未定义存储操作

无法匹配任何路由.URL段:';英雄';

如何在AngularJs中剪断细绳

Ngrx Select 器返回部分对象

一次重定向后,所有子路由都处于活动状态

Summernote 文本区域的输入字段文本验证失败

Observable .do() 运算符 (rxjs) 的用例

模块AppModule导入的意外指令LoginComponent,请添加@NgModule 注释

取消订阅服务中的http observable

找不到模块'@angular/compiler'

由于时区,Angular 4 日期管道显示错误的日期 - 如何解决这个问题?

Angular 2中基于Condition条件的点击事件

Angular2 中的providers提供者是什么意思?

angular2 测试,我如何模拟子组件

Angular2:无法读取未定义的属性名称

declarations和entryComponents组件有什么区别