简体   繁体   English

如何始终从动态选项卡中预先选择第一个选项卡?

[英]How to pre selected first tab always from the dynamic tabs?

I am working on an angular 4 project, I need to create a structure in which there are three checkboxes. 我正在做一个angular 4项目,我需要创建一个包含三个复选框的结构。 A tab should be dunamically created once the first checkbox is selected, so by that logic if I select all three of them then there will be three tabs, the problem I am stuck at it as follows:I need at any gievn point of time, one tab to be selected, so lets say if first checkbox is selected then first tab should be preselected, if first checkbox is unselected then second tab should be selected..any help this regard will be highly appreciated. 一旦选择了第一个复选框,就应该自动创建一个标签页,因此按照这种逻辑,如果我选择了所有三个标签页,则将有三个标签页,我遇到的问题如下:我需要在任何时间点,可以选择一个选项卡,因此可以说,如果选择了第一个复选框,则应预选择第一个选项卡,如果未选择第一个复选框,则应选择第二个选项卡。在这方面的任何帮助将不胜感激。 Thanks! 谢谢!

For better understanding I had created a plunker example: https://plnkr.co/edit/2UPBf67y2ExmLaePP3VV?p=preview 为了更好地理解,我创建了一个小例子: https ://plnkr.co/edit/2UPBf67y2ExmLaePP3VV ? p = preview

Html code: HTML代码:

<div *ngFor="let industry of industries">   
    <input type="checkbox" (change)="onIndustryChange($event.target.checked, industry.id)">
    <span>{{industry?.name}}</span>
</div>
<div class="tab-wrapper">
  <ul class="nav nav-tabs app-tab-menu">
     <ng-container *ngFor="let industry of industries">
        <li *ngIf="isIndustrySelected(industry.id)">
          <a href="#industry_{{industry.id}}" data-toggle="tab">{{industry?.name}}</a>
        </li>
     </ng-container>
  </ul>
<div class="tab-content">
  <ng-container *ngFor="let industry of industries">
    <div class="tab-pane" id="industry_{{industry.id}}" *ngIf="isIndustrySelected(industry.id)">
      <span>{{industry.name}}</span>
    </div>
  </ng-container>
</div>

You need to check if the current index is equal to 0, then set the [checked] property of the checkbox. 您需要检查当前索引是否等于0,然后设置复选框的[checked]属性。

<div *ngFor="let industry of industries; let i = index">    
    <input type="checkbox" [checked]="i ==0" (change)="onIndustryChange($event.target.checked, industry.id)">
    <span>{{industry?.name}}</span>

<ul class="nav nav-tabs app-tab-menu">
  <ng-container *ngFor="let industry of industries; let i = index">
     <li *ngIf="isIndustrySelected(industry.id) || i == 0">
        <a href="#industry_{{industry.id}}" data-toggle="tab">{{industry?.name}}</a>
     </li>
  </ng-container>
</ul>

** UPDATE ** **更新**

Since this will not work based on your requirements, here is my suggestion: 由于这无法根据您的要求进行,因此,我的建议是:

  • If I'm not wrong, industry would come from an api call so it will return a promise. 如果我没看错, industry将来自api调用,因此它将返回一个承诺。 Put this code on ngOnInit() so the html will not render until the data arrived. 将此代码放在ngOnInit()以便在数据到达之前不会呈现html。 Then on put the first index of industry to selectedIndustries. 然后将第一个行业指数添加到选定的行业。 On html, you just add [checked]="isIndustrySelected(industry.id)" on your first div. 在html上,您只需在第一个div上添加[checked]="isIndustrySelected(industry.id)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Angular 4选项卡中选择选项卡时,如何调用函数? - How to call a function when a tab is selected in Angular 4 Tabs? 如何禁用除angular4材料中选定选项卡之外的所有选项卡 - How to disable all tabs except selected tab in angular4 material Angular中的动态标签:在动态标签内容中导航 - Dynamic tabs in Angular : navigation in dynamic tab content Angular Dynamic Mat Tab - 如何将新创建的选项卡添加为第一个选项卡 - Angular Dynamic Mat Tab - How to add newly created tab as the first tab 角形的金属片未打开出口的第一个片 - Angular metrial tabs not opening the first tab for outlets Ionic 2-如何在切换标签页时取回所选标签页的根导航页面 - Ionic 2 - How to get back the root nav page of selected tab while switching tabs 如何从 Angular 和 Typescript 的选项卡数组中禁用选项卡元素 - How to disable a tab element from array of tabs in Angular and Typescript 如何将参数从选项卡容器传递到离子 4 中的一个选项卡 - How to pass parameter from Tabs container to one tab in ionic 4 Ionic 2:添加新的动态选项卡后刷新选项卡视图 - Ionic 2 : Refresh tabs view after adding a new dynamic tab 带有动态清晰度选项卡的 Angular 表单在添加选项卡后获取 ExpressionChangedAfterItHasBeenCheckedError - Angular form with dynamic clarity tabs get ExpressionChangedAfterItHasBeenCheckedError after add tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM