【问题标题】:How to remove and add dropdown values based on the condition selected by another dropdown in angular?如何根据另一个下拉菜单选择的条件删除和添加下拉列表值?
【发布时间】:2021-09-05 15:23:04
【问题描述】:

我的要求: 我必须创建一个包含两个选择下拉菜单的表单行,最后添加和删除按钮 1. 第一个下拉列表将包含 ed.. ['one', 'two', 'three','four'] 的值列表 2. 第二个下拉菜单的条件是['and','or']

假设选择表单值的第一行,如 ['one'] ['and'] 然后单击添加按钮,然后将创建第二行。这里的第一个下拉列表不应该显示“一”值,因为条件是“和”。如果用户选择,那么它应该是所有值。对于我必须以角度创建逻辑的所有行都是相似的。

HTML 代码:

<div class="state-filter-conditions-grid subs-model-sec filter-grid" *ngFor="let filterCondition of filters;  index as i">
  <!-- OUTPUT PROPERTY -->

    <ng-container *ngTemplateOutlet="filterView == 'subscription'? attrs: map; context: { index: i }"></ng-container>


  <!-- CONNECTOR CONDITION -->
  <div class="dt-attr valueCondition ctrl-condition minimal">
    <div class="abs-cheveron select-box-cheveron">
      <select class="state-select" [(ngModel)]="filters[i].op">
        <option *ngFor="let val of op" [value]="val">{{val}}</option>
      </select>
    </div>

  </div>
  <!-- INPUT -->
  <ng-container *ngTemplateOutlet="filterView != 'subscription'? attrs: map; context: { index: i }"></ng-container>
  <!-- SELECT -->
  <div class="dt-attr icons center-align">
    <select class="ctrl-condition" *ngIf="!(i==filters.length-1)" [(ngModel)]="filters[i].logop" (change)="operatorChange(filters[i])">
      <option *ngFor="let val of logop" [value]="val">{{val}}</option>
    </select>

  </div>
  <!-- ICONS -->

  <div class="dt-attr icons center-align">
    <button *ngIf="i==filters.length-1" class="add-btn" (click)="addStateConditionRow()"><i
        class="fa fa-sm fa-plus add-icon" aria-hidden="true"></i></button>
    <button *ngIf="i>0" class="delete-btn" (click)="deleteStateConditionRow(i)"><i
        class="fa fa-sm fa-trash delete-icon"></i></button>
  </div>
</div>

<ng-template  #attrs let-i="index">
  <div class="dt-attr ">
    <ng-container *ngIf="!dataProps">
      <input class="ctrl-attr" type="text" [(ngModel)]="filters[i].value">
    </ng-container>
    <ng-container *ngIf="dataProps">
      <select class="value-select" [(ngModel)]="filters[i].value">
        <option *ngFor="let data of dataProps" [value]="data.attrId">{{data.attrName}}</option>
      </select>
    </ng-container>
  </div>
</ng-template>

<ng-template  #map let-i="index">
  <div class="dt-attr ctrl-condition minimal">
    <input class="ctrl-attr" type="text" clickOutside (clickOutside)="closeAccordion($event)" (click)="openAccordion($event)" [(ngModel)]="filters[i].attribute">
    <div *ngIf="showAccordion" class="state-filter-accordion" style="position: absolute;
    top: 105%;
    width: 100%;z-index:1">
      <app-common-accordion-mapping [filterInputRef]="filterInputRef" [inputAttributes]='inputAttributes' ></app-common-accordion-mapping>
    </div>
  </div>
</ng-template>

你们能帮我实现这个目标吗?

提前致谢。

【问题讨论】:

  • 这个网站是寻求代码方面的帮助,而不是某人为你编写代码...显示您到目前为止所获得的内容,也许有人会为您指明正确的方向...
  • 你能设置一个 sn-p 或一个 stackblitz/codepen/jsfiddle.. 一些可测试的东西吗?如果没有看到您的数据源和示例,就不可能提供帮助 - 更不用说您的实际问题很难理解。如果htere是一个例子,我们可以看到数据,html,ts等会更容易。谢谢
  • @bagya,你可以使用&lt;ng-container *ngFor="let val of op"&gt;&lt;option *ngIf="&lt;replace by your condition&gt;"&gt;..&lt;/option&gt;&lt;/ng-container&gt;你需要找到“条件”来显示选项。请记住,当您使用 ngModel 时,您可以使用变量来创建条件

标签: javascript angular


【解决方案1】:

我认为您正在寻找的是根据特定逻辑动态填充下拉列表。 This stackoverflow 的答案涵盖了 AngularJS 和 a stackoverflow question + blog 的 Angular 8 。但是我无法帮助您处理业务逻辑本身,因为我不清楚。

【讨论】:

  • 链接的答案适用于 AngularJS,而此问题适用于 Angular 2.0 或更高版本。
  • @MikkelChristensen 感谢您的指出,我用 Angular 8 的示例更新了我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 2017-01-11
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
相关资源
最近更新 更多