【问题标题】:PrimeNG stop to open accordionPrimeNG 停止打开手风琴
【发布时间】:2020-08-05 08:23:40
【问题描述】:

如何在 PrimeNG 中强制不打开手风琴。我的意思是在某些情况下,我不希望我选择的手风琴打开。这种方式我试过了,但是不行

https://www.primefaces.org/primeng/showcase/#/accordion

  <p-accordion #accordionQuestion *ngIf="questionItems && questionItems.length > 0"
                     [activeIndex]="currentIndexAccordion"
                     class="text-left mt-2 w-100"
                     (onOpen)="checkOpenAccordion($event)"
                     [expandIcon]="'fa fa-fw fa-chevron-right'"
                     [collapseIcon]="'fa fa-fw fa-chevron-down'">

          <p-accordionTab #accordionTab *ngFor="let question of questionItems; let i = index;" 
                   class="mb-2" id="accordionTab">
            <p-header class="w-100">
              <span class="w-100" [innerText]="question.code + ' - ' + question.text"></span>
            </p-header>
             <question #question *ngIf="i == currentIndexAccordion"
             (formValuesChanged)="questionChanged($event)"
              </question>
          </p-accordionTab> 

   </p-accordion>

checkOpenAccordion方法在哪里

@ViewChild('question', {static: false}) accordion: Accordion;

checkOpenAccordion(index: number) {
           if (this.currentIndexAccordion != index && this.changed) {
                this.disabledOthersAccordion();
                this.accordionQuestion.activeIndex = this.currentIndexAccordion;
           } else {
               this.currentIndexAccordion = index;
           }
}




  disabledOthersAccordion() {
      if (this.accordion && this.accordion.tabs && 
         this.accordionDomande.tabs.length > 0) {
          for (let i = 0; i < this.accordion.tabs.length; i++) {
            if (i !== this.currentIndexAccordion) {
              this.accordion.tabs[i].disabled = true;
            }
          }
        }
    }


questionChanged(changed) {
  this.changed = changed;
}

【问题讨论】:

  • 你能创建 stackblitz 吗?
  • 很遗憾没有
  • 你想要什么
  • 我有一些手风琴......每个手风琴都有一个表格。如果我将某些内容更改为表单(所以何时更改)如果我尝试单击另一个手风琴,我想停止或阻止打开新的手风琴点击并留在我当前的手风琴中。
  • 你能做一个stackblitz吗here

标签: angular accordion primeng


【解决方案1】:

为此,您需要在 html 示例中添加一些类似 (input)input 方法

(input)="FirstAccordion($event)"

为此,您可以检查用户是否在输入字段中输入了某些内容,例如

let first = event.target.value;

之后,您需要在每个标题上添加[disabled]="whatEverYouWantName" 属性,例如

<p-accordionTab header="Address Name" [disabled]="address"></p-accordionTab>

如果您想默认首先关闭手风琴,然后在第一个选项卡上添加此 [selected]="false" 属性,例如

<p-accordionTab header="first Name" [selected]="true">

完整代码示例

HTML

<p-accordion>
    <p-accordionTab header="first Name" [selected]="true" [disabled]="firstname">
        <form>
            first Name<br/>
            <input type="text" (input)="FirstAccordion($event)"/> &nbsp;
      <button type="button">Save</button>
        </form>
    </p-accordionTab>
    <p-accordionTab header="Last Name" [disabled]="lastname">
        Last Name<br/>
        <input type="text" (input)="SecondAccordion($event)"/>&nbsp;
      <button type="button">Save</button>
    </p-accordionTab>
        <p-accordionTab header="Address Name" [disabled]="address">
            Address Name<br/>
            <input type="text" (input)="ThirdAccordion($event)"/>&nbsp;
      <button type="button">Save</button>
    </p-accordionTab>
</p-accordion>

TS

firstname: boolean = false;
  lastname: boolean = false;
  address: boolean = false;

  FirstAccordion(event) {
    console.log(event.target.value);
    let first = event.target.value;
    if (first) {
      this.lastname = true;
      this.address = true;
    } else if (first == '') {
      this.lastname = false;
      this.address = false;
    }
  }

  SecondAccordion(event) {
    console.log(event.target.value);
    let last = event.target.value;
    if (last) {
      this.firstname = true;
      this.address = true;
    } else if (last == '') {
      this.firstname = false;
      this.address = false;
    }
  }

  ThirdAccordion(event) {
    console.log(event.target.value);
    let address = event.target.value;
    if (address) {
      this.firstname = true;
      this.lastname = true;
    } else if (address == '') {
      this.firstname = false;
      this.lastname = false;
    }
  }

现在你必须根据你的要求做一些逻辑,并且在保存按钮上没有写任何你必须写的逻辑

这是我的 stackblitz Check here or preview here

【讨论】:

  • 这和我做的一样。检查我编辑的问题。不同的是我有一个viewChild
  • 你想要这个要求,有很多方法可以做到,但你需要把逻辑放在上面
猜你喜欢
  • 2020-05-12
  • 2014-07-20
  • 2022-07-25
  • 2017-02-10
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多