如何触发PrimeNG Accordion在Angular 2.0中以编程方式单击?

时间:2017-03-20 11:02:31

标签: angular accordion primeng

我有一支手风琴如下所示。

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

怎么可能?

<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>

我尝试添加这个&#34;#accordionTab&#34;到元素并从TypeScript访问它但不起作用:

@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

6 个答案:

答案 0 :(得分:4)

为什么不直接使用手风琴本身的tabs-property?

<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;
        }
    }
}

答案 1 :(得分:2)

我能够使用与您相同的方法实现此工作,但将模板变量移动到自定义p-header内的元素,而不是p-accordion-Tab。

<p-accordionTab>
    <p-header>
        <span #header>
            Header Content
        </span>
    </p-header>
    Body Content
</p-accordionTab>

他们的文档页面底部有关于使用自定义标题内容的信息:https://www.primefaces.org/primeng/#/accordion

答案 2 :(得分:1)

针对带有箭头功能的es6更新了Martin Kronstad's answer

  closeAllAccordionTabs() {
    if (this.accordion && this.accordion.tabs) {
      this.accordion.tabs.forEach(tab => tab.selected = false);
    }
  }

答案 3 :(得分:1)

你可以在 HTML 中做这样的事情

<p-accordion (onOpen)='onTabOpen($event)'>
<p-accordionTab *ngFor='let chart of chartList' header='{{chart.chartName}}'>
<div *ngFor='let content of chartContentList'>{{content.referenceName}}</div>
</p-accordionTab>
</p-accordion>

并在 Typescript 中编写 onTabOpen 事件处理程序

chartList: ChartModel[]=[];
chartContentList: ChartContentModel[]=[];

onTabOpen(e){
const index=e.index
const contentList=this.chartList.filter(x=>x.id==index);
this.service.getChartContent(contentList[0].chartName).subscribe((res)=>{
if(res){
this.chartContentList=res;
}
});
}

因此,当您单击任何标题选项卡时,它会将索引传递给 OnTabOpen 处理程序,然后它会在选项卡下加载内容并动态绑定它。

查看模型:

public interface ChartModel{
id?:number;
chartName?:string;
}

public interface ChartContentModel{
referenceName?:string;
}

答案 4 :(得分:1)

您可以使用 p-accordion 的 selected 属性:

<a (click)="openCloseAll()" >{{openCloseAllText}} all</a>
<p-accordion [multiple]="true" [seleted]="accordionTrigger">
    <div class="row" *ngFor="let category of result.Categories">                        
        <p-accordionTab #accordianTab header="{{category.Name}}">
        </p-accordionTab>                       
    </div>
</p-accordion>
accordionTrigger: boolean = false;    // false is closed and true is opened

changeAccordion() {
    this.accordionTrigger = !this.accordionTrigger;
}

如果你有很多手风琴,你可以做一个布尔数组并为每个手风琴设置一个id。

答案 5 :(得分:0)

这是不需要@ViewChild的替代方法。如果您的手风琴在* ngIf中并且仅在特定条件下才添加到DOM中,则使用此方法(在这种情况下,@ViewChild未定义并且无法设置)。

使用activeIndex属性:

<p-accordion [activeIndex]="openAccordion">

将此变量放入组件中:

openAccordion = -1;

要打开手风琴时,请将this.openAccordion设置为要打开的手风琴标签的索引。例如

this.openAccordion = 0;

或在我正在使用的单元测试中:

fixture.componentInstance.openAccordion = 0;