1

p-tabMenu对于禁用的菜单,我对 PrimeNG 有疑问。

例如,我有一个带有 4 个子选项卡的 tabMenu -> AAA、BBB、CCC、DDD。

这就是 ts 组件中 menuItems 的设置方式。

//....

invDocs: InventoryDocs[] = [];
invMenu: InventoryDocs[] = [];
this.invMenu = this.data.cdMenu;
this.invDoc = this.data.cdDocs;
this.menuItems = []; 
if(this.invMenu != null && this.invMenu.length > 1){
    this.showMenu = true;
    for (let i = 0; i < this.invMenu.length; i++) {                  
        this.menuItems.push({label: this.invMenu[i].fileDescr, id:  this.invMenu[i].menuId, disabled: this.invMenu[i].subCount > 0});
        this.activeItem = this.menuItems[0];
    }
    this.currentPdf = this.invDoc.docBlob;
}
                
            


activateMenu(tab){ 
    console.log(tab);
    this.invDocId = tab.activeItem.id;
    this.showMenu = true;
    this.retriveCurrentPdf(this.invDocId);
}           
                
.....//

推送的样本值:

this.menuItems.push({lable: 'AAA', id: 1, disabled: false});
this.menuItems.push({lable: 'BBB', id: 1, disabled: true});
this.menuItems.push({lable: 'CCC', id: 1, disabled: true});
this.menuItems.push({lable: 'DDD', id: 1, disabled: false});

默认情况下,“AAA”设置为活动。

我的组件 html 如下所示:

<div style="width: 3em;">
       <p-tabMenu #tab [model]="menuItems" [activeItem]="activeItem" (click)="activateMenu(tab)" class="customWrapper" ></p-tabMenu> 
</div> 

该页面使用 4 个选项卡呈现,其中启用了 AAA 和 DDD,禁用了 BBB、CCC。选项卡上的点击事件调用该activateMenu方法并在 UI 中显示 diff pdf。

问题在于这个点击事件,它也被禁用的按钮触发。即使选项卡 BBB、CCC 被禁用,它也可以让我单击选项卡,但选项卡#中的 activeItem 保留了以前活动的内容,因此我无法停止事件传播。说页面加载时默认为 AAA 选项卡。现在,即使 BBB 被禁用,它也可以让我单击选项卡“BBB”,当我在 labelconsole.logactivateMenu()的 activeItem 中打印时,id 显示选项卡 AAA 的。有人可以建议我如何防止点击禁用标签吗?

4

1 回答 1

1

我认为您应该使用command属于MenuItem. 仅在禁用选项卡时,才会在单击上触发此功能。

this.items = [
  {
    label: "Home",
    icon: "pi pi-fw pi-home",
    command: event => {
      this.activateMenu(event);
    }
  },
  {
    label: "Edit",
    icon: "pi pi-fw pi-pencil",
    disabled: true,
    command: event => {
      this.activateMenu(event); // this one won't be triggered because tab is disabled
    }
  }
]

activateMenu(event) {
    console.log("click on " + event.item.label + " tab!");
}

演示

于 2020-12-08T07:31:52.380 回答