简体   繁体   English

Angular 禁用按钮不适用于动态名称

[英]Angular Disable button not working with dynamic names

Thank you in advance for helping me with this problem.提前感谢您帮助我解决这个问题。 I'm unable to disable the first button within ngx-datatable-colum which has a dynamic name "Btn_Up_{{row.ordre}}".我无法禁用 ngx-datatable-colum 中具有动态名称“Btn_Up_{{row.ordre}}”的第一个按钮。 I've tried it inside ngOnInit and ngAfterViewInit(): (document.getElementById('Btn_Up_0') as HTMLInputElement).disabled=true;我已经在 ngOnInit 和 ngAfterViewInit() 中尝试过: (document.getElementById('Btn_Up_0') as HTMLInputElement).disabled=true; . . It works only for static names (ids).它仅适用于 static 名称 (id)。

The output of ngx-datatable is a list of buttons with a group names from services. ngx-datatable 的 output 是一个按钮列表,其中包含来自服务的组名。 The idea is to disable the first button (up) and the last button (down).这个想法是禁用第一个按钮(向上)和最后一个按钮(向下)。

My HTML component:我的 HTML 组件:

<ngx-datatable                
        #rows
        class="bootstrap expandable"
        [columnMode]="'flex'"
        [headerHeight]="50"
        [footerHeight]="50"
        [rowHeight]="'auto'"
        [scrollbarV]="false"          
        [rows]="parents">
          <ngx-datatable-column name="Groupe" [flexGrow]="2">
            <ng-template let-row="row" let-rowIndex="rowIndex" ngx-datatable-cell-template>
              <a href="javascript:;" class="text-bold-500 primary text-uppercase" (click)="rechercherApplicationParGroupe(row)">{{ row.libelle }}</a>
            </ng-template>
          </ngx-datatable-column>
          <ngx-datatable-column name="Actions" [flexGrow]="1.5">
            <ng-template let-row="row" let-rowIndex="rowIndex" ngx-datatable-cell-template>
              <button type="button" class="btn btn-sm btn-outline-danger square ml-1 mb-0"  (click)="prepareFormEditGroup(row)" >
                <i class="far fa-edit"></i>
              </button>
              <button type="button" id="Btn_Up_{{row.code}}" (click)='getOrderUp($event,row)' class="btn btn-sm btn-raised btn-primary square ml-1 mb-0">
                <i class="fas fa-angle-up"></i>
              </button>
              <button type="button" name="Btn_Down_{{row.code}}" class="btn btn-sm btn-raised btn-primary square ml-1 mb-0">
                <i class="fas fa-angle-down"></i>
              </button>
            </ng-template>
          </ngx-datatable-column>
  </ngx-datatable>

Instead of disabling the button programmatically as there is a possibility that Angular might not detect this change and hence does not refresh the view, just add the following code for your button:不要以编程方式禁用按钮,因为 Angular 可能无法检测到此更改,因此不会刷新视图,只需为您的按钮添加以下代码:

 <button type="button" class="btn btn-sm btn-outline-danger square ml-1 mb-0" (click)="prepareFormEditGroup(row)" [disabled]="row.code===0">

This should disable the button whenever the "row.code" is set to 0.每当“row.code”设置为 0 时,这应该禁用该按钮。

You can add [disabled] attribute to your button and handle it in your ts file.您可以将 [disabled] 属性添加到您的按钮并在您的 ts 文件中处理它。 like this:像这样:

<button type="button" [disabled]="IsDisabled"
 class="btn btn-sm" (click)="prepareFormEditGroup(row)" >
 <i class="far fa-edit"></i>
</button>

and in your component.ts:在你的 component.ts 中:

IsDisabled: boolean = false;

then base your condition change it.然后根据你的条件改变它。

 this.IsDisabled = true;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM