如何在Angular 2.0中以编程方式触发PrimeNG Accordion Click? [英] How to trigger PrimeNG Accordion Click programmatically in Angular 2.0?

查看:106
本文介绍了如何在Angular 2.0中以编程方式触发PrimeNG Accordion Click?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个手风琴,如下所示。

I have an accordion as shown below.

单击链接时,我需要触发所有p-accordianTab元素的click事件。

When the link is clicked, I'd need to trigger the click event of all p-accordianTab elements.

怎么可能?

<a (click)="openCloseAll()" >{{openCloseAllText}} all</a>
                <p-accordion [multiple]="true">
                    <div class="row" *ngFor="let category of result.Categories">                        

                            <p-accordionTab #accordianTab header="{{category.Name}}">

                            </p-accordionTab>                       

                    </div>
                </p-accordion>

我尝试将此 #accordionTab添加到元素中并从TypeScript访问它,但是不起作用:

I tried adding this "#accordionTab" to the element and accessing it from TypeScript but doesn't work:

@ViewChild('accordionTab') accordionTab: ElementRef;
 openCloseAllText: string = "Open";
 openCloseAll() {
        // get all accordions and click them
        this.openCloseAllText = this.openCloseAllText === "Open" ? "Close" : "Open";
        this.accordionTab.nativeElement.click();
    }

TypeError: Cannot read property 'nativeElement' of undefined


推荐答案

为什么不只使用手风琴本身的制表符属性?

Why not just use the tabs-property of the accordion itself?

<p-accordion #accordion>
    <p-accordionTab header="Header Content">
         Body Content
    </p-accordionTab>
</p-accordion>

@ViewChild('accordion') accordion: Accordion;

closeAllAccordionTabs() {
    if(!isNullOrUndefined(this.accordion.tabs)){
        for(let tab of this.accordion.tabs) {
            if(tab.selected) tab.selected = false;
        }
    }
}

openAllAccordionTabs() {
    if(!isNullOrUndefined(this.accordion.tabs)){
        for(let tab of this.accordion.tabs) {
            if(!tab.selected) tab.selected = true;
        }
    }
}

这篇关于如何在Angular 2.0中以编程方式触发PrimeNG Accordion Click?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆